understanding multicast from a programmer's per...

       when I was doing on ws-discovery, I just finished the method "Probe", but this is not enough.
methods "Hello" and "Bye" is also important since all the device will send "Hello" to me when they are
on line, and send "Bye" to me when they are off line. If I finished this, all will look like intelligent. So I 
learned the packet deeply. I found that before sending probe method, there is a IGMP pakcket, and after
several hours' studying, I found I have done the wrong version of IGMP, I would rather choose IGMP v3.
And I am sure I will spare some time finishing IGMPV3 too.

okay, let's go into the key point:
ws-discovery regulates the multicast-address : 239.255.255.250 and port : 3702, so If I wanna to receive
"Hello" or "Bye" data , all I need to do is join this group, and wait for the data coming, ok, here we go :
first , create a udp socket.
second,  join this group:
 struct ip_mreq mAddr;
mAddr.imr_interface.s_addr = INADDR_ANY;
mAddr.imr_multiaddr.s_addr = inet_addr("239.255.255.250");  #include <WS2TCPIP.H>

ret = setsockopt(sockfd,IPPROTO_IP,IP_ADD_MEMBERSHIP,(const char *)&mAddr,sizeof(mAddr));
before we call recvfrom, we should first bind local address to port 3702, and we can also 
set the following ip multicast options: 
IP_MULTICAST_TTL, IP_MULTICAST_LOOP in the level of IPPROTO_IP, u can reference information to 
http://msdn.microsoft.com/en-us/library/ms738586(v=VS.85).aspx

finally, leave this group:
        setsockopt(sockfd,IPPROTO_IP,IP_DROP_MEMBERSHIP,(const char *)&mAddr,sizeof(mAddr));

你可能感兴趣的:(Multicast)