Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Let two UDP-servers listen on the same port?

I have a client which sends data via UDP-broadcast. (To let's say 127.0.0.255:12345)

Now I want to have multiple servers listening to this data. To do so on a local machine, they need to share the port 12345 for listening.

My question is, if that is possible, if there are any disadvantages and if there could be problems with this approach.

There is one alternative which unfortunately brings with a lot of overhead:
Implement some kind of registration-process. On startup, each server tells the client its port. The client then sends the messages to each port (having to send the data multiple times, some kind of handshaking needs to be implemented...)
Do you know any better alternative?

If that matters:
I'm using C++ with Boost::Asio. The software should be portable (mainly Linux and Windows).

like image 950
MOnsDaR Avatar asked Dec 06 '10 08:12

MOnsDaR


People also ask

Can two processes listen on the same UDP port?

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

Can multiple services listen on same port?

It is possible. You just have to bind on the right IP address/interface each service using the same port. Ports (be them UDP or TCP) have their own pool per IP address. You can listen on the same port if you change: IP address or protocol (UDP or TCP).

Can 2 applications use the same port?

Yes, different applications can bind to the same port on different transport protocols. They can also open the same port on the same protocol but different IP addresses.

Can UDP ports listen?

As mentioned in the comments, UDP is connectionless. Unlike with TCP, it has no concept of "listening", "established", "closed", or anything like that. If a UDP port is open, it appears in the listing; if it's not open, it doesn't. There is no other state to display.


2 Answers

You will have to bind the socket in both processes with the SO_REUSEPORT option. If you don't specify this option in the first process, binding in the second will fail. Likewise, if you specify this option in the first but not the second, binding in the second will fail. This option effectively specifies both a request ("I want to bind to this port even if it's already bound by another process") and a permission ("other processes may bind to this port too").

See section 4.12 of this document for more information.

like image 177
cdhowie Avatar answered Sep 27 '22 22:09

cdhowie


This answer is referenced to the answer of cdhowie, who linked a document which states that SO_REUSEPORT would have the effect I'm trying to achieve.

I've researched how and if this option is implemented and focused mainly on Boost::Asio and Linux.

Boost::Asio does only set this option if the OS is equal to BSD or MacOSX. The code for that is contained in the file boost/asio/detail/reactive_socket_service.hpp (Boost Version 1.40, in newer versions, the code has been moved into other files).
I've wondered why Asio does not define this option for platforms like Linux and Windows.

There are several references discussing that this is not implemented in Linux: https://web.archive.org/web/20120315052906/http://kerneltrap.org/mailarchive/linux-netdev/2008/8/7/2851754
http://kerneltrap.org/mailarchive/linux-kernel/2010/6/23/4586155

There also is a patch which should add this functionality to the kernel: https://web-beta.archive.org/web/20110807043058/http://kerneltrap.org/mailarchive/linux-netdev/2010/4/19/6274993

I don't know if this option is existing for Windows, but by defining portable as an attribute for software which runs on Linux too, this means, that SO_REUSEPORT is OS specific and there is no portable solution for my question.

In one of the discussions I've linked it is recommended for UDP to implement a master-listener which then provides the incoming data to multiple slave-listeners.

I will mark this answer as accepted (though feeling kind of bad by accepting my own answer), because it points out why the approach of using SO_REUSEPORT will fail when trying to use it with portable software.

like image 36
MOnsDaR Avatar answered Sep 27 '22 20:09

MOnsDaR