UDP multicast using winsock API differences between XP and Vista

c++, sockets, windows, windows-vista, winsock

Solution

What error are you getting from the `setsockopt()` call that you make to apply `IP_ADD_MEMBERSHIP` and join the multicast group?

I've just run some tests here with my server framework and I note that I DO call `bind()` on Windows 7 (I don't have a Vista box to hand) and I can then also join a multicast group as expected as long as both the binding address and the multicast address are valid.

However I cannot call bind() with `INADDR_ANY`, as that causes the joining of the multicast group to fail with error 10022 (`WSAEINVAL`) and if the multicast group address isn't a valid multicast address the call fails with error 10049 (`WSAEADDRNOTAVAIL`). Which all seems fair enough.

I'm booting an XP box now...

[Edited after testing on XP]

On XP it's legal to `bind()` to `INADDR_ANY` and it's legal to use invalid multicast addresses. So, I expect, you're doing one of those and it therefore works on XP and not on Vista/Win7.

So, what error are you getting and what address are you binding to?

Problem

It seems to be that the implementation required to set up a UDP multicast socket has changed between windows XP and windows vista. Specifically: Under windows XP, you must call `bind()` before you can reference any multicast-specific socket options. However, under windows vista, you must not call `bind()` when dealing with multicast sockets. Failing either of these two points results in a socket error at runtime. Am I going crazy, or have other people seen this as well? Is there a better solution than conditional compilation of the bind() call?

Original source