Sockets

This article is not assessed by the IB but may be helpful to deepen your understanding. Plus, I think it's cool.

The Big Idea

A socket is the fundamental software structure that enables two processes—possibly running on different machines—to communicate over a network. While an IP address identifies which device and a port number identifies which application on that device, a socket represents the actual communication session between the two endpoints.

A socket binds together three key identifiers:

  1. IP address
  2. Transport protocol (TCP or UDP)
  3. Port number

This combination allows data to move reliably and predictably between programs. In the IB DP syllabus, sockets relate directly to how the transport layer supports message delivery (A2.1.5, A2.3.3), and how processes communicate using network protocols.

Sockets are closely related to ports

 

What Is a Socket?

A socket is an abstraction created by the operating system that provides a process with a file-like interface to the network. When a program wants to send or receive data, it doesn’t manipulate packets directly. Instead, it interacts with a socket using operations such as:

  • bind() — attach socket to a port
  • listen() — wait for a connection (TCP servers)
  • connect() — establish a connection (TCP clients)
  • send() / recv() — exchange data
  • close() — release the socket

This design hides underlying complexities—packet fragmentation, routing, retransmissions—allowing programmers to focus on application logic.

 

The Structure of a Socket

A socket is formally defined by a socket address, which consists of:

<protocol, local IP, local port>  
<protocol, remote IP, remote port>

In TCP, this pair is often called a 4-tuple (or 5-tuple if including the protocol explicitly):

(source IP, source port, destination IP, destination port)

Every TCP connection on a machine is uniquely identified by its 4-tuple, allowing thousands of simultaneous connections, even to the same server and port.

Example:
Your browser may open multiple connections to the same server on port 443 by assigning different ephemeral ports on your machine.

 

Sockets in the TCP/IP Model

Sockets sit at the boundary between applications and the transport layer of the TCP/IP stack.
This maps directly to A2.1.5 in the IB guide (HL: TCP/IP model components).

Application Layer
Your program reads/writes bytes using send()/recv().

Transport Layer (TCP or UDP)
The socket links your program’s data to TCP/UDP segments, handles reliability, ordering, port numbers, checksums.

Internet Layer (IP)
Adds source/destination IP addresses and handles routing.

Network Interface Layer
Transmits frames over Ethernet, Wi-Fi, etc.

The socket acts as the application’s gateway into this layered communication framework.

 

TCP Sockets vs UDP Sockets

Sockets behave differently depending on whether they use TCP or UDP—an important distinction required by A2.1.4 (transport protocols).

TCP Sockets (Connection-Oriented)

  • Require a handshake (SYN, SYN-ACK, ACK)
  • Guarantee reliable delivery
  • Preserve message order
  • Provide congestion and flow control
  • Represent long-lived connections

A TCP server typically:

  1. Creates a socket
  2. Binds to a port
  3. Listens for connections
  4. Accepts connections (creating a new socket per client)

Example:
A web server listening on port 443 uses a listening socket. Each client browser gets its own dedicated connection socket once accepted.

UDP Sockets (Connectionless)

  • No handshake
  • No guarantee of delivery or ordering
  • Suitable for latency-sensitive applications
  • Each datagram is handled independently

Example:
DNS queries use UDP sockets on port 53 because speed matters more than guaranteed delivery.

 

Why Sockets Matter

Sockets are essential for:

  • Web browsing
  • Streaming media
  • Online gaming
  • SSH and remote administration
  • Database connections
  • Microservices architectures

Every modern networked application—from WhatsApp to banking apps—uses sockets as its basic communication building block.

 

Example: How a Client–Server Interaction Uses Sockets

1. Server Prepares

server_socket = socket()
server_socket.bind(0.0.0.0, 5000)
server_socket.listen()

2. Client Connects

client_socket = socket()
client_socket.connect(server_ip, 5000)

3. Server Accepts

connection_socket = server_socket.accept()

4. Data Exchange

connection_socket.send()
connection_socket.recv()

5. Close

connection_socket.close()
server_socket.close()
client_socket.close()

 

 

How Sockets Relate to Other Parts of the IB Syllabus

  • A2.1.3 Network devices — sockets interact closely with NICs and drivers.
  • A2.1.4 Protocols — sockets depend on TCP/UDP behavior.
  • A2.3.3 Packet switching — all socket data becomes packets that switches and routers deliver.
  • A2.4.1 Firewalls — block or allow traffic to specific socket endpoints.
  • B2 Programming — higher-level languages expose sockets through libraries (socket in Python, java.net in Java).

Sockets are where theoretical networking connects directly to practical software engineering.

 

Summary

  • A socket is the software endpoint that allows applications to communicate across a network.
  • It combines IP address, port, and protocol to identify a specific communication session.
  • TCP sockets are stateful and reliable; UDP sockets are stateless and fast.
  • Sockets are foundational to all networked applications and appear implicitly or explicitly in almost everything students use online.
  • Understanding sockets helps students see how the TCP/IP model becomes a concrete programming interface.