Menu

Earn Premium with Referrals

Invite your friends and earn Premium rewards through our referral program.

See how it works and start inviting friends.

Port Numbers & Sockets
CN

Port Numbers & Sockets

How your computer knows which app gets which data: Master Port Types and the Socket abstraction.

Ports allow a single machine to run multiple network applications simultaneously. A port number (16-bit, 0-65535) identifies the specific application or service on a host.

Port Ranges

RangeCategoryExamples
0-1023Well-known ports (system services)HTTP (80), HTTPS (443), SSH (22), DNS (53), SMTP (25)
1024-49151Registered ports (specific apps)MySQL (3306), PostgreSQL (5432), Docker (2375)
49152-65535Ephemeral ports (temporary client ports)Used by browsers, clients

Important Ports to Know for Interviews

PortProtocolService
20/21TCPFTP (data/control)
22TCPSSH
23TCPTelnet
25TCPSMTP
53UDP/TCPDNS
67/68UDPDHCP
80TCPHTTP
110TCPPOP3
143TCPIMAP
443TCPHTTPS
3306TCPMySQL
6379TCPRedis

Sockets

A socket is the API endpoint for network communication. It’s identified by:

Socket = IP Address + Port Number

A TCP connection is uniquely identified by a 5-tuple:

(Source IP, Source Port, Destination IP, Destination Port, Protocol)

Socket API Flow (Server)

socket() → bind(local_port) → listen() → accept() → recv()/send() → close()

Socket API Flow (Client)

socket() → connect(server_ip, server_port) → send()/recv() → close()

Q: List important well-known ports.

A: HTTP (80), HTTPS (443), SSH (22), FTP (20/21), SMTP (25), DNS (53), DHCP (67/68), MySQL (3306).

Q: What is a network socket?

A: A software endpoint for network communication — the combination of an IP address and a port number. Like a “software door” with a specific address (house number + room number).

Q: What are ephemeral ports?

A: Temporary ports (49152-65535) assigned by the OS to client applications for outbound connections. When your browser connects to a website, it gets an ephemeral source port that’s released after the session ends.

Q: How does the socket API work?

A: Server: socket() → bind() → listen() → accept() → recv()/send(). Client: socket() → connect() → send()/recv(). Both call close() when done.

My Private Notes

Notes are auto-saved locally to this device.