ConceptDifficulty: Beginner2 min

The Transport Layer: From One Host to One Program

An IP address reaches a machine, and a port number reaches the program waiting on it.

The transport layer, layer 4, delivers data from a program on one machine to a program on another. The network layer stops at the machine: an IP address names a host, not the web server, database or mail server running on it. The transport layer adds a port number, a 16-bit number that names one program's endpoint on that host, so the operating system knows which program each arriving packet is for.

Picture a server with one IP address, 203.0.113.10, running a web server, an SSH server for remote logins and a PostgreSQL database. Packets for all three arrive at the same address. The port number in each one decides where it goes.

One host, one IP address, three programs told apart by port
ProgramTransportPortReached at
Web serverTCP443203.0.113.10, port 443
SSH serverTCP22203.0.113.10, port 22
PostgreSQLTCP5432203.0.113.10, port 5432

Think of the IP address as a building and the port as a flat number inside it: the postal service gets a letter to the building, and the flat number gets it to one household. The comparison breaks in one place. A flat holds one household, but one server port, such as 443, can hold thousands of connections at once, which the next ideas explain.

A program claims a port by listening on it: it asks the operating system to hand it everything that arrives for that number. Only one program can listen on a given port and transport at a time, which is why starting a second web server on port 443 fails with "address already in use".

The internet has two transports in wide use, and they differ in one question: what happens when a packet is lost. TCP (Transmission Control Protocol) notices, sends it again and delivers everything in order, so the program receives a complete, ordered stream. UDP (User Datagram Protocol) does nothing; each packet arrives or it does not, and the program deals with the gap.

Ports belong to a transport, so TCP port 53 and UDP port 53 are different endpoints, and a program can listen on both.

So when data reaches the right machine but the wrong program, or no program, the question is at layer 4: which port, and which transport.