IP addresses, ports, and sockets explained
IPv4 vs IPv6, public vs private IP, NAT, ports 0-65535, well-known ports, socket definition, socket pair uniqueness
IPs, Ports, and Sockets
A socket is the combination of an IP address and a port number. It is the actual endpoint of a network connection — not a URL, not a hostname.
IP addresses
IPv4 addresses are 32-bit numbers written as four octets: 192.168.1.1. About 4.3 billion possible addresses — exhausted in 2011. IPv6 uses 128-bit addresses: 2001:0db8::1, solving the shortage with a virtually unlimited address space.
Private IP ranges (RFC 1918) are not routable on the public internet:
10.0.0.0/8 # Large private networks
172.16.0.0/12 # Medium private networks
192.168.0.0/16 # Home/office networks (your router)NAT (Network Address Translation) maps many private IPs to one public IP. That is why your laptop has 192.168.x.x but the server sees your router's public IP.
Ports
Ports 0–1023 are well-known (reserved by IANA):
- 80 — HTTP
- 443 — HTTPS
- 22 — SSH
- 53 — DNS
Ports 1024–49151 are registered for specific applications. Ports 49152–65535 are ephemeral — the OS picks one randomly for the client side of outgoing connections. That random client port is what makes each connection unique.
Sockets
A TCP connection is uniquely identified by four values: (client IP, client port, server IP, server port). This is why one server IP on port 443 can handle thousands of simultaneous connections — the client-side ephemeral port differs every time.
# Inspect active sockets on Linux/macOS
ss -tnp
# or
netstat -an | grep ESTABLISHED