Thread overview
Winsock Multicast setOption error
Feb 04, 2014
Evan Davis
Feb 04, 2014
Vladimir Panteleev
Feb 04, 2014
Evan Davis
February 04, 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
February 04, 2014
On Tuesday, 4 February 2014 at 01:47:11 UTC, Evan Davis wrote:
> 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.

You need to use the definition of IP_ADD_MEMBERSHIP from the WinSock2 header files (=12, not =5).

The problem is explained here:
http://support.microsoft.com/kb/257460
February 04, 2014
On Tuesday, 4 February 2014 at 09:02:35 UTC, Vladimir Panteleev wrote:
> You need to use the definition of IP_ADD_MEMBERSHIP from the WinSock2 header files (=12, not =5).
>
> The problem is explained here:
> http://support.microsoft.com/kb/257460

Thanks very much for a solution. Worked great.

-Evan Davis