Sub topic 4
Sockets and ephemeral client ports
This chapter assumes you know what a port number identifies, the three port ranges, and the difference between TCP and UDP. It explains how a connection is named and why clients use temporary ports.
What a socket is
A socket endpoint is the combination of an IP address, a port number and a transport protocol. For example, 93.184.216.34:80 over TCP is one endpoint. A TCP connection is identified by a pair of endpoints, so it has four values: source IP, source port, destination IP and destination port. This is called the 4-tuple, and together with the protocol it is the 5-tuple.
The 4-tuple is why one server port can serve thousands of clients at once. Every connection to port 443 shares the server's IP and port, but each client has a different IP address or source port, so each connection stays distinct.
Ephemeral client ports
A server listens on a fixed, well-known port so that clients can find it. A client has no such need. When a client program connects, the operating system picks a free source port for it. This is called an ephemeral port, and it is released when the connection ends.
The IANA suggests the dynamic range 49152–65535 for this purpose. Operating systems differ. Linux defaults to 32768–60999, which you can read from /proc/sys/net/ipv4/ip_local_port_range. Windows and recent macOS versions use the IANA range.
Seeing both endpoints
The following script connects to a web server and prints both endpoints.
import socket
with socket.create_connection(("example.com", 80), timeout=5) as s:
print("local :", s.getsockname())
print("remote:", s.getpeername())The remote endpoint always ends in port 80. The local port changes on each run, because the operating system chooses it. Run the script several times to see this.
Common errors
- Assuming the client port matters. Client ports are random, so firewall rules and logs should not depend on a specific one. Rules normally match the destination port on inbound traffic.
- Running out of ephemeral ports. Each outgoing connection to the same destination IP and port needs a distinct source port. A busy client or proxy making many short connections can exhaust the range, and new connections then fail with errors such as
EADDRNOTAVAIL. Closed connections also linger in theTIME_WAITstate for a while, which holds ports longer than you might expect. - Confusing a listening socket with a connection. A server's listening socket waits on one port. Each accepted connection is a new socket with its own 4-tuple, while the listening socket keeps waiting.
- Binding a server to a port in use. Starting a second server on the same address and port fails with "Address already in use".
Checking it yourself
Run ss -tn on Linux while a browser is open. The local column shows high ephemeral ports, and the peer column shows ports such as 443.
Exercise
A web server at 192.0.2.10 listens on port 443. One client at 198.51.100.7 opens two simultaneous connections to it. What differs between the two connections' 4-tuples, and why can the server tell them apart?
Show answer
Only the client's source port differs, because the operating system gives each connection its own ephemeral port. The server IP, server port and client IP are the same. Since the full 4-tuple is different, the server treats them as two separate connections.
Exercise
Why do firewall rules for a web server usually specify port 443 but not the client's source port?
Show answer
The client's source port is an ephemeral port chosen at random for each connection, so it cannot be predicted. The destination port, 443, is the fixed value that identifies the service.