Skip to main content

What is a socket?

A socket is an endpoint of a network connection, through which an application sends and receives data over the network.

More simply: a socket is a pairing of an address and a port through which a program communicates over the network.


What a socket actually is

In practice, a socket is uniquely identified by the set:

IP address + port + protocol (TCP or UDP)

Example:

192.168.1.10 : 443 / TCP

This means:

  • a device with IP 192.168.1.10
  • an application listening on port 443
  • using the TCP protocol

Why sockets are needed

Sockets are needed so that:

  • applications can exchange data over the network
  • the operating system understands:
    • which process to pass incoming data to
  • multiple simultaneous connections can be supported

Without sockets:

  • the OS could not connect the network to applications

How sockets are used in a connection

TCP

In TCP, a connection is defined by a pair of sockets:

  • the client socket
  • the server socket

A full TCP connection:

(client IP, client port) ↔ (server IP, server port)

UDP

In UDP:

  • there is no persistent connection
  • a socket simply:
    • receives
    • sends datagrams

Types of sockets (at a basic level)

  • TCP socket - reliable connection, a data stream
  • UDP socket - message delivery without guarantees

What is important to remember for the interview

  • a socket is not a port by itself
  • a socket is the interface between an application and the transport layer
  • one port can serve many sockets (different clients)
  • sockets exist on the side of the OS and applications

Relationship to the OSI layers

  • the application layer - uses sockets
  • the transport layer - implements sockets (TCP/UDP)
  • the network layer - delivers packets

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.