Subscribing to multiple multicast groups on a single socket (Linux, C)

Can I get data from more than one multicast group on the same socket?

For instance:

void AddGroup(int sock, const char* mc_addr_str, int mc_port, const char* interface) { struct sockaddr_in mc_addr; memset(&mc_addr, 0, sizeof(mc_addr)); mc_addr.sin_family = AF_INET; mc_addr.sin_addr.s_addr = inet_addr(mc_addr_str); mc_addr.sin_port = htons(mc_port); if ((bind(sock, (struct sockaddr *) &mc_addr, sizeof(mc_addr))) < 0) { perror("bind() failed"); exit(1); } // construct an IGMP join request structure struct ip_mreq mc_req; mc_req.imr_multiaddr.s_addr = inet_addr(mc_addr_str); mc_req.imr_interface.s_addr = inet_addr(interface); if ((setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (void*) &mc_req, sizeof(mc_req))) < 0) { perror("setsockopt() failed"); exit(1); } } 

This code works when I add one multicast group. But when I try to add another, bind fails. I do not quite understand why the bunch should be there in the first place? (but the code does not work without it).

Ideally, I would like to call AddGroup several times on the same socket. Is it possible? Or do I need one socket per group and then just use polling?

+6
source share
6 answers

You can combine as many multicast groups as you want by using the appropriate setsockopt() call with the IP_ADD_MEMBERSHIP parameter rather than bind ().

+6
source

You only bind the socket once. Skip the binding a second time and see what happens.

+3
source

bind to the passive address, i.e. 0.0.0.0 for IPv4, and use ASM or SSM to attract additional groups, for example. IP_ADD_MEMBERSHIP as indicated.

You can only bind once.

+2
source

You can join the many multicast groups that you want to use on the same socket. See Setsockopt (), IP_PKTINFO, to find out which multicast group you are reading.

+2
source

Yes, it is possible: look at the example in the link ( http://www.tenouk.com/Module41c.html ) To shorten this in a few steps:

  • You install with SO_REUSEADDR
  • You are attached to INADDR_ANY
  • You install a device with IP_ADD_MEMBERSHIP for each group from which you want to receive a datagram.
  • It seems to me that using IP_PKTINFO makes it possible to distinguish between received packets, but the sender should take care of their preparation ( Setting the source IP address for a UDP socket )
0
source

I do not believe that it is possible to combine several multicast groups on a given socket. The socket is determined by pairing the ip / port source and the destination IP address. The operating system does not know what to do with the data coming from the second ip / port, because it will not have a socket to send it.

Linking is required to register a connection to the operating system. This is basically like registering a file descriptor.

The best way to do this is to create a socket for each multicast group, and then call select (). select () will tell you if any socket has data ready to be read.

Check your man pages for more information on sockets (), bind (), and select ().

-3
source

Source: https://habr.com/ru/post/908224/


All Articles