Multiplexing · Lesson 3 · Beginner

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 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

network input OS DEMUX reads port Web tcp:8080 DB tcp:5432 Drop box ready
Address labels select an app inbox. No listener means no delivery.
Ready. Deliver first segment.

Application inboxes

Payloads delivered by OS demultiplexing
ListenerSocket labelInbox
web servertcp://127.0.0.1:8080
databasetcp://127.0.0.1:5432
droppedno listener

What to notice: multiplexing at the network card becomes demultiplexing inside the operating system.

How to recognize this problem

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

A TCP packet arrives at 127.0.0.1 port 8080. How does the OS choose the receiving app?

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?