Sub topic 2
Ports, sockets and multiplexing
This chapter assumes you know the layered model (application, transport, network, link), what an IP address and a packet are, and basic Python. IP delivers a packet to a host. The transport layer must then deliver the data to the right program on that host. Ports and sockets make that possible.
Why an IP address is not enough
A host usually runs many networked programs at once: a browser, a mail client, a chat app, a DNS resolver. All of them share one IP address. When a packet arrives, the operating system needs a second piece of addressing to decide which program receives it.
That second address is the port. A port is a 16-bit number from 0 to 65535 carried in the TCP or UDP header. The pair of an IP address and a port identifies one communication endpoint, such as 192.0.2.10:443.
Port ranges
| Range | Name | Typical use |
|---|---|---|
| 0–1023 | Well-known ports | Standard services such as HTTP (80), HTTPS (443), SSH (22), DNS (53). Binding usually needs elevated privileges. |
| 1024–49151 | Registered ports | Applications registered with IANA, such as PostgreSQL (5432). |
| 49152–65535 | Ephemeral (dynamic) ports | Chosen by the OS for the client side of outgoing connections. Some systems use a different range, for example Linux defaults to 32768–60999. |
A server listens on a well-known port so clients can find it. A client does not need a predictable port, so the OS picks a free ephemeral port for it.
What a socket is
A socket is the operating system object that an application uses to send and receive data through the network. You create it with the sockets API, then bind it to a local address and port, or let the OS pick one when you connect.
A socket is not the same thing as a port. A port is a number in a header. A socket is a kernel object that is associated with a protocol, a local address and port, and, for a connected socket, a remote address and port.
Multiplexing and demultiplexing
Multiplexing means the sender side gathers data from many sockets and puts each piece into a segment or datagram tagged with source and destination ports. Demultiplexing means the receiver side reads those port numbers and hands the data to the correct socket.
How the receiver chooses the socket depends on the protocol:
- UDP uses the destination IP address and destination port. Datagrams from different senders to the same port arrive at the same socket.
- TCP uses the full four-tuple: source IP, source port, destination IP, destination port. Each connection has its own socket.
This is why a web server can hold thousands of connections to port 443 at once. They all share the destination port, but each has a different client address or client port, so each four-tuple is unique.
Seeing the ports in code
The following program connects to a local server and prints both endpoints. Start the echo server from the later socket chapter first, or any TCP server listening on port 9000.
import socket
with socket.create_connection(("127.0.0.1", 9000)) as s:
print("local endpoint: ", s.getsockname())
print("remote endpoint:", s.getpeername())The remote endpoint is ('127.0.0.1', 9000). The local port is a different number chosen by the OS from the ephemeral range, and it changes between runs.
Common errors
- Address already in use. A port can be bound by only one socket for a given address and protocol. Starting a second server on the same port fails. After a server exits, the old connections may linger in the TIMEWAIT state, so servers often set `SOREUSEADDR` before binding.
- Permission denied. Binding to a port below 1024 usually requires administrator rights on Unix-like systems.
- Confusing TCP and UDP ports. TCP port 53 and UDP port 53 are separate. A TCP socket and a UDP socket can bind the same number at the same time.
- Binding to the wrong address. Binding to
127.0.0.1accepts only local connections. Binding to0.0.0.0accepts connections on every IPv4 interface.
Exercise
A server listens on TCP port 8080. Two clients on the same machine, with source ports 40001 and 40002, connect to it. How many sockets does the server have, and how does the OS decide which one receives an incoming segment?
Show answer
Three sockets: one listening socket on port 8080 and one connected socket per client. The OS matches each incoming segment by its four-tuple (source IP, source port, destination IP, destination port), so segments from port 40001 go to the first connected socket and segments from port 40002 go to the second.
Exercise
Why can a UDP server use one socket to receive datagrams from many clients, while a TCP server needs a separate socket for each client?
Show answer
UDP demultiplexes on the destination address and port only and has no connection state, so every datagram to that port reaches the same socket, and recvfrom reports each sender. TCP keeps per-connection state (sequence numbers, windows), so each connection gets its own socket, identified by the four-tuple.