← Module 04 · Layer 4: Transport

Ports & Sockets

A port number alone doesn't identify a conversation. It takes the full combination, a socket, to tell two connections to the exact same service apart.

What a port actually is

An IP (Internet Protocol) address gets a segment to the right host. A port number, a 16-bit field in both the TCP and UDP headers, gets it to the right application on that host. Without ports, a single host could only ever run one network application at a time, since there'd be no way to tell a web server's traffic from a DNS (Domain Name System) resolver's.

RangeNameTypical use
0–1023Well-known portsStandard services: 22 (SSH), 53 (DNS), 80 (HTTP), 443 (HTTPS)
1024–49151Registered portsAssigned to specific applications: 3389 (RDP), 5432 (PostgreSQL)
49152–65535Dynamic / ephemeral portsAssigned temporarily by the OS (Operating System) for the client side of an outgoing connection
PortProtocolService
20 / 21TCPFTP (File Transfer Protocol): data / control
22TCPSSH (Secure Shell)
23TCPTelnet
25TCPSMTP (Simple Mail Transfer Protocol)
53UDP / TCPDNS
67 / 68UDPDHCP (Dynamic Host Configuration Protocol)
80TCPHTTP (Hypertext Transfer Protocol)
110TCPPOP3 (Post Office Protocol v3)
143TCPIMAP (Internet Message Access Protocol)
443TCPHTTPS
3389TCPRDP (Remote Desktop Protocol)

A socket is what actually identifies a conversation

An IP address is a building's street address. A port number is the apartment inside that building mail is headed for. But if you're expecting several different replies from the same apartment, one for a package, one for a bill, just knowing the apartment number isn't enough to sort your own incoming mail. A socket is the full pairing stapled to every exchange: which address and apartment it came from, which address and apartment it's going to, and which protocol it's using. Formally, it's a five-part combination: protocol, source IP, source port, destination IP, destination port. Change any one of those five, and it's a different socket, a completely distinct connection, even if everything else about it is identical.

YOU SERVER :443 Tab 1: source port 51000 ↔ 443 Tab 2: source port 51001 ↔ 443
Same client, same server, same destination port 443, at the same time. The two connections are only distinguishable because the client's OS gave each tab a different ephemeral source port.
ProtocolSourceDestination
Tab 1's socketTCP192.168.1.10:5100093.184.216.34:443
Tab 2's socketTCP192.168.1.10:5100193.184.216.34:443
WHY YOUR OS PICKS A RANDOM SOURCE PORT FOR OUTGOING CONNECTIONS

Every time an application opens an outgoing connection, the OS assigns it an unused port from the ephemeral range, exactly so the situation above works: the OS itself is what keeps every simultaneous outgoing connection from the same host distinct. This is also the exact mechanism PAT depends on, rewriting each internal host's chosen source port to a unique public one so replies for every device come back to the right place.