Transport Layer
The transport layer (OSI Layer 4) provides end-to-end (process-to-process) communication between applications on different hosts. It handles segmentation, flow control, error control, and connection management.
Transport Layer Services
The transport layer provides process-to-process delivery using port numbers (0–65535). Well-known ports (0–1023) are assigned to standard services (HTTP=80, FTP=21, SSH=22). It offers either connection-oriented (reliable) or connectionless (unreliable but fast) service.
UDP
UDP (User Datagram Protocol) is connectionless and unreliable. It adds minimal overhead: source port, destination port, length, and checksum. UDP is used for real-time applications (VoIP, video streaming, DNS, DHCP) where speed matters more than reliability.
TCP
TCP (Transmission Control Protocol) is connection-oriented and reliable. It guarantees in-order delivery, error-free data, and flow control. TCP segments include sequence numbers, acknowledgment numbers, window size, flags (SYN, ACK, FIN, RST), and checksum.
TCP Connection Management
TCP uses a three-way handshake to establish a connection: SYN → SYN-ACK → ACK. Connection termination uses a four-way handshake: FIN → ACK → FIN → ACK. The TIME-WAIT state ensures all segments are received before closing.
TCP Flow Control
TCP uses a sliding window mechanism for flow control. The receiver advertises a receive window (rwnd) indicating how much data it can buffer. The sender limits unacknowledged data to this window size. Window scaling allows windows larger than 64 KB.
TCP Congestion Control
TCP manages network congestion using four algorithms: slow start (exponential growth from 1 MSS), congestion avoidance (linear growth after threshold), fast retransmit (retransmit after 3 duplicate ACKs), and fast recovery (avoid slow start after fast retransmit). The congestion window (cwnd) limits sending rate.
Multiplexing and Demultiplexing
Multiplexing at the sender combines data from multiple sockets and adds transport headers. Demultiplexing at the receiver delivers segments to the correct socket. UDP uses destination port; TCP uses the 4-tuple (source IP, source port, destination IP, destination port).
Summary
The transport layer bridges the gap between applications and the network. TCP provides reliable, ordered delivery with flow and congestion control; UDP provides lightweight, fast delivery for time-sensitive applications.