Soapbox of Destiny

$ cat /dev/brain > here

0 notes

WSAENOPROTOOPT when attempting to join a multicast group

I’ve been writing a static library that uses multicast for work over the past few days. Today I wanted to statically link in the winsock calls to my library so that the user themselves didn’t have to include winsock.

After specifying Ws2_32.lib as a dependancy my program started failing when it tried to join a multicast group. I narrowed it down to the following call:

int joinResult = ::setsockopt( nativeSocket,
                               IPPROTO_IP,
                               IP_ADD_MEMBERSHIP,
                               (char*)&joinRequest,
                               sizeof(joinRequest );

The call was returning with SOCKET_ERROR, and a call to ::WSAGetLastError() returned WSAENOPROTOOPT (Bad protocol option)

After a lot of soul searching and trawling through code I found that the issue occurs when you explicitly specify to link ws2_32.lib. By default it seems that if the dependency is not set, Windows will choose to use winsock.lib instead, which performs the join operation without any issues.

The resolution was to instead specify winsock.lib as a link dependency rather than Ws2_32.lib. After this change, I’m happy to report that everything is now working fine again.

Filed under c++ windows winsock programming