The bind operation basically says: “Use this local UDP port to send and receive data. In other words, it allocates this UDP port for exclusive use for your application. (The same is true for TCP sockets).
When you bind to "0.0.0.0" (INADDR_ANY), you basically speak at the TCP / IP level to use all available adapters to listen to and choose the best adapter to send. This is standard practice for most socket codes. The only time you do not specify 0 for the IP address is when you want to send / receive over a specific network adapter.
Similarly, if you specify the value of port 0 during binding, the OS will assign a randomly available port number to this socket. Therefore, I would expect UDP to multicast, you bind to INADDR_ANY at the specific port number on which multicast traffic is expected to be sent.
The join multicast group operation (IP_ADD_MEMBERSHIP) is necessary because it basically tells the network adapter not only to listen on Ethernet frames, where the destination MAC address is your own, it also reports that the ethernet adapter (NIC) is listening on IP multicast traffic, as well as for the corresponding multicast Ethernet address. Each multicast IP address corresponds to an Ethernet multicast address. When you use socket sending to a specific multicast IP address, the destination MAC address in the ethernet frame is set to the corresponding multicast MAC address for the multicast IP address. When you join a multicast group, you configure the network adapter to listen for traffic sent to the same MAC address (in addition to its own).
Without hardware support, multicasting will not be more efficient than regular broadcast IP messages. The connection operation also tells your router / gateway to forward multicast traffic from other networks. (Does anyone remember MBONE?)
If you join a multicast group, all network traffic for all ports on this IP address will be received by the NIC. Only traffic destined for your connected listening port will be transferred by the TCP / IP stack to your application. As to why the ports are indicated during multicast subscription - this is because the multicast IP address is only the IP address. "ports" is a property of the upper protocols (UDP and TCP).
You can learn more about how multicast IP addresses are mapped to Ethernet multicast addresses at different sites. The Wikipedia article is about as good:
IANA owns the OUI MAC address 01: 00: 5e, so multicast packets are delivered using the Ethernet MAC address range 01: 00: 5e: 00: 00: 00 - 01: 00: 5: 7f: ff: ff. This is 23 bits of available address space. The first octet (01) enables broadcast / multicast a bit. The lower 23 bits of the 28-bit multicast IP address are mapped to 23 bits of the available Ethernet address space.
selbie May 22 '12 at 4:00 2012-05-22 04:00
source share