Transport Layer Overview: TCP, UDP, Sockets, and Port Numbers
This article provides a comprehensive overview of the transport layer in the TCP/IP model, explaining the roles of TCP and UDP protocols, socket APIs, port numbers, multiplexing, and how data is encapsulated and transmitted between end systems.
Welcome to the fourth article in the computer network series. This piece focuses on the transport layer, the fourth layer of the OSI model, which is responsible for end‑to‑end communication between applications on different hosts.
Transport Layer Overview
The transport layer is analogous to a highway, moving packets (called segments ) from one end system to another. An end system can be any device capable of exchanging information, such as a phone, computer, or router.
Transport‑layer protocols enforce rules such as data size limits and protocol selection, enabling logical communication between hosts. These protocols run on end systems, not on routers, similar to a courier delivering a package to the recipient's address.
Key transport protocols are TCP (Transmission Control Protocol) and UDP (User Datagram Protocol). TCP provides reliable, connection‑oriented services, while UDP offers a connection‑less, low‑overhead alternative.
TCP and UDP Prerequisites
TCP is a reliable, connection‑oriented protocol that offers a reliable, connection‑oriented service to the application layer. UDP is a connection‑less protocol that allows applications to send datagrams without establishing a connection.
Why does the network stack have so many terms for describing data?
Both TCP and UDP encapsulate data into segments (TCP) or datagrams (UDP), which are then handed to the network layer.
Sockets
Before sending data, an application must obtain a socket , which acts as an interface between the application layer and the network layer. The operating system provides socket APIs that expose an Application Programming Interface for creating, binding, listening, connecting, reading, writing, and closing sockets.
Socket types include:
Datagram sockets : provide an unconnected service using UDP.
Stream sockets : provide a reliable, connection‑oriented service using TCP.
Raw sockets : allow direct sending and receiving of IP packets without transport‑layer formatting.
The typical socket communication process involves creating a socket descriptor, binding a name (address and port), listening for connections, accepting connections, and then using read/write APIs. Finally, close releases all resources.
Port Numbers
A port number is a 16‑bit unsigned integer (0‑65535) used to differentiate multiple applications on the same host. Well‑known ports (0‑1023) are assigned to standard services (e.g., HTTP 80, FTP 21, SSH 22). Registered ports (1024‑49151) and dynamic/private ports (49152‑65535) are allocated as needed.
When a packet arrives, the destination port determines which socket (and thus which application) should receive the data. The combination of source IP, destination IP, source port, and destination port forms a four‑tuple that uniquely identifies a TCP connection.
Multiplexing and Demultiplexing
Multiplexing allows multiple logical connections to share a single physical link. In UDP (connection‑less), a datagram is identified by a two‑tuple (source IP, source port). In TCP (connection‑oriented), a four‑tuple (source IP, source port, destination IP, destination port) is used.
UDP
UDP (User Datagram Protocol) provides a fast, connection‑less transport service with minimal header overhead (8 bytes). It is used for real‑time applications such as streaming media, voice, video conferencing, and DNS.
Speed: No congestion control, immediate transmission.
No connection setup: No three‑way handshake, reducing latency.
No connection state: No buffers or sequence numbers, allowing many simultaneous users.
Small header: Only 8 bytes compared to TCP’s 20 bytes.
Not all UDP‑based applications are unreliable; reliability can be added at the application layer.
UDP Packet Structure
A UDP packet consists of a header and a data section. The header contains four 16‑bit fields: Source Port, Destination Port, Length, and Checksum.
Source Port : identifies the sending application (optional, defaults to 0).
Destination Port : identifies the receiving application.
Length : total length of header plus data (minimum 8 bytes).
Checksum : provides error detection by summing 16‑bit words and taking the one's complement.
The checksum is calculated by adding 16‑bit words, discarding overflow, and then inverting the bits. If the receiver’s sum of all words (including the checksum) is not all 1s, an error is detected.
Why does UDP provide error detection if it does not guarantee reliability?
UDP follows an end‑to‑end design principle: errors should be detected as low as possible, but recovery is left to higher‑level protocols or applications.
Full-Stack Internet Architecture
Introducing full-stack Internet architecture technologies centered on Java
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.