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 ?
Q: How many processes can listen on a specific port?
A: as many as you can spawn.
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.
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 *:* - 22.214.171.124:5000 LISTENING 126.96.36.199:12345 - 188.8.131.52:5000 CONNECTED 184.108.40.206:83247 - 220.127.116.11: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.