← Lessons

quiz vs the machine

Gold1360

Networking

Port Numbers and Sockets

How ports multiplex many connections onto one IP address.

4 min read · core · beat Gold to climb

Beyond the address

An IP address gets a packet to the right host, but a host runs many programs at once. A port number is a sixteen bit label that picks which program should receive the data. Together an IP and a port identify one endpoint.

The socket

A socket is the operating system handle for one endpoint of a connection. A TCP connection is uniquely identified by a four tuple:

  • The source IP and source port.
  • The destination IP and destination port.

Because the four tuple must be unique, one server port can serve thousands of clients at once, each distinguished by a different client address or port. Well known services listen on fixed ports such as eighty for HTTP, while clients usually pick a high ephemeral port at random.

Why it matters

Ports let one machine host a web server, a database, and an SSH daemon without confusion. Firewalls and load balancers reason about traffic largely by port, and exhausting ephemeral ports is a real failure mode for busy clients.

Key idea

A socket is identified by the four tuple of source and destination IP and port, letting one host multiplex many simultaneous connections.

Check yourself

Answer to earn rating on the learn ladder.

1. What four values uniquely identify a TCP connection?

2. How can one server port serve thousands of clients?