Skip to content

Try “polymorphism”, “TCP handshake” or “git rebase”.

    Computer Basics / Networking 2: TCP, UDP and Ports

    Ports

    The IP address is the building (03networking1addresses.md Ch 4).

    Chapter 1 of 125 min read

    Start With the Problem#

    The previous file got a packet from your laptop to StudyBuddy's server at 13.235.10.20. But:

    1. 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?

    2. 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?

    3. If your laptop sends faster than the server can read — or faster than the cables in between can carry — who slows it down?

    4. 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#

    PortUsed byYou'll meet it in
    22SSH (remote login)The Linux Command Line
    53DNSNetworking 3: DNS, HTTP and HTTPS
    80HTTPNetworking 3: DNS, HTTP and HTTPS
    443HTTPSNetworking 3: DNS, HTTP and HTTPS
    5432PostgreSQLDatabases (coming soon)
    3306MySQLDatabases (coming soon)
    6379RedisRedis (coming soon)
    27017MongoDBMongoDB (coming soon)
    8000FastAPI / uvicorn during developmentBackend with Python (coming soon)
    3000, 5173React / Next.js / Vite dev serversFrontend (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:

    Output
      ( 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 questions
    1

    What does a port identify?

    Show answer

    Which program on a computer the data is for.

    2

    How many possible ports are there, and why?

    Show answer

    65,536 (0–65535), because a port is a 16-bit number.

    3

    Which ports do HTTPS, SSH and PostgreSQL use by default?

    Show answer

    443, 22, 5432.

    4

    How can one server accept 10,000 connections all on port 443?

    Show answer

    Each connection has a different five-tuple — the clients' IPs and ephemeral ports differ.