4

I have two processes. both listen to the same port.
This is UDP multicast so both use the socket option SO_REUSEADDR, and join the multicast group prior to bind().

When I send a message to the multicast group ip:port only one of the processes gets the message.
How can both of them get it?

Thanks.

hudac
  • 2,584
  • 6
  • 34
  • 57
  • It's certainly possible. I have working java code that does just that. Better have a look at your source code... – Andy Brown Jun 11 '15 at 15:26
  • I don't think this works. When a packet is received at the UDP layer it checks its internal list of tcb's. As soon as it finds a match , the packet is lifted and copied by the socket layer from network context to user context. Multicast is intented to send to several ip addresses. Internally in udp there is a function find_tcb and it returns a single one, not multiple ones. SO_REUSEADDR is for different purposes. – Philip Stuyck Jun 11 '15 at 17:08
  • So what is the point of enabling multiple bind()s, specifically on udp multicast? – hudac Jun 11 '15 at 18:30
  • You tell me, you are the one that is trying to do this. Multicast or broadcast is one source sending to multiple ip addresses. Unless those 2 processes are using different ip addresses. – Philip Stuyck Jun 11 '15 at 18:33
  • You should be using SO_REUSEPORT if you have it. – user207421 Jun 11 '15 at 22:05
  • Looks like for multicast, REUSEPORT equals to REUSEADDR (http://stackoverflow.com/questions/14388706/socket-options-so-reuseaddr-and-so-reuseport-how-do-they-differ-do-they-mean-t/14388707#14388707). I want to send simple multicast message. The only reason I want to do multiple bind(), is to debug the processes which send this multicast message on the same machine, and not on different ones. So I nees all the processes to bind() on the same port. Unless you tell me that if a process is added to a multicast group, it gets all of its messages, doesn't matter which port it's bounded to. – hudac Jun 11 '15 at 22:39
  • I checked the bsd sources of UDP and indeed special processing is there for multicast : http://svnweb.freebsd.org/base/head/sys/netinet/udp_usrreq.c?revision=277331&view=markup line 520. So it should work and the SO_REUSEADDR or PORT should be used : line 597 – Philip Stuyck Jun 12 '15 at 04:37
  • @hudac Your citation says exactly the opposite: '`SO_REUSEPORT` does not imply `SO_REUSEADDR`'. They are different. – user207421 Jun 12 '15 at 05:42
  • @PhilipStuyck That's only one of several places where it could matter, and there is nothing in the question about FreeBSD. I don't know what line 520 has to do with it. Did you mean the comment at line 590? – user207421 Jun 12 '15 at 05:44
  • @EJP I am correcting my earlier comments that it would not work. I looked in my own source code of udp and based on that it does not work. But it is not because our udp does not support multicast that this means it is not supported at all. Hence I looked in the source of freebsd and I clearly see multicast support in there. Line 520 shows a foreach and not the single entry returning find_tcb that I mentioned earlier. (freebsd is just a representative implementation of TCP/IP) – Philip Stuyck Jun 12 '15 at 08:24
  • @EJP it says that they are equal in multicast: "Actually the code treats SO_REUSEADDR and SO_REUSEPORT identically for multicast addresses". But later it says, in the `Linux` section: 1. "both processes must have the same `EUID`". 2. "For UDP sockets, it tries to distribute datagrams evenly" - means that it does some kind of load balancing, and not all bounded sockets receive the datagram? – hudac Jun 12 '15 at 09:45
  • I just tried this on RedHat 5 running the 2.6.18 kernel, and was successfully able to receive a multicast messages on two separate sockets in two separate instances of the program. What OS/kernel are you running? Also, can you post your receiver code? – dbush Jul 07 '15 at 21:06
  • I don't know what to say and what was the problem.. it works for me also :O :O :O – hudac Jul 09 '15 at 05:09

2 Answers2

1

Two process can listen at the same port. Although two sockets for connections can't be bound to the same IP address with same port.

You just add an entry for each process in your hosts file that points to a unique ip address.

Something like : You bind process P1 to 127.0.0.1 port 8080 and P2 to 127.0.0.2 port 8080.

adrianmcmenamin
  • 1,081
  • 1
  • 15
  • 44
AMS
  • 29
  • 4
-2

I don't think it's possible, and I think that sometimes it is a lot more coherent this way. Maybe you should implement a receiver (which will listen to the port) which will distribute the messages to the other processes via inter-processes communication (pipes for instance).

Raspbeguy
  • 127
  • 4