Skip to content
uencoder

Socket

A socket is a network communication endpoint identified by (protocol, local IP, local port, remote IP, remote port), serving as the interface between the application and transport layers.

Purpose

It gives applications an abstract communication endpoint that hides the underlying protocol details.

Mechanism

The socket API: socket() creates, bind() assigns an address, listen() puts a server in listening mode, accept() takes a connection, connect() dials from the client, send()/recv() transfer data, and close() shuts down. TCP sockets have a state machine; UDP is connectionless.

Use cases

  • Network servers and clients
  • Database connections
  • WebSocket communication
  • Unix domain sockets (local IPC)

Examples

  • Input
    (TCP, 0.0.0.0:8080, client:1234)
    Output
    One connected TCP socket

    The five-tuple

Common errors

Unclosed sockets exhaust available ports
Use SO_REUSEADDR or close sockets promptly

Related terms

Related tools

References