How do two programs on different machines actually communicate? Without sockets, there’s no way to establish a connection, send data, and receive responses—the foundation of all network communication is missing.
A socket is a communication endpoint between two machines, uniquely identified by the 4-tuple: (client IP, client port, server IP, server port). It’s the OS-managed channel that allows programs to send and receive data over a network.
- Server Side: Server creates a socket, binds to an IP:port, and listens for connections
- Client Side: Client creates a socket and connects to server’s IP:port
- Connection: TCP handshake establishes the connection (for TCP sockets)
- Communication: Both sides can send() and receive() data through the socket
- Termination: Connection closes when either side calls close()
The socket is essentially a file-like interface managed by the OS kernel. You read from it and write to it just like a file.
- Unique 4-tuple identifies each connection: (client IP, client port, server IP, server port)
- Two main types: TCP (reliable, ordered) and UDP (fast, unreliable)
- Sockets have send and receive buffers to handle network timing differences
- Server has one listening socket per port, plus one connection socket per client
- Everything network-related runs on top of sockets—HTTP, TLS, WebSockets, databases
- Built from: IP Address — IP identifies the machine
- Built from: Port — port identifies the program
- Built from: MAC Address — final delivery uses MAC on local network
- Builds into: TCP Handshake — TCP handshake creates socket connections
- Builds into: HTTP — HTTP runs over socket connections
- Builds into: TLS Handshake — TLS runs over sockets
- Related: Backend as Program — backend programs use sockets to receive requests
- Each client gets a NEW connection socket—server doesn’t reuse the listening socket for data
- Socket buffers can overflow if data isn’t consumed fast enough (backpressure)
- Network partitions can leave sockets in unclear states—timeouts matter
- Non-blocking sockets are used in high-performance servers (Node.js event loop)