Start With the Problem#
The previous file got a packet from your laptop to StudyBuddy's server at 13.235.10.20. But:
That server runs several programs that all use the network: the StudyBuddy web server, a database, a remote-login service, a monitoring agent. The packet arrived at the right building. Which apartment is it for?
The internet is best-effort: packets get lost, duplicated and reordered. Priya's notes are 5 MB — about 3,500 packets. If packet 1,207 is lost, the file is corrupted. Who notices, and who resends?
If your laptop sends faster than the server can read — or faster than the cables in between can carry — who slows it down?
Your home has one public IP address, but your laptop, phone and TV all browse at the same time using private addresses (Networking 1: Addresses and Routing Ch 6). How do the replies find the right device?
This file answers all four: ports, TCP and UDP, and NAT.
The Plain Picture#
A letter addressed to a big apartment building needs two pieces of information: the building's street address, and the flat number. The building's address gets it to the right building; the flat number gets it to the right family inside.
The Real Names#
The IP address is the building (Networking 1: Addresses and Routing Ch 4).
A port is the flat number: a 16-bit number (How Computers Work Ch 2: 2¹⁶ = 65,536 values, 0–65535) that identifies which program on a computer the data is for.
A program that waits for others to contact it — a web server, a database — listens on a fixed port.
An address plus a port is written
13.235.10.20:443.
Under the Hood#
Well-known ports — memorise these#
| Port | Used by | You'll meet it in |
|---|---|---|
| 22 | SSH (remote login) | The Linux Command Line |
| 53 | DNS | Networking 3: DNS, HTTP and HTTPS |
| 80 | HTTP | Networking 3: DNS, HTTP and HTTPS |
| 443 | HTTPS | Networking 3: DNS, HTTP and HTTPS |
| 5432 | PostgreSQL | Databases (coming soon) |
| 3306 | MySQL | Databases (coming soon) |
| 6379 | Redis | Redis (coming soon) |
| 27017 | MongoDB | MongoDB (coming soon) |
| 8000 | FastAPI / uvicorn during development | Backend with Python (coming soon) |
| 3000, 5173 | React / Next.js / Vite dev servers | Frontend (coming soon) |
Ports 0–1023 are "well-known"; on Linux only the administrator (root) may listen on them. That's why development servers use ports like 8000 and 3000.
These are conventions, not laws. You can run a web server on port 9999; the client just has to know.
Ephemeral ports — the client's side#
When StudyBuddy (the client) connects to the server's port 443, the client needs a port number too, so replies can find it. The kernel picks a random free one from a range reserved for this — an ephemeral port (short-lived port). Linux uses 32768–60999 by default; macOS uses 49152–65535.
The five-tuple — what identifies one connection#
Every connection is uniquely identified by five values:
( protocol, source IP, source port, destination IP, destination port )
( TCP, 192.168.1.37, 52341, 13.235.10.20, 443 )This is why a server can handle thousands of clients on the same port 443: every client connection has a different source IP or source port, so every five-tuple is different.
Check yourself
4 questionsWhat does a port identify?
Show answerHide answer
Which program on a computer the data is for.
How many possible ports are there, and why?
Show answerHide answer
65,536 (0–65535), because a port is a 16-bit number.
Which ports do HTTPS, SSH and PostgreSQL use by default?
Show answerHide answer
443, 22, 5432.
How can one server accept 10,000 connections all on port 443?
Show answerHide answer
Each connection has a different five-tuple — the clients' IPs and ephemeral ports differ.