AlgoViz
Networking
Networking · MediumLesson 7 of 13

Ports & sockets

An IP address gets a message to the right machine — but one machine runs lots of programs at the same time: a browser, an email app, a game, all on the same computer. So how does an arriving message know WHICH program it's for? The answer is a port number. And a single live connection between two programs — your IP and port talking to their IP and port — is called a socket.

The IP finds the machine; the port finds the program

one machine — IP 142.250.72.14Browserport 443Email appport 25Gameport 27015incoming packets→ :443→ :25→ :27015IP finds the machine; the port picks the program
One machine, one IP, several programs — each waiting at a numbered port. An arriving packet's port number says which program gets it.

Think of an apartment building. The street address gets the mail to the right BUILDING — but the building has many apartments, so you also need an apartment number to reach the right family. The IP address is the street address (it finds the machine). The port number is the apartment number (it finds the program on that machine). So every message actually carries two parts of an address: the IP (which computer) and the port (which program on it). When a packet arrives, the machine looks at the port number and hands it to whichever program is waiting at that number. The browser waits at one port, the email app at another, the game at another — same machine, same IP, but the port keeps their mail from getting mixed up.

Some ports are agreed-on by everyone (well-known ports)

If a port is just an apartment number, how does your browser know which port a website is listening on? Because the world agreed on a few standard numbers ahead of time. Port 80 is the agreed door for plain web traffic (HTTP), and port 443 is the door for secure web traffic (HTTPS). So when you visit a website, your browser knows to knock on port 443 without being told. These are called well-known ports — a small set of pre-agreed numbers for common services, so programs can find each other without arranging it first. You don't need to memorize a list; just know that 80 and 443 mean 'web,' and that the idea is simply 'everyone agreed this number means this service.'

A socket = one specific connection

Now put it together. A socket is one specific live conversation, pinned down by four things: your IP, your port, their IP, and their port. That full set uniquely names a single connection — like saying 'the phone call between THIS exact phone and THAT exact phone right now.' Why four parts? Because your computer can have many connections open at once — several browser tabs each talking to a different server, all on the same machine. Each one is a different socket because at least one of the four parts differs. The socket is how your computer keeps every conversation separate and delivers each reply to the exact tab that asked. One socket = one connection = (your IP + port) ↔ (their IP + port).

Questions you might have

Why do I need a port if the IP already found the right computer?

Because one computer runs many programs at once — browser, email, game — and they all share the one IP. The port is the apartment number after the street address: it tells the machine which program a message belongs to, so the browser's mail doesn't land in the game.

Who decides which port a program uses?

For common services, the world agreed on standard numbers ahead of time — like 80 and 443 for web traffic — so programs can find each other without arranging it. Other programs just pick a free number when they need one. Your browser, when it makes a request, is usually given a random free port to receive the answer on.

What's the difference between a port and a socket?

A port is just a number that labels one program on one machine. A socket is a whole live connection — it's the pairing of your IP and port with their IP and port. Think of the port as the apartment number, and the socket as the actual phone call happening between two specific apartments.

How can I have many browser tabs open to different sites at once?

Each tab's connection is a separate socket. Even though they share your one IP, each uses a different port on your side (or talks to a different server), so the four-part combination is unique. Your computer uses that uniqueness to deliver each site's reply back to the exact tab that asked for it.

🧠An IP address finds the machine and a port number finds the program on it (like a street address plus an apartment number); a socket is one specific live connection, named by your IP+port talking to their IP+port.
✅ Check yourself4 quick questions — prove the idea stuck.Start →

Best read after: TCP vs UDP, IP addresses