Address Labels: Ports and Sockets
Your computer demultiplexes network traffic by reading address labels: protocol, IP address, and port.
One apartment building, many doorbells
A street address gets a package to the building. A doorbell number gets it to the right apartment. Your computer is the building. The IP address gets traffic to the computer. The port gets traffic to the right app.
Memorable label: “Doorbells on one building.” Ports are not tiny wires. They are numbers the operating system uses to sort incoming traffic.
Six-year-old version: If three people live in one house, the delivery person needs a name on the package. “House 10, pizza for Maya” goes to Maya. “House 10, letter for Sam” goes to Sam.
The OS is a demultiplexer
Last lesson asked: if a receiver gets network data, how does it know which app should receive it? The answer is a socket.
A listening app tells the operating system: “I am waiting on this protocol, IP address, and port.” Incoming segments carry destination protocol, IP address, and port. The OS matches those labels and delivers the payload to the right app.
For example:
- tcp:8080 could be a local web server.
- tcp:5432 could be a database.
- udp:5353 could be local discovery.
TCP uses ports as part of connection endpoint identification. The TCP RFC describes ports as identifiers used with internet addresses to name sockets (RFC 9293).
Demo: sort packets by doorbell
Deliver each incoming segment. Watch the OS demultiplexer route payloads to apps by protocol, IP, and port. Unknown ports get dropped.
Operating system demux
Application inboxes
| Listener | Socket label | Inbox |
|---|---|---|
| web server | tcp://127.0.0.1:8080 | |
| database | tcp://127.0.0.1:5432 | |
| dropped | no listener |
What to notice: multiplexing at the network card becomes demultiplexing inside the operating system.
How to recognize this problem
- Many apps on one machine need network data: web server, database, SSH, browser, game.
- Traffic arrives on one network interface: the OS must sort it.
- You see “port already in use”: another app already claimed that doorbell.
- You see “connection refused”: packet reached the machine, but no app was listening on that socket.
Exercise: run the port demux model
Run:
node --test tests/port-demux.test.mjs Then open src/port-demux.mjs. Notice how tcp://127.0.0.1:8080 and udp://127.0.0.1:8080 are different labels. Same port number, different protocol.
Knowledge check
Interview payoff
Question: “What role do ports play in demultiplexing?”
Model answer: “Ports help the operating system deliver incoming transport data to the right application. The OS matches incoming protocol, destination IP, and destination port to a socket owned by a listening process. That is demultiplexing: one network interface receives mixed traffic, then labels route each payload to the correct app.”
Pro tip: When debugging local services, always ask two questions: “Is the app listening?” and “On which protocol/IP/port?” Many bugs are just a wrong doorbell.
Next challenge: Ports deliver bytes to an app. But inside one app, how can one thread watch many sockets without blocking on just one?