Listening on an ephemeral TCP port
The classic TCP client/server interaction
When a TCP client calls connect(2), it usually gets assigned a port number from the ephemeral port range which is not used yet.
The server, on the other hand, usually calls bind(2) to set the port that it will listen on.
Some less known fun facts
Servers that listen on random ports
While a server will usually bind(2) itself to a fixed port number, it can absolutely omit that, and then, when it calls listen(2), the server will get an ephemeral port assigned by the kernel which it will then listen on. (This is similar to setting the bound port number to 0.)
Clients that use a specific port number
If a client socket uses bind(2) before connect(2), the client can enforce using a specific port number for its own side.
Reference
The best description I found of this is in ip(7):
An ephemeral port is allocated to a socket in the following circumstances:
- the port number in a socket address is specified as 0 when calling bind(2);
- listen(2) is called on a stream socket that was not previously bound;
- connect(2) was called on a socket that was not previously bound;
- sendto(2) is called on a datagram socket that was not previously bound.
So the behaviour of bind(2) actually works the same on both client and server sockets. It’s just in daily use where it only gets used for server sockets.
Previous discussion on the kernel mailing lists