Make multicast sockets work under windows

msdn article 737550 states that "the preferred method is to call the
bind function to associate a socket with a local IP address and then
join the multicast group. Although this order of operations is not
mandatory, it is strongly recommended".
Since binding to the multicast address seems to fail, let's try binding
to INADDR_ANY and joint thereafter.
This commit is contained in:
Torsten Dreyer
2011-05-27 21:06:06 +02:00
parent 3a07e2fe21
commit b06e38699a
+16
View File
@@ -260,15 +260,31 @@ int Socket::bind ( const char* host, int port )
int result; int result;
assert ( handle != -1 ) ; assert ( handle != -1 ) ;
IPAddress addr ( host, port ) ; IPAddress addr ( host, port ) ;
#if !defined(WINSOCK)
if( (result = ::bind(handle,(const sockaddr*)&addr,sizeof(IPAddress))) < 0 ) { if( (result = ::bind(handle,(const sockaddr*)&addr,sizeof(IPAddress))) < 0 ) {
SG_LOG(SG_IO, SG_ALERT, "bind(" << host << ":" << port << ") failed. Errno " << errno << " (" << strerror(errno) << ")"); SG_LOG(SG_IO, SG_ALERT, "bind(" << host << ":" << port << ") failed. Errno " << errno << " (" << strerror(errno) << ")");
return result; return result;
} }
#endif
// 224.0.0.0 - 239.255.255.255 are multicast // 224.0.0.0 - 239.255.255.255 are multicast
// Usage of 239.x.x.x is recommended for local scope // Usage of 239.x.x.x is recommended for local scope
// Reference: http://tools.ietf.org/html/rfc5771 // Reference: http://tools.ietf.org/html/rfc5771
if( ntohl(addr.getIP()) >= 0xe0000000 && ntohl(addr.getIP()) <= 0xefffffff ) { if( ntohl(addr.getIP()) >= 0xe0000000 && ntohl(addr.getIP()) <= 0xefffffff ) {
#if defined(WINSOCK)
struct sockaddr_in a;
a.sin_addr.S_un.S_addr = INADDR_ANY;
a.sin_family = AF_INET;
a.sin_port = htons(port);
if( (result = ::bind(handle,(const sockaddr*)&a,sizeof(a))) < 0 ) {
SG_LOG(SG_IO, SG_ALERT, "bind(any:" << port << ") failed. Errno " << errno << " (" << strerror(errno) << ")");
return result;
}
#endif
struct ip_mreq mreq; struct ip_mreq mreq;
mreq.imr_multiaddr.s_addr = addr.getIP(); mreq.imr_multiaddr.s_addr = addr.getIP();
mreq.imr_interface.s_addr = htonl(INADDR_ANY); mreq.imr_interface.s_addr = htonl(INADDR_ANY);