Winsock Multicast setOption error

Evan Davis cptroot at gmail.com
Mon Feb 3 17:47:09 PST 2014


Hello,

I've been working on using std.socket to do multicast programming 
with d. So far I have converted c code into d code to obtain the 
following, but I am still getting errors.

The code is as follows (windows only):

   InternetAddress localAddress = new InternetAddress(port);
   InternetAddress multicastGroupAddr = new 
InternetAddress(multicastGroupIP, port);
   ip_mreq addRequest;

   sockaddr_in local_sockaddr_in = 
cast(sockaddr_in)(*localAddress.name);
   sockaddr_in multi_sockaddr_in = 
cast(sockaddr_in)(*multicastGroupAddr.name);

   addRequest.imr_multiaddr = multi_sockaddr_in.sin_addr;
   addRequest.imr_interface = local_sockaddr_in.sin_addr;

   auto optionValue = (cast(char*)&addRequest)[0.. ip_mreq.sizeof];
   socket.setOption(SocketOptionLevel.IP, 
cast(SocketOption)IP_ADD_MEMBERSHIP, optionValue);

Note that I had to import IP_ADD_MEMBERSHIP, and import 
sockaddr_in. This code gives me the error "Unable to set socket 
option: An unknown, invalid, or unsupported option or level was 
specified in a getsockopt or setsockopt call."

If I first bind the UdpSocket that is passed in, I instead get 
the error "Unable to bind socket: The requested address is not 
valid in its context."

I see suggestions to link to wsock32.lib in c++ forums rather 
than ws2_32.lib, but socket.d seems to link both? Any help 
diagnosing is appreciated.

-Evan Davis


More information about the Digitalmars-d mailing list