Course outline · 0% complete

0/25 lessons0%

Course overview →

Ports: many doors on one machine

lesson 2-2 · ~9 min · 4/25

One address, many programs

An IP address gets a message to the right machine, but one machine runs many network programs at once: a web server, a database, an SSH login service. A port is a number from 1 to 65535 that picks which program on that machine should receive the message. Address plus port is written 93.184.215.14:443.

Well-known services have standard ports, which is why you rarely type them:

PortService
22SSH (remote terminal)
53DNS
80HTTP (unencrypted web)
443HTTPS (encrypted web)
5432PostgreSQL database

When you visit https://example.com, the browser silently connects to port 443. If a URL includes an explicit port, like http://localhost:3000, that overrides the default. Developers see localhost:3000-style URLs constantly: a development server running on your own machine, listening on port 3000.

Ports earn their keep in debugging: a huge share of "cannot connect" bugs are port bugs. The machine is up and the service is running, but the client is dialing port 5432 while the service listens on 5433, and the connection is refused. Whenever a connection fails, "right machine, right port?" is the first question worth asking.

Code exercise · bash

Run this. A case statement maps well-known port numbers to their service names, the same lookup a tool like nmap does when it labels open ports.

Quiz

You type https://example.com into a browser, with no port in the URL. Which port does the browser connect to?

Code exercise · bash

Your turn. Each address below is host:port. Split each one (the same trimming trick from lesson 1-2: ${addr%%:*} keeps what is before the colon, ${addr##*:} keeps what is after) and print "HOST listens on PORT".