Skip to main content
networking

Socket

A socket is a communication endpoint identified by an IP address and port number pair. A TCP connection is uniquely identified by a 5-tuple: (protocol, src IP, src port, dst IP, dst port). Applications create sockets to send and receive data – the OS manages the underlying protocol state.

Definition

A socket is the programming abstraction for network communication. The Berkeley sockets API (socket, bind, listen, accept, connect, send, recv) is the foundation of all network programming on Unix and Windows. A listening server socket binds to a port and accepts incoming connections, creating a new connected socket for each client. Each connected socket represents one conversation. A server on port 80 can handle thousands of simultaneous connections because each is uniquely identified by the full 5-tuple, not just the local port. Socket buffers in the kernel hold data between the network and the application – the receive buffer fills when data arrives faster than the application reads, and the send buffer queues data until the network can transmit it.

Examples

  • socket(AF_INET, SOCK_STREAM, 0) creates a TCP socket
  • netstat -tlnp shows listening sockets
  • ss -s shows socket statistics by state
  • lsof -i :8080 shows which process holds port 8080

Related Protocols

Related Terms