Sockets

What Are Sockets?

Sockets are software endpoints that enable processes to communicate across a computer network. Each socket combines an IP address and a port number to uniquely identify a communication channel between two networked hosts, giving applications a standard interface to the underlying transport protocols. The concept emerged from the Berkeley Software Distribution (BSD) Unix project in 1982 and became the foundation for nearly all networked application development that followed.

Sockets sit at the boundary between an application and the network stack. An application writes data to a socket as though writing to a file, and the operating system handles framing, routing, and delivery through the appropriate transport protocol. This abstraction allows a developer to build networked software without managing raw packet construction or protocol timing directly.

Socket Types and Communication Models

The two most common socket types correspond to the two principal transport protocols. Stream sockets use the Transmission Control Protocol (TCP) to provide a reliable, ordered byte stream between endpoints. Datagram sockets use the User Datagram Protocol (UDP) to send discrete, connectionless messages with lower overhead and no delivery guarantee. A third type, the raw socket, bypasses the TCP/UDP layer entirely and exposes the IP layer directly; raw sockets are used primarily for network diagnostics, protocol research, and security testing, and their unrestricted use carries well-documented risks including susceptibility to IP spoofing and packet injection. The socket interface overview on ScienceDirect describes how each type maps to specific operations and lifecycle states within the network stack.

The Berkeley Sockets API

The Berkeley Sockets application programming interface defines the functions that programs use to create and manipulate sockets. Core calls include socket() to allocate a socket object, bind() to associate it with an address and port, listen() and accept() to receive incoming connections, and connect() to initiate a connection to a remote endpoint. Data transfer uses send() and recv(), or the more general write() and read() calls inherited from the Unix file abstraction. The API became the industry standard because it was adopted early across Unix variants and later incorporated into the POSIX specification. The IEEE Standard 1003.1g-2000, titled "Protocol Independent Interfaces," formally standardized the socket API as part of POSIX, ensuring portability across compliant operating systems. Microsoft's WinSock is a Windows-specific variant that aligns closely with the Berkeley model.

Connection Lifecycle

A TCP stream socket follows a defined lifecycle. The server side allocates a socket, binds it to a port, and calls listen() to enter a waiting state. When a client calls connect(), the TCP three-way handshake begins: the client sends a SYN segment, the server replies with SYN-ACK, and the client completes with ACK. The server then calls accept(), which returns a new socket dedicated to that client while the original listening socket remains open for additional requests. Data flows bidirectionally until either side calls close(), triggering a four-step FIN exchange that terminates the connection gracefully. For UDP datagram sockets the lifecycle is simpler: neither handshake nor teardown is required, which is why UDP suits applications such as DNS queries and real-time audio where low latency outweighs delivery assurance. The TCP/IP sockets and ports introduction from Microchip Developer Help describes how port numbers at the transport layer route incoming packets to the correct socket on a multitasking host.

Applications

Sockets have applications in a wide range of disciplines, including:

  • Web servers and HTTP clients establishing TCP connections to exchange requests and responses
  • Real-time audio and video streaming over UDP for conferencing and media delivery
  • Networked games using both TCP for reliable state updates and UDP for time-sensitive position data
  • Distributed computing frameworks where worker processes communicate via socket-based message passing
  • Operating system network stacks as the programmable interface exposed to user-space applications
Loading…