Menu
Coddy logo textTech

What Is a Port

Lesson 10 of 47 in Coddy's Networking with the Terminal course.

An address gets your request to the right machine. But a single machine may be running a website, a database and a mail server at once. Something has to say which of them the request is for.

That is a port: a number from 1 to 65535 that picks the program. The address is the building, the port is the door.

In a URL it comes after a colon:

curl -s http://127.0.0.1:9104/hello.txt
#                        ^^^^ the port

An address and a port together identify one end of a conversation, and that pair is called a socket. When you hear that a server "opened a socket on 9104", it means exactly this pairing.

Two programs on one machine cannot both listen on the same port, which is the cause of the error every developer meets eventually: address already in use.

challenge icon

Challenge

Hard

Prove that the port picks the program, by running two servers on one machine.

  1. Make two folders, a and b
  2. Put a file i.txt in each: AAA in a, BBB in b
  3. Serve a on port 9104 and b on port 9105
  4. Fetch i.txt from each port, 9104 first

Same address, same file name, different door:

AAA
BBB

(cd a && python3 -m http.server 9104 &) starts a server for one folder without changing the folder you are standing in.

Try it yourself

Terminal
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Networking with the Terminal

Practice on your own: Terminal playground