Transmission Control Protocol (TCP)

Introduction

The Transmission Control Protocol (TCP) is a core protocol of the Internet protocol suite. It provides reliable, ordered, and error-checked delivery of a stream of bytes between applications running on hosts communicating via an IP network. TCP is designed to be robust and to handle network imperfections, making it suitable for a wide range of applications where data integrity and completeness are critical.

Unlike its counterpart, UDP (User Datagram Protocol), TCP guarantees that data arrives in the correct order and without duplication. This reliability comes at the cost of some overhead and potentially higher latency, but for applications like web browsing, email, and file transfer, TCP is the indispensable choice.

Key Features

  • Connection-Oriented: TCP establishes a connection before data transmission begins and terminates it afterward.
  • Reliable Data Transfer: It ensures that data is delivered without loss, duplication, or corruption.
  • Ordered Delivery: Data segments are reassembled in the correct sequence at the destination.
  • Flow Control: Prevents a fast sender from overwhelming a slow receiver.
  • Congestion Control: Manages network traffic to avoid overwhelming the network itself.
  • Full-Duplex Communication: Data can flow in both directions simultaneously.

How TCP Works

TCP operates at the Transport Layer (Layer 4) of the OSI model. It uses a combination of sequence numbers, acknowledgments, and timeouts to achieve its reliability guarantees. Data is broken down into smaller units called segments, which are then encapsulated within IP packets for transmission.

When a host sends data, TCP assigns a sequence number to each byte. The receiving TCP entity acknowledges the receipt of these bytes. If an acknowledgment is not received within a certain timeout period, the sender assumes the segment was lost and retransmits it. The receiver uses the sequence numbers to reorder segments and discard duplicates.

The TCP Three-Way Handshake

Before any data can be exchanged, TCP establishes a connection using a process called the three-way handshake. This ensures that both the sender and receiver are ready to communicate and agree on initial sequence numbers.

  1. SYN (Synchronize): The client sends a SYN segment to the server, indicating its desire to establish a connection and proposing an initial sequence number.
  2. SYN-ACK (Synchronize-Acknowledge): The server receives the SYN, allocates resources, and sends back a SYN-ACK segment. This acknowledges the client's SYN and proposes its own initial sequence number.
  3. ACK (Acknowledge): The client receives the SYN-ACK, acknowledges the server's SYN, and sends back an ACK segment. The connection is now established.

A similar process is used to gracefully terminate a TCP connection.

TCP Segments

A TCP segment is the basic unit of data transfer in TCP. It consists of a TCP header and an optional data payload. The header contains crucial information for managing the connection:

  • Source Port & Destination Port: Identify the sending and receiving application processes.
  • Sequence Number: Identifies the position of the data in the byte stream.
  • Acknowledgment Number: Indicates the next sequence number expected from the sender.
  • Flags: Control bits (SYN, ACK, FIN, RST, PSH, URG) that manage connection state and data transfer.
  • Window Size: Used for flow control.
  • Checksum: Used for error detection.

The structure of a TCP segment header is standardized, ensuring interoperability between different systems.

Flow Control

Flow control mechanisms prevent a sender from overwhelming a receiver with data. TCP uses a sliding window mechanism. The receiver advertises a window size in its acknowledgment packets, indicating how much buffer space it has available. The sender is only allowed to send data up to this advertised window size. As the receiver processes data and frees up buffer space, it updates the window size in subsequent acknowledgments, allowing the sender to transmit more data.

Congestion Control

Congestion control aims to prevent and respond to network congestion. TCP uses algorithms to dynamically adjust its sending rate based on perceived network conditions. When congestion is detected (e.g., through packet loss or increased round-trip times), TCP reduces its sending rate (congestion window). When conditions improve, the rate is gradually increased.

Common congestion control algorithms include:

  • Slow Start
  • Congestion Avoidance
  • Fast Retransmit
  • Fast Recovery

These algorithms help TCP share network bandwidth fairly and efficiently.

TCP in Windows

The Windows operating system implements a robust TCP/IP stack, including advanced features for TCP performance optimization. Microsoft's TCP implementation has evolved significantly over the years, incorporating modern congestion control algorithms and optimizations for high-speed networks.

Administrators can tune TCP parameters on Windows systems to optimize network performance for specific workloads using tools like netsh int tcp show global and registry settings.

Important Note

Understanding the intricacies of TCP congestion control is crucial for diagnosing and resolving network performance issues, especially in high-latency or lossy environments.

Further Reading