I'm trying to use a socket to receive multicast packets from a specific multicast group sent to a specific port and would like to clarify the correct address to use for bind()
. The man pages typically discuss bind()
in the context of unicast but not so much for the special case of multicast.
I understand that because multiple processes can listen on the same port for multicast, the use of SO_REUSEADDR
is usually prescribed. For bind()
, I think it's clear that the port to use is the destination port. However, what's not so clear is the address. I see in multiple examples the use of INADDR_ANY
for bind. (e.g. http://ift.tt/1aUfY8V & http://ift.tt/1LWOxwq)
However, my experience has been that this way of calling bind()
results in receiving unwanted packets, but if I use the multicast group address for bind(), the problem goes away. I have another process concurrently listening to other multicast groups on the same port and I think the current process gets them too because of the indiscriminate use of INADDR_ANY
.
So, I would like to confirm whether the multicast group address is the correct address to bind()
in my case, like this:
// Don't do this: // bindaddr.sin_addr.s_addr = htonl(INADDR_ANY); // Do this instead: bindaddr.sin_addr = mcastGroupAddr; // Then bind(). if (bind(sockFd, reinterpret_cast<sockaddr*>(&bindaddr), sizeof(bindaddr))) return -1;
If this is not the correct address to bind()
, what should I use?
No comments:
Post a Comment