How many processes can listen on a specific port?

6,167

Solution 1

Q: How many processes can listen on a specific port?

A: as many as you can spawn.

However for SOCK_STREAM sockets at least, and unless you use the SO_REUSEPORT option (new in Linux 3.9), a process cannot bind a socket to a local endpoint (address+port for TCP, filename for Unix...) if there's already another socket bound on that (or a listening one on that port with the wildcard address).

So unless you use SO_REUSEPORT, the only way to have different processes listen on the same port is to have their corresponding file descriptors pointing to the same open file description (to the same socket).

That happens automatically when you fork() a process. If fd 3 points to a listening TCP socket on the wildcard address and port 12345, both processes after the fork will be listening on that port on that fd (zsh syntax below):

$ zmodload zsh/net/tcp
$ ztcp -ld 3 12345
$ sleep 10 &
$ lsof -ni tcp:12345
COMMAND   PID     USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
zsh     26277 stephane    3u  IPv4 506354      0t0  TCP *:12345 (LISTEN)
sleep   26988 stephane    3u  IPv4 506354      0t0  TCP *:12345 (LISTEN)

For processes that are not related, the only way (that I know) for a process to get access to that listening socket would be to use the SCM_RIGHT mechanism to pass fds (actually more like open file descriptions) between processes using unix domain sockets.

You'll notice that one argument to listen() is the backlog.

As soon as there's a socket listening on given endpoint, the kernel will start accepting incoming connections to that endpoint (the backlog is a hint to the kernel as to how many it may accept that have not been accepted by applications).

Then the first process that does an accept() on any of the fds that point to the listening socket (or any of the listening sockets with SO_REUSEPORT) will get the incoming connection. accept() creates a new socket, and returns a new fd to that socket. In turn, that fd can be duplicated (a new fd pointing to the same socket) and children processes will inherit that connected socket like they would the listening one.

Solution 2

This answer discusses TCP on IPv4.

Only one process can listen for new connections. You'll get an "address already used" error if more than one processes attempt to claim the same port.

This is totally different from the amount of processes that are actively using that port.

Have a look at the following output:

remote          local        state
*:*           - 4.3.2.1:5000 LISTENING
1.2.3.4:12345 - 4.3.2.1:5000 CONNECTED
4.5.6.7:83247 - 4.3.2.1:5000 CONNECTED

What needs to be unique, is the 4-tuple (remote-ip, remote-port, local-ip, local-port). As the (remote-ip, remote-port) in the LISTENING state is a *:*, only one process can listen.

The listening application will start a new thread/task/process on each incoming connection.

Share:
6,167

Related videos on Youtube

faressoft
Author by

faressoft

Updated on September 18, 2022

Comments

  • faressoft
    faressoft over 1 year

    How many processes can listen on a specific port such as 80 ? and how the child processes of some application can use the same port to listen on ? Is there any difference between listening on a port or establishing a listening connection ?

  • pawel7318
    pawel7318 over 8 years
    You can bind to specified address only as well (rather than to *) and remember that IPv4 and IPv6 are separate stacks.
  • Stéphane Chazelas
    Stéphane Chazelas over 8 years
    You can have several processes listening on the same protocol:address:port as long as it's on file descriptors sharing the same open file descriptions (like children inheriting fds, or fds passed with SCM_RIGHTS).