Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I need a PORT when joining a multicast group or just the IP?

I would like to learn that once and for all. What is the procedure to connect a multicast socket? I know you have to bind to a local interface (do you need IP and port for that?) then I know you have to join a group (do you need IP:PORT for the address you want to join and the network interface again!!!??) and then finally you can leave the group.

Can someone with experience clarify what is the whole of those many addresses? I will list below:

  • BindAddress (IP:PORT)
  • NetworkAddress (IP:PORT)
  • MulticastAddress (IP:PORT)

Where and what is the multicast group here?

like image 988
chrisapotek Avatar asked Feb 24 '12 00:02

chrisapotek


People also ask

Does port matter for multicast?

Not really ... just like unicast, it is the combination of the address and the port that matters. But you would do well to use different multicast IP addresses for different application because switches will distribute multicast packets according to the IP address (regardless of port).

How do you join a multicast group?

To join a multicast group, a host sends a join message, using the Internet Group Management Protocol (IGMP), to its first-hop router. Groups are identified by a single class D IP address (in the range 224.0. 0.0 to 239.255. 255.255).

Do I need to join multicast group to send?

A host can send datagrams to a multicast group address even though there are no members of that group, and a host doesn't need to be a member of a group to send multicast datagrams to that group.

What does it mean to join a multicast group?

A multicast group is defined by an IP address and a port number. Once a host has group membership, the host will receive any data packets that are sent to that group defined by an IP address/port number.


2 Answers

A multicast group is a special IP address. You join it via setsockopt() using the socket option IP_ADDMEMBERSHIP, or e.g. in Java via MulticastSocket.joinGroup(). No port number here. If you want to join via a specific local address, use the overload that specifies a local address, or call setNetworkInterface() first.

Binding to a local address is a separate operation, which primarily determines which local addresses the socket can send and receive data on: one, or all of them: either one local address, which determines which of your available subnets you are listening to and can send via, or a port, or both. Normally it is best to use INADDR_ANY as the bind-address, unless your application magically knows about the network topology.

This is muddied by the fact that you can bind to a multicast address in Linux, but this appears to be a misunderstanding that will now always be with us.

You send to a multicast group by sending to the multicast address.

like image 133
user207421 Avatar answered Sep 21 '22 23:09

user207421


Yes, you must define both the address and the port for sending/receiving multicast messages. These are UDP packets, so they require both address and port for the networking stack to be able to properly deliver the messages to participating processes. So to listen for a particular set of multicast messages, your application needs to bind to a particular multicast ip address and port combination (and obviously for a set or all interfaces on the machine). The group is defined by the address/port combination.

Good quick explanation

Some sample source code in C and other languages

like image 35
ribram Avatar answered Sep 18 '22 23:09

ribram