Bonjour à tous,

Je cherche à envoyer et recevoir un message en multicast d'un 'serveur' vers un 'client'. J'ai donc créée deux projet S et C, l'un contenant une fonction multiSocketRcvfrom() et l'autre multiSocketSendto.h...j'ai donc réussi à envoyer le message via la seconde fonction, mais je n'arrive pas à le réceptionner avec la première...

fonction multiSocketRcvfrom() :
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
 
#include "multiSocketRcvfrom.h"
 
int multiSocketRcfrom(int port_dest, char *rcv_ip_addr){
    int iRecvLen, cb;                       // Length of recv_sin
    char szMessage[100];                // ASCII string
 
    SOCKET Sock = INVALID_SOCKET;       // Datagram window socket
 
    struct ip_mreq mreq;                // Used in adding or dropping
                                        // multicasting addresses
    SOCKADDR_IN local_sin,              // Local socket's address
                recv_sin;               // Holds the source address upon
                                        // recvfrom function returns
    #ifdef _WIN32
    WSADATA WSAData;                    // Contains details of the
                                      // Winsock implementation
 
    // Initialize Winsock.
    if (WSAStartup (MAKEWORD(1,1), &WSAData) == 0)
    {
        printf( "WSAStartup succeed.\n");
    }
    else
    {
    	printf( "WSAStartup failed! Error: %d\n", SOCKET_ERRNO);
        return FALSE;
    }
    #endif
 
    // Create a datagram socket, Sock.
    if ((Sock = socket (AF_INET, SOCK_DGRAM, 0)) != -1)
    {
        printf( "Allocating socket succeed.\n");
    }
    else
    {
    	printf( "Allocating socket failed! Error: %d\n", SOCKET_ERRNO);
        return FALSE;
    }
 
    // Fill out the local socket's address information.
    local_sin.sin_family = AF_INET;
    local_sin.sin_port = htons (port_dest);
    local_sin.sin_addr.s_addr = htonl (INADDR_ANY);
 
    // Associate the local address with Sock.
    if (bind (Sock,
             (struct sockaddr FAR *) &local_sin,
              sizeof (local_sin)) == SOCKET_ERROR)
    {
    	printf( "Binding socket failed! Error: %d\n", SOCKET_ERRNO);
    	closesocket (Sock);
        return FALSE;
    }
 
    // Join the multicast group from which to receive datagrams.
    mreq.imr_multiaddr.s_addr = inet_addr (rcv_ip_addr);
    mreq.imr_interface.s_addr = INADDR_ANY;
 
    if (setsockopt (Sock,
                    IPPROTO_IP,
                    IP_ADD_MEMBERSHIP,
                    (char FAR *)&mreq,
                    sizeof (mreq)) == SOCKET_ERROR)
    {
    	printf( "setsockopt failed! Error: %d\n", SOCKET_ERRNO);
    	closesocket (Sock);
    	return FALSE;
    }
 
    iRecvLen = sizeof (recv_sin);
 
    // Receive data from the multicasting group server.
    if( (cb = recvfrom (Sock,
    					szMessage,
						100,
						0,
						(struct sockaddr FAR *) &recv_sin,
						&iRecvLen)) != -1)
    {
    	printf("successful message reception.\n");
    	szMessage[cb] = 0;
        printf( szMessage );
    }
    else
    {
    	printf( "recvfrom failed! Error: %d\n", SOCKET_ERRNO);
    	closesocket (Sock);
    	return FALSE;
    }
 
    // Disable receiving on Sock before closing it.
    shutdown (Sock, 0x00);
 
    // Close Sock.
    closesocket (Sock);
 
    return TRUE;
 
}
L'erreur suivant apparaît:
WSAStartup succeed.
Allocating socket succeed.
Binding succeed

setsockopt failed! Error: 10049
D'après mes recherches, c'est une question de compatibilité d'adresse IP, mais je ne vois pas laquelle.

Le problème se trouvant dans cette partie, avez vous des idées ?

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
setsockopt (Sock,
                    IPPROTO_IP,
                    IP_ADD_MEMBERSHIP,
                    (char FAR *)&mreq,
                    sizeof (mreq)) == SOCKET_ERROR)

L'addresse IP et le port que j'ai utilisé pour envoyer le message sont respectivement : "192.168.0.1" et 7


Merci d'avance pour vos réponses,


Bien à vous


beaug