Ports Aren’t Physical Sockets — So What Are They?
Change one digit in a web address and the sailing club’s race results disappear. In their place is the camera running order. Both pages come from the same computer. How did the browser choose between them?
On the back of my studio equipment, a port is something I can point to. Follow an HDMI cable and I can show you exactly where a camera is plugged in. Ask me to point to port 443 on my Linux computer and I’d bring up a terminal on the screen.
It’s a question worth asking after building a web server. We’ve seen how DNS finds an IP address and how packets travel across a network. But suppose the packets have arrived. The Linux machine runs a website, answers DNS queries and accepts remote logins. Which program should receive the next piece of data?
One address can have several things to say
A port is a number carried in a TCP or UDP header. When a new TCP connection arrives, the operating system uses the transport protocol, local IP address and destination port to find the software socket waiting for it. A socket, in this sense, is a communication endpoint that a program asks the operating system to create.
Take the address http://127.0.0.1:8000/ apart. The http tells the browser to speak HTTP. The 127.0.0.1 is the IPv4 loopback address, meaning this very computer. The 8000 after the colon tells the browser which TCP port to contact. The final slash asks for the root of that website.
In the TCP/IP stack, those jobs sit at different layers. IP handles addressing and delivery to the host. TCP carries an ordered stream of bytes between endpoints. HTTP gives those bytes meaning as a request for a page and the server’s response. We can watch all three at work without sending anything down a cable.
Why browsers usually leave the port number out
Imagine having to ask for the port number of every website you visited. Protocol defaults save us that conversation.
| Service | Usual server port and transport | What a client asks for |
|---|---|---|
| HTTP | 80/TCP | A browser requests a web page. |
| HTTPS | 443/TCP for HTTP/1.1 or HTTP/2, usually 443/UDP for HTTP/3 over QUIC | A browser requests a page over an encrypted connection. |
| SSH (Secure Shell) | 22/TCP | An SSH client opens an encrypted terminal session on another machine. |
| DNS (Domain Name System) | 53/UDP or 53/TCP | A resolver asks for a record, such as the IP address associated with a name. |
| NTP (Network Time Protocol) | 123/UDP | A computer obtains timing information to synchronise its clock. |
Type an HTTPS address without a port and the browser has a default of 443. Give it http://127.0.0.1:8000/ and you have explicitly chosen HTTP on 8000. The Python servers below do exactly that. Equally, moving a plain HTTP service to 443 would do nothing to encrypt its traffic. Encryption depends on what the client and server actually do.
Notice that DNS appears against both UDP and TCP. Each transport has its own set of port numbers. Linux can have a UDP socket and a TCP socket bound to port 53 at the same address.
Linux practical — give one computer two front pages
You’ll need a Linux machine with Python 3.7 or later, the ss command, and a browser on that machine. Allow about ten minutes. Two terminal tabs will run the servers and a third will let you observe them.
1. Take a reading before you start. In the third terminal, run the following command.
ss -lntuRead the switches as a sentence. Show sockets waiting for traffic (-l), keep the ports numeric (-n), and include TCP (-t) and UDP (-u). Concentrate on State and Local Address:Port. A TCP row marked LISTEN means a program is ready to accept a connection. UDP is connectionless, so a bound UDP socket waiting for datagrams commonly appears as UNCONN. A short list is a perfectly good starting point.
Look for 8000 and 8001. If either TCP port is already in use, choose two unused high-numbered ports, such as 8100 and 8101, and substitute them throughout.
2. Put the race results on port 8000. In the first terminal, enter these commands.
race_dir=$(mktemp -d)
echo '<h1>Race results</h1>' > "$race_dir/index.html"
python3 -m http.server 8000 --bind 127.0.0.1 --directory "$race_dir"Leave that terminal running. The first two lines create a fresh temporary folder with a single page in it. The third starts a small web server and binds it to the loopback address. Open http://127.0.0.1:8000/ in the browser. You should see Race results. Look back at the terminal to see the server log the browser’s request.
3. Put the camera running order on port 8001. In the second terminal, enter these commands.
camera_dir=$(mktemp -d)
echo '<h1>Camera running order</h1>' > "$camera_dir/index.html"
python3 -m http.server 8001 --bind 127.0.0.1 --directory "$camera_dir"Open a second browser tab and change the last digit of the port to make http://127.0.0.1:8001/. The heading now reads Camera running order. Both servers received a request for the same path, /, at the same IP address. The destination port determined which Python process received each connection.
4. Find the evidence on the Linux machine. In the third terminal, run the following command.
ss -ltnAmong the other sockets, look for these two rows. Here are the relevant columns from the test run.
State Local Address:Port
LISTEN 127.0.0.1:8000
LISTEN 127.0.0.1:8001Try ss -ltnp to add the process name and process ID where your account can see them. The two Python processes can have the same name. Their process IDs distinguish them.
The address column matters. A listener on 127.0.0.1 accepts connections from this machine. A TCP listener showing 0.0.0.0 is bound to all local IPv4 interfaces. Whether another machine can reach it also depends on the network and firewall. If you type our loopback address on an iPad, the iPad will look for a server on the iPad.
The client has a port of its own
So far we have chosen the server ports. Who chooses the client’s port? When a browser opens a fresh TCP connection, its operating system normally assigns a temporary source port, also called an ephemeral port. You can catch it in the act using the command-line web client curl. With the race-results server running, use the third terminal for this command.
curl -sS -o /dev/null -w 'Client port %{local_port}, server port %{remote_port}\n' http://127.0.0.1:8000/One run gave Client port 42348, server port 8000. Your client number will probably differ. For the request, TCP’s source port was 42348 and its destination port was 8000. For the reply, the numbers swapped roles.
Here is the answer to another good A-level question. How can thirty pupils use the same web server on port 443 at once? Each TCP connection is identified by both ends — the client IP address and port together with the server IP address and port. A server can accept many distinct connections on 443 while it continues listening there for the next pupil.
This gives a practical route into OCR A Level Computer Science H446, section 1.3.3, which brings together protocols, the TCP/IP stack, layering and client–server networks. On a Windows PC, students can compare TCP listeners by running Get-NetTCPConnection -State Listen in PowerShell and finding the LocalAddress and LocalPort columns.
The question I’d finish the lesson with
Before touching the keyboard, I’d ask a student to point to the page they expect to survive when we stop the race-results server. Then press Ctrl+C in its terminal, run ss -ltn again, and make a fresh request to each address. A page already displayed can remain on screen, so use a hard refresh or request each URL with curl.
The 8000 listener disappears. A fresh connection to it fails. The camera running order on 8001 still loads. Press Ctrl+C in the camera server’s terminal when you’ve finished.
That’s the moment I like in a practical. A student who started with “HTTP is port 80” can now explain why HTTP worked on 8000, why the other page survived and why the same computer could play both client and server. Next time they see a number after a colon in a web address, they have a better question to ask. What is listening there, and what does it expect me to say?









