Bonjour a tous,
Dans le cadre d'un projet d'ecole d'ingenieur, je dois faire communiquer un pc avec plusieurs systemes (stm32f4);
L'ordinateur doit d'abord envoyer une "discovery request" en multicast à une adresse de groupe (les systèmes) ; Cette requête contient l'information d'un canal unicast;
Chaque périphérique du réseau s'abonne donc à l'adresse de groupe convenue et, lorsqu'il reçoit the "discovery request", il s'identifie en utilisant les informations du canal unicast fournies dans la requête.
Voici les détails des informations échangées entre les systèmes: https://github.com/prodrig/picudo
J'ai donc créé 2 projets différents; un projet Serveur et un projet Client servant à simuler les systèmes.
Le projet Serveur contient les fonctions :
- void multi_socket_send(int sockfd, char multi_add, int multi_port, u_int8_t id, struct request_packet msg);
- void unicast_socket_recvfrom(int, struct sockaddr_in, int listen_port);
Le projet Client contient les fonctions :
- void multi_socket_rvcfrom(int sockfd, struct sockaddr_in their_addr, char multi_add, int listen_multi_port);
- void unicast_socket_sendto(int sockfd, int port, char address, struct answer_packet data);
Mais voila qu'arrivent mes problemes; je dois utiliser des threads pour chaque communications en multicast ou en unicast. Mais impossibles de savoir comment faire communiquer ses threads.
Pour le Multithread du serveur; voici le code:
Code C : 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 #include <windows.h> #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <string.h> #include <sys/time.h> /* gettimeofday */ #include <pthread.h> #include "unicast_socket_rcvfrom.h" #include "timestamp.h" #include "app_config.h" #include "time_conversion_request.h" int nb; HANDLE hThread2; DWORD Thread2ID, Thread2Param = 100; #define LISTEN_PORT 1234 /* The port the server listens on. */ DWORD WINAPI Thread1( void ) { struct request_packet rp; u_int8_t id; switch (id) { case PROTOCOL_ID_DISCOVERY: rp.wk_ip_address= MULTI_ADDR; rp.wk_udp_port= MULTI_PORT; rp.originateTimestamp ='\0'; rp.refTimestamp = '\0'; rp.revcTimestamp = '\0'; rp.transmitTimestamp ='\0'; break; case PROTOCOL_ID_START_CMD: /* Do whatever is needed to start sampling and sending audio data */ rp.wk_ip_address= '\0'; rp.wk_udp_port= '\0'; rp.originateTimestamp = '\0'; rp.refTimestamp = '\0'; rp.revcTimestamp = '\0'; rp.transmitTimestamp = '\0'; break; case PROTOCOL_ID_STOP_CMD: /* Do whatever is needed to stop sampling */ rp.wk_ip_address= '\0'; rp.wk_udp_port= '\0'; rp.originateTimestamp = '\0'; rp.refTimestamp = '\0'; rp.revcTimestamp = '\0'; rp.transmitTimestamp = '\0'; break; case PROTOCOL_ID_TIMESTAMP: /* Do whatever is needed to update the local time */ rp.wk_ip_address= '\0'; rp.wk_udp_port= '\0'; rp.originateTimestamp = ntp_to_tv(rp.originateTimestamp); rp.refTimestamp = ntp_to_tv(rp.refTimestamp); rp.revcTimestamp = ntp_to_tv(rp.revcTimestamp); rp.transmitTimestamp = ntp_to_tv(rp.revcTimestamp); break; default: DieWithError ("ERROR, illegal prot_id\n"); break; } while (1) { multi_socket_send(MULTI_ADDR, MULTI_PORT, id, rp); } return 0; } DWORD WINAPI Thread2( void ) { int sock;//, numbytes; struct sockaddr_in listen_addr; /* server address info */ while (1) { unicast_socket_recvfrom(sock, listen_addr, LISTEN_PORT); } close(sock); return 0; } void main() { printf("Creation d'un thread...\n\r"); hThread1 = CreateThread(NULL, 0, Thread1, &Thread1Param, 0, &Thread2ID); if (hThread1 == NULL) { printf("Erreur de creation du thread !\n\r"); } else { printf("Creation de thread reussie !\n\r"); } getchar(); }
Pour le Multithread du Client:
Code C : 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 #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <string.h> #include <sys/time.h> /* gettimeofday */ #include "app_config.h" #include "unicast_socket_sendto.h" #define SERVER_PORT 123 /* server port the client connects to */ #define POLL_TIME 5 /* Number of seconds to wait until sending to the server again */ #include <windows.h> #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <string.h> #include <sys/time.h> /* gettimeofday */ #include <pthread.h> #include "unicast_socket_sendto.h" #include "app_config.h" int nb; HANDLE hThread2; DWORD Thread2ID, Thread2Param = 100; DWORD WINAPI Thread1( void ) { int sockfd; struct sockaddr_in their_addr; /* Group address to listen on */ char msg; char multi_add; int listen_multi_port; // Infinite loop until exit while (1) { multi_socket_rvcfrom(sockfd, their_addr, MULTI_ADDR, MULTI_PORT); sleep(POLL_TIME); } close(sockfd); return 0; } DWORD WINAPI Thread2( void ) { int sockfd; char msg; // Infinite loop until exit while (1) { unicast_socket_sendto(sockfd, SERVER_PORT, UNI_ADDR, MESSAGE); sleep(POLL_TIME); } close(sockfd); return 0; } void main() { printf("Creation d'un thread...\n\r"); hThread2 = CreateThread(NULL, 0, Thread2, &Thread2Param, 0, &Thread2ID); if (hThread2 == NULL) { printf("Erreur de creation du thread !\n\r"); } else { printf("Creation de thread reussie !\n\r"); } getchar(); }
Avez vous une idée de comment manipuler les threads dans le main de chacun svp?![]()








Répondre avec citation
Partager