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

15,358

Solution 1

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.

Solution 2

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

Share:
15,358
chrisapotek
Author by

chrisapotek

I enjoy playing with technology and programming. Hope I can help you.

Updated on June 24, 2022

Comments

  • chrisapotek
    chrisapotek almost 2 years

    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?

  • user207421
    user207421 about 12 years
    Not my downvote, but you haven't answered the question. He didn't ask about sending and receiving, he asked about joining.
  • chrisapotek
    chrisapotek about 12 years
    Thanks EJP, but I still do not understand. Java's DatagramChannelImpl.joinGroup takes a SocketAddress (with port). So I guess you are saying that the multicast address (the IP) is also known as the multicast group, right? I would like to understand the differences and purposes of those three addresses: MulticastAddress, NetworkAddress and BindAddress.
  • chrisapotek
    chrisapotek about 12 years
    I was looking more to understand what role (what purpose) each of those three address play in this story.
  • chrisapotek
    chrisapotek about 12 years
    @EJB I opened two additional questions to complement this one. I really would like to understand this: Here are they: stackoverflow.com/questions/9468991/… and stackoverflow.com/questions/9469036/…
  • yves Baumes
    yves Baumes about 9 years
    Is it possible to specify 'any' port when binding?
  • user207421
    user207421 over 7 years
    @yvesBaumes Yes, just specify port zero. Hard to see the point. If you're client you don't even have to bind at all, and if you're a server you need to bind to a fixed port.