Bonjour.
Je cherche a faire dialoguer deux petit programme. Le premier envoi des données de navigation et le deuxième les traites.
La connexion, marche, l'envoi des messages également, mais des que je place la boucle while pour une écoute en continue, ca ne marche plus et les donnée sont plus qu'incohérente...
Ma procedure d'envoi est :
-envoi de la chaine "NavData" puis envoi de 8 valeurs.
Merci d'avance.
serveur:
et le client:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113 #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> #define INVALID_SOCKET -1 #define SOCKET_ERROR -1 #define closesocket(s) close(s) typedef int SOCKET; typedef struct sockaddr_in SOCKADDR_IN; typedef struct sockaddr SOCKADDR; #include <stdio.h> #include <stdlib.h> #include <string.h> #define PORTDATA 8010 void* serveurData(void* d) { int erreur = 0; NavData data; int navDataInProgress = -1; SOCKET sock; SOCKADDR_IN sin; SOCKET csock; SOCKADDR_IN csin; char buffer[32] = "Bonjour !"; char recv_data[1024]; socklen_t recsize = sizeof(csin); int sock_err; /* Si les sockets Windows fonctionnent */ if (!erreur) { sock = socket(AF_INET, SOCK_STREAM, 0); /* Si la socket est valide */ if (sock != INVALID_SOCKET) { printf("La socket %d est maintenant ouverte en mode TCP/IP\n", sock); /* Configuration */ sin.sin_addr.s_addr = htonl(INADDR_ANY); /* Adresse IP automatique */ sin.sin_family = AF_INET; /* Protocole familial (IP) */ sin.sin_port = htons(PORTDATA); /* Listage du port */ sock_err = bind(sock, (SOCKADDR*)&sin, sizeof(sin)); /* Si la socket fonctionne */ if (sock_err != SOCKET_ERROR) { /* Démarrage du listage (mode server) */ sock_err = listen(sock, 5); printf("Listage du port %d...\n", PORTDATA); /* Si la socket fonctionne */ if (sock_err != SOCKET_ERROR) { csock = accept(sock, (SOCKADDR*)&csin, &recsize); printf("Un client se connecte avec la socket %d de %s:%d\n", csock, inet_ntoa(csin.sin_addr), htons(csin.sin_port)); while (1) { int32_t i; int bytes_read = recv(sock,&i,sizeof(i),0); int val= ntohl (i); recv_data[bytes_read] = '\0'; if (val == 1315010116) { printf("Recepeption de NavData"); navDataInProgress = 0; } if (navDataInProgress != -1) { switch (navDataInProgress) { case 0 : data.ctrl_state = val; navDataInProgress++; break; //[...] case 8: data.vz = val; printf("Control state : %i \n", data.ctrl_state); printf("Battery level : %i/100 \n", data.vbat_flying_percentage); printf("Orientation : [Theta] %i [Phi] %i [Psi] %i \n", data.theta, data.phi, data.psi); printf("Altitude : %i \n",data.altitude); printf("Speed : [vX] %i [vY] %i [vZ] %i \n", data.vx, data.vy, data.vz); navDataInProgress=-1; break; } } } shutdown(csock, 2); } } /* Fermeture de la socket */ closesocket(sock); } #if defined (WIN32) WSACleanup(); #endif } getchar(); }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118 #if defined (WIN32) #include <winsock2.h> typedef int socklen_t; #elif defined (linux) #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> #define INVALID_SOCKET -1 #define SOCKET_ERROR -1 #define closesocket(s) close(s) typedef int SOCKET; typedef struct sockaddr_in SOCKADDR_IN; typedef struct sockaddr SOCKADDR; #endif #include <stdio.h> #include <stdlib.h> #define PORT 8010 int main(void) { #if defined (WIN32) WSADATA WSAData; int erreur = WSAStartup(MAKEWORD(2,2), &WSAData); #else int erreur = 0; #endif SOCKET sock; SOCKADDR_IN sin; char buffer[32] = ""; /* Si les sockets Windows fonctionnent */ if (!erreur) { /* Création de la socket */ sock = socket(AF_INET, SOCK_STREAM, 0); /* Configuration de la connexion */ sin.sin_addr.s_addr = inet_addr("127.0.0.1"); sin.sin_family = AF_INET; sin.sin_port = htons(PORT); /* Si l'on a réussi à se connecter */ if (connect(sock, (SOCKADDR*)&sin, sizeof(sin)) != SOCKET_ERROR) { printf("Connection à %s sur le port %d\n", inet_ntoa(sin.sin_addr), htons(sin.sin_port)); while (1) { char send_data[1028]; printf("send NavData (y/n):"); gets(send_data); if ((strcmp(send_data , "n") == 0) || strcmp(send_data , "N") == 0) break; else { int ctrl_state = 20; int vbat_flying_percentage = 80; int theta = 50; int phi = 34; int psi = 13; int altitude = 24; int vx = 22; int vy = 34; int vz = 55; send(sock, "NavData", strlen("NavData"), 0 ); int32_t i; i = htonl (ctrl_state); send(sock, &i, sizeof(i), 0 ); i = htonl(vbat_flying_percentage); send(sock, &i, sizeof(i), 0 ); i = htonl(theta); send(sock, &i, sizeof(i), 0 ); i = htonl(phi); send(sock, &i, sizeof(i), 0 ); i = htonl(psi); send(sock, &i, sizeof(i), 0 ); i = htonl(altitude); send(sock, &i, sizeof(i), 0 ); i = htonl(vx); send(sock, &i, sizeof(i), 0 ); i = htonl(vy); send(sock, &i, sizeof(i), 0 ); i = htonl(vz); send(sock, &i, sizeof(i), 0 ); } } } /* sinon, on affiche "Impossible de se connecter" */ else { printf("Impossible de se connecter\n"); } /* On ferme la socket */ closesocket(sock); #if defined (WIN32) WSACleanup(); #endif } /* On attend que l'utilisateur tape sur une touche, puis on ferme */ getchar(); return EXIT_SUCCESS; }
Partager