Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UDP: Listening to the same port for two different multicast streams

I need to listen to 2 different multicast groups using the same port. Program A will listen from 230.0.0.1 and Program B from 230.0.0.2. Both multicast groups use the same port 2000 and I have no control over it.

When I run my programs I receive both multicast streams in each program, that is both the data packets broadcasted on 230.0.0.1 and 230.0.0.2. I suspect the problem is due to the common port. This is the code I am using to subscribe to the multicast:

if( (sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0 ) {
  perror("socket");
  return -1;
}

if( setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0 ) {
  perror("setsockopt SO_REUSEADDR");
  return -1;
}

memset(&in_addr, 0, sizeof(in_addr));
in_addr.sin_family = AF_INET;
in_addr.sin_addr.s_addr = htonl(INADDR_ANY);
in_addr.sin_port = htons(2000);
if( bind(sd, (struct sockaddr*)&in_addr, sizeof(in_addr)) < 0 ) {
  perror("bind");
  return -1;
}

memset(&req, 0, sizeof(req));
inet_aton(intfc_ip, &req.imr_interface);
inet_aton("230.0.0.1", &req.imr_multiaddr);
if( setsockopt(sd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &req, sizeof(req)) < 0 ) {
  perror("setsockopt IP_ADD_MEMBERSHIP");
  return -1;
}

recv()...

How can I filter a specific multicast group in each program?

like image 469
Robert Kubrick Avatar asked Dec 12 '11 20:12

Robert Kubrick


People also ask

Can two processes listen on the same UDP port?

The short answer is “no, not on the same host."

What happens if two services use the same port?

Two applications at the same address cannot use the same port number. If you are configuring your system with multiple instances of TCP/IP on the same system, however, they will have different addresses and therefore the same port number can be used for the same function on each stack.

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).

Is UDP used for multicast?

Unicast uses TCP (Transmission Control Protocol) for communications while multicast communication uses UDP (User Datagram Protocol).


2 Answers

If you change

in_addr.sin_addr.s_addr = htonl(INADDR_ANY);

to

inet_aton(<your wanted IP address>, &in_addr.sin_addr.s_addr);

you could have more success.

(And if you change your program to work with getaddrinfo(), you make it future-proof.)

like image 114
glglgl Avatar answered Sep 30 '22 20:09

glglgl


"connect" might be what you need after all. Typically, for connecting TCP sockets, the man page also suggests that it can be used for filtering out UDP packets from other addresses:

From the man page posted here:

If the socket sockfd is of type SOCK_DGRAM then addr is the address to which datagrams are sent by default, and the only address from which datagrams are received.

like image 41
selbie Avatar answered Sep 30 '22 22:09

selbie