IP Addresses and Ports Explained for Beginners
Your new router's leaflet says to open 192.168.1.1 in your browser, and a colleague says a website's machine is up but nothing answers on port 443. This lesson explains IP addresses and ports: the first finds the right machine, the second the right program on it.
IP addresses find the machine
As lesson 1 showed, each packet's header carries a source and a destination IP address (Internet Protocol address), and routers forward it by the destination.
The common kind, IPv4 (IP version 4), looks like 203.0.113.7: four numbers from 0 to 255, separated by dots, each 8 bits (a bit is a single 0 or 1), so 32 bits in all. That allows about 4.3 billion addresses, too few for every device online today.
IPv6 (IP version 6) uses 128 bits, written as eight groups of up to four hexadecimal digits (0 to 9, then a to f) separated by colons. One run of all-zero groups can shrink to two colons, as in 2001:db8::7.
Public and private addresses
A public address is unique on the whole internet, so any machine online can reach it, unless blocked. Websites are reached through public addresses, and the internet side of your home router usually has one too.
A private address works only inside one local network, such as a home or office. Three IPv4 ranges are private: addresses starting with 10, with 172.16 up to 172.31, or with 192.168. Internet routers do not carry packets to them, so millions of homes can each use 192.168.1.5 without clashing.
Note in the table that 172.32.0.1, just past the middle range, is public.
So at home your laptop has a private address, and on IPv4 your devices share the router's public one; lesson 12 shows how.
Ports find the program
One machine runs many network programs at once, such as a web server and a database. A port is a number from 0 to 65535 that says which program the data is for; port 0 is reserved, so real connections use 1 to 65535.
Ports sit in the second header from lesson 1, added by TCP (Transmission Control Protocol) or UDP (User Datagram Protocol), compared in lesson 5.
A server program asks the operating system, the software that runs the computer (Windows, macOS or Linux), for a port; this is called listening. The operating system reads each arriving packet's port and hands the data to the program listening there. In the flowchart, every packet reaches one machine; only the port picks the program.
Ports you will meet often
The Internet Assigned Numbers Authority (IANA) keeps the official list of port numbers. Ports 0 to 1023 are the well-known ports; remember these four:
- 80: HTTP (Hypertext Transfer Protocol), plain web traffic (lesson 6).
- 443: HTTPS (HTTP Secure), encrypted web traffic (lesson 8).
- 22: SSH (Secure Shell), for logging in to another computer's command line.
- 53: DNS (Domain Name System), which turns names into IP addresses (lesson 3).
These are conventions, not laws. A web server on port 8080 needs the port in the address, as in http://example.com:8080; without one, your browser uses 443 for https:// and 80 for http://. The flowchart's PostgreSQL database uses 5432, which a firewall, a filter that blocks unwanted traffic, normally hides from the internet.
Four numbers name every connection
Joined by a colon, 203.0.113.7:443 means port 443 on that machine. IPv6 addresses already contain colons, so they go in square brackets, as in [2001:db8::7]:443. Both are documentation examples, never real machines.
Your side needs a port too: for each connection, your operating system lends your browser a temporary one, usually a high number such as 52114, so replies reach the browser and not your music app.
So four things name a connection: your IP address and port, and the server's IP address and port. Strictly, the protocol is a fifth, since TCP port 53 and UDP port 53 are different doors. That is how one server on port 443 serves thousands of visitors at once: they all share the server's half, but no two share the visitor's half.
"The server is down" or "nothing is listening"?
A failed connection does not always mean "the server is down": the machine and the program fail separately, and the error shows which.
"Connection refused" usually means the machine is up but nothing listens on that port, so its operating system refuses at once (Chrome shows ERR_CONNECTION_REFUSED). Fix it on that machine: start the program, or use the port it really listens on.
A timeout, when your device gives up waiting (ERR_CONNECTION_TIMED_OUT in Chrome), means nothing answered. The machine may be off or unreachable, or a firewall may be dropping packets silently (lesson 13).
Two more traps: a program listening only on 127.0.0.1, which always means "this same machine", is unreachable from other machines. And a reply to ping, the "are you there?" test in lesson 19, proves only that the machine answers, not that a program listens on your port.
Check yourself
Your laptop and a friend's, in another city, are both 192.168.1.5. Is that a problem?
No. 192.168 is private: each such address works only inside its own home network, and online each home uses its router's public address.
A web server serves 5,000 visitors on port 443 at once. How does each reply reach the right one?
Each connection is named by four things. The server's half is shared, but each visitor's IP address and port pair differs, so the connections stay apart.
A site "refused to connect", yet its machine answers ping. What is the likely problem?
The machine is up, but no program listens on that port, perhaps because the web server stopped. Ping checks only the machine, and the refusal came from its operating system.