Bonjour,
Je suis actuellement en train de créer des sockets multicast en c/c++ sous linux. Avec ethereal, les messages multicast sont bien émis et arrivent à la machine en réception mais l'interface semble les jeter car ils ne sont pas traités. J'ai vérifié que le noyau supportait le multicast. Je pense donc que le problème vient de mon code. J'aimerais que les paquets arrivent sur l'interface eth0 qui est configurée avec l'adresse 192.168.18.15.
Code voici le code en réception : 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 #define GROUP "239.137.194.111" #define PORT 55555 int main() { struct ip_mreq imr; int sock_channel; struct sockaddr_in m_socketAddr; const char * multicastIPaddr; unsigned short port = 55000; int len; struct sockaddr_in remote_addr; socklen_t remote_addr_len = sizeof(struct sockaddr_in); unsigned char recv_buffer[9000]; multicastIPaddr = GROUP; // open the socket sock_channel = socket(PF_INET, SOCK_DGRAM, 0); if(sock_channel < 0) { perror("Can't open the receive socket\n"); exit(1); } // initialize the channel bzero(&m_socketAddr, sizeof(m_socketAddr)); m_socketAddr.sin_family = AF_INET; m_socketAddr.sin_port = port; if(inet_aton("192.168.18.15", &(m_socketAddr.sin_addr))<0) { perror("cannot get the locale IP address for 192.168.18.15 \n"); exit(1); } one = 1; if(setsockopt(sock_channel, SOL_SOCKET, SO_REUSEADDR, (char *)&one, sizeof(one))<0) { perror("Error in reusing addr\n"); exit(1); } // creation of the link between the socket and its port if (bind(sock_channel, (struct sockaddr *)&m_socketAddr, sizeof(m_socketAddr)) < 0) { perror("problem with function bind errno \n"); exit(1); } // activation of the multicast memset(&imr, 0, sizeof(struct ip_mreq)); if(inet_aton(multicastIPaddr, (struct in_addr*)&(imr.imr_multiaddr.s_addr))<0) { perror("cannot get the multicast IP address for 239.137.194.111\n"); exit(1); } if(inet_aton("192.168.18.15", (struct in_addr*)&(imr.imr_interface.s_addr))<0) { perror("cannot get the local IP address for multicast interface 192.168.18.15 \n"); exit(1); } if (setsockopt(sock_channel, IPPROTO_IP, IP_ADD_MEMBERSHIP, (void *)&imr, sizeof(struct ip_mreq)) < 0) { perror("setsockopt: join multicast group with IP_ADD_MEMBERSHIP failed\n"); exit(1); } while(1) { len = recvfrom(sock_channel, recv_buffer, sizeof(recv_buffer), 0, (struct sockaddr*)&remote_addr, &remote_addr_len); if(len == -1) { perror("%s reception of udp datagramm failed\n"); exit(1); } printf("%s:%d [%d]\t: %s\n", inet_ntoa(remote_addr.sin_addr), ntohs(remote_addr.sin_port), len, recv_buffer); } return 0; }
Code voici le code en émission : 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 #define GROUP "239.137.194.111" #define PORT 55555 int main() { unsigned char ttl = 1; int sock_channel; struct sockaddr_in m_socketAddr; const char * multicastIPaddr; unsigned short port = 55000; int len; struct sockaddr_in remote_addr; socklen_t remote_addr_len = sizeof(struct sockaddr_in); unsigned char send_buffer[9000]; multicastIPaddr = GROUP; char *requete; requete = "bonjour!"; // open the socket sock_channel = socket(PF_INET, SOCK_DGRAM, 0); if(sock_channel < 0) { perror("Can't open the receive socket, errno %d (%s)\n"); exit(1); } // Set destination characteristics to send the datagramms // get the remote IP address if(inet_aton(GROUP, &(remote_addr.sin_addr))<0) { perror("cannot get the remote IP address for %s \n"); exit(1); } remote_addr.sin_family = AF_INET; remote_addr.sin_port = port; // initialize the channel bzero(&m_socketAddr, sizeof(m_socketAddr)); m_socketAddr.sin_family = AF_INET; m_socketAddr.sin_port = port; if(inet_aton("192.168.18.2", &(m_socketAddr.sin_addr))<0) { perror("cannot get the locale IP address for 192.168.18.2 \n"); exit(1); } // creation of the link between the socket and its port if (bind(sock_channel, (struct sockaddr *)&m_socketAddr, sizeof(m_socketAddr)) < 0) { perror("problem with function bind errno\n"); exit(1); } // Activation of the multicast packets sending if(setsockopt(sock_channel, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0) { perror("setsockopt: IP_MULTICAST_TTL activation failed\n"); exit(1); } len = strlen(requete) + 1; memcpy(send_buffer, requete, len); if(sendto(sock_channel, send_buffer, len, 0, (struct sockaddr *) &remote_addr, sizeof(remote_addr))< len) { perror("Error: sendto(..,0,..) errno %s (%d)\n"); exit(1); } return 0; }
Merci de me donner votre avis pour m'expliquer pourquoi les paquets ne sont pas acceptés par la machine en réception. (Le code compile et il n'y a pas de messages d'erreur)
Partager