What exactly is a socket, and how does it let programs communicate?

August 4, 2026 5 answers
Asked by MellowCedar42
Could someone explain what a socket is in technical but beginner-friendly terms? I'm especially interested in how sockets relate to files, processes, IP addresses, ports, TCP, UDP, and local inter-process communication.

Answers

BrightWalrus7
A socket is a kernel-managed communication endpoint. It gives a program a way to exchange data with another program, either on the same computer or across a network. Programs typically create a socket, then use operations such as connect, listen, accept, send, and receive. The operating system keeps track of the underlying buffers, connection state, addresses, and permissions.

MellowCedar42: That helpsโ€”so the socket is more like the communication endpoint, while the program uses an operating-system handle to access it?

Reply to this answer
NimbleQuartz56
A simple analogy is a telephone system: the socket is one endpoint, the address and port identify where to reach it, and connect or accept establishes the conversation. After that, both programs exchange bytes through the socket. The socket API hides the lower-level details of moving those bytes between processes or machines.
Reply to this answer
QuietMaple19
On Unix-like systems, sockets are treated similarly to files because the kernel represents them with file descriptors. You can pass that descriptor to operations such as read, write, send, and receive, but a socket is not normally for storing data on disk. Data written to it is placed in a kernel buffer and is consumed when the other side reads it.

SilverPanda63: The file comparison is useful, but itโ€™s worth remembering that the descriptor is the handle the process owns; the socket itself is the kernel object behind that handle.

Reply to this answer
CrispLantern8
For network communication, an endpoint is commonly identified by an address and a port, such as 192.0.2.10:443. A server usually creates a socket, binds it to a local address and port, and listens for connections. A client creates its own socket and connects to the server. With TCP, the connection provides an ordered, reliable byte stream. With UDP, messages are sent as separate datagrams, with less overhead but no built-in guarantee that they will arrive or arrive in order.
Reply to this answer
AmberOtter31
Sockets can also be used between processes on the same machine. Unix-domain sockets use a local filesystem path as their address instead of an IP address and port. They behave much like network sockets, but avoid the network stack and can use filesystem ownership and permissions for access control. A listening server can accept multiple clients and handle each connection separately.
Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions