chrisapotek
chrisapotek

Reputation: 6227

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:

Where and what is the multicast group here?

Upvotes: 8

Views: 20043

Answers (2)

user207421
user207421

Reputation: 310883

A multicast group is a special IP address. You join it via setsockopt() using the socket option IP_ADD_MEMBERSHIP, 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.

Upvotes: 8

ribram
ribram

Reputation: 2450

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

Upvotes: 1

Related Questions