Sub topic 1
What a Port Number Identifies
Prerequisites
This chapter assumes you already know the following:
- What an IP address is and what a host is.
- The client-server model: a server waits for requests and a client starts them.
- How to run commands in a terminal.
- How to read a short Python script.
You do not need to know the details of TCP or UDP yet. A later chapter covers them.
The problem a port solves
An IP address identifies a network interface on a host. A single host often runs many network programs at once, such as a web server, an SSH daemon and a database. When a packet arrives at the host's IP address, the operating system must decide which program gets the data.
A port is a 16-bit unsigned integer in the transport-layer header. It tells the receiving operating system which endpoint on the host the data belongs to. The valid values run from 0 to 65535, which is 2^16 values.
The IP address finds the host. The port finds the program on that host. This sorting step is called demultiplexing.
What a port does not identify
A port number does not identify a protocol in any enforced way. It is a convention. Port 80 is where web servers conventionally listen for HTTP, but nothing in the network stops you from running SSH on port 80 or a web server on port 22. The last chapter of this tutorial returns to this point.
A port also does not identify a machine or a process permanently. It identifies an endpoint that one process has asked the operating system to bind to. When the process exits, the port becomes free again.
Sockets: IP address, port and protocol
An endpoint is described by three values:
- The IP address.
- The port number.
- The transport protocol, usually TCP or UDP.
Together these form a socket address. People often write it as 192.0.2.10:443. The protocol is usually left out of that notation, but it is part of the identity.
TCP ports and UDP ports are separate namespaces. TCP port 53 and UDP port 53 are different endpoints. One program can listen on TCP 53 while another listens on UDP 53, and neither conflicts with the other. DNS uses both, as a later chapter shows.
Connections are identified by five values
A server listening on port 443 can talk to thousands of clients at once, all using the same server port. The operating system keeps the conversations apart by using the full 5-tuple:
| Field | Example |
|---|---|
| Protocol | TCP |
| Source IP | 198.51.100.7 |
| Source port | 51822 |
| Destination IP | 192.0.2.10 |
| Destination port | 443 |
Two connections differ if any of the five values differ. A second connection from the same client to the same server uses a different source port, for example 51823. That source port is chosen by the client's operating system. It is called an ephemeral port, and the chapter on sockets covers it in detail.
Server ports and client ports
The two ends of a connection play different roles.
- A server binds to a chosen port and listens. Clients must know that port in advance, which is why default port numbers exist.
- A client does not choose its own port in the usual case. The operating system assigns one automatically when the client connects.
This asymmetry is why the numbers you memorize, such as 22 or 443, are almost always server ports.
Looking up names and numbers
Operating systems keep a local database that maps service names to port numbers. On Linux and macOS it is the file /etc/services. On Windows it is C:\Windows\System32\drivers\etc\services. Python exposes the lookup through the socket module:
import socket
for name in ("http", "https", "ssh", "smtp", "domain"):
print(name, socket.getservbyname(name, "tcp"))
print(socket.getservbyport(443, "tcp"))The name for DNS in this database is domain, not dns. Names come from the registry's service names, which do not always match what people say aloud. If a name is missing from the local file, getservbyname raises OSError. The file is a convenience copy and may be shorter than the full IANA registry.
Seeing both ends of a real connection
The next script opens a connection to a web server and prints both socket addresses:
import socket
with socket.create_connection(("example.com", 80), timeout=5) as s:
print("local :", s.getsockname())
print("remote:", s.getpeername())The remote address ends in port 80, which is the number the script asked for. The local address ends in a high, seemingly random port. You did not choose it. The operating system picked it for this connection. Run the script twice and the local port will usually differ.
Common errors
Most beginner problems with ports fall into a few patterns.
- Address already in use. Only one socket can bind to a given IP address, port and protocol at a time. Starting a second server on the same port fails with this error. Find the first process with
ss -tulnp, which a later chapter covers. - Connection refused. The host is reachable, but nothing is listening on that port. The host's operating system answers with a rejection straight away.
- Timeout. No answer comes back at all. A firewall that silently drops packets is a common cause, as is a host that is down or unreachable.
- Ports below 1024 need privileges. On Unix-like systems, binding to a low port normally requires root or a specific capability. The next chapter explains why.
- Wrong protocol. A UDP service does not answer TCP connection attempts, and the reverse is also true.
Port 0 and the upper limit
Port 0 is reserved. A program that binds to port 0 is asking the operating system to pick any free port. This is how many test programs and short-lived servers get an unused port without guessing. Applications should not send traffic with port 0 as a real destination.
Because the field is 16 bits, there are 65,536 values per protocol per IP address. That is also the ceiling on how many simultaneous connections one client IP can make to one server IP and port. Every connection needs a distinct source port, so heavy clients such as proxies and load balancers can run out. That condition is called ephemeral port exhaustion.
Summary of the working model
- A port is a 16-bit number that lets the operating system deliver data to the right program.
- A socket address is an IP address, a port and a transport protocol.
- A connection is identified by the 5-tuple, so one server port can serve many clients.
- Servers use well-known numbers. Clients get temporary numbers from the operating system.
- Port numbers are conventions, not guarantees about the protocol behind them.
Exercise
A web server at 192.0.2.10 listens on TCP port 443. Two browser tabs on the same laptop (198.51.100.7) are connected to it at the same time. How does the server's operating system keep the two connections apart, and could a DNS server also listen on UDP port 443 on that host?
Show answer
The connections differ in their source port. Each tab's connection has the same protocol, source IP, destination IP and destination port, but the laptop's operating system gave each one a different ephemeral source port, so the 5-tuples are different. A different program could listen on UDP port 443 on the same host, because TCP and UDP port numbers are separate namespaces and the full socket address includes the protocol. (Whether it would be a DNS server is up to its configuration, since the port number does not enforce the protocol.)
Exercise
You run a script that connects to a server on port 8080. One run fails instantly with "connection refused". Another run against a different server hangs for the full timeout. What does each result suggest?
Show answer
"Connection refused" suggests the host is reachable but nothing is listening on port 8080, because the host answered with a rejection. The hang suggests packets are being dropped or never answered, for example by a firewall that silently discards them, or the host or route being down.