Transmission Control Protocol (TCP)
1. Overview and Core Services
The Transmission Control Protocol (TCP) (RFC 793) is the primary connection-oriented, reliable transport protocol of the Internet.
Key Architectural Services:
- Connection-Oriented: A logical end-to-end connection is established via a Three-Way Handshake before any user data is exchanged.
- Reliable Byte-Stream Service: Data is treated as an unstructured sequence of continuous bytes. Every transmitted byte is assigned an incremental 32-bit sequence number.
- Full-Duplex Communication: Data can travel in both directions simultaneously between two established endpoints.
- End-to-End Flow Control: Prevents a fast sender from overrunning the receiver's memory buffer using a dynamic Advertised Receive Window (
rwnd). - End-to-End Congestion Control: Dynamically throttles transmission rates to prevent collapse of the intermediate global Internet routers using a Congestion Window (
cwnd).
2. TCP Segment Header Format
The TCP header is between 20 and 60 bytes in length:
Critical Header Fields:
- Sequence Number (32 bits): Specifies the byte number of the first byte carried in this segment's payload relative to an Initial Sequence Number (ISN).
- Acknowledgment Number (32 bits): Carries the byte number of the next byte expected from the other party (cumulative acknowledgment).
- Data Offset / HLEN (4 bits): Specifies the total header size in 4-byte (32-bit) words. Minimum value is (), maximum is ().
- Control Flags (6 bits):
- URG: Urgent pointer field is valid (high-priority data).
- ACK: Acknowledgment field is valid (set on all packets after initial SYN).
- PSH: Push data immediately to application without waiting to fill buffer.
- RST: Reset / abort connection due to fatal error or rejected port.
- SYN: Synchronize sequence numbers during connection establishment.
- FIN: Finish / graceful connection termination (no more data to send).
- Window Size (16 bits): Advertises the receiving host's available buffer capacity (
rwnd) in bytes for flow control (can be scaled up to 1 GB via Window Scale option). - Checksum (16 bits): Covers TCP header, payload, and an IP pseudo-header.
3. Connection Establishment: The Three-Way Handshake
To prevent stale duplicate connection requests from establishing phantom connections, TCP uses a Three-Way Handshake:
Note: A SYN segment consumes exactly 1 sequence number even though it carries zero payload bytes.
4. Connection Termination: The Four-Way Handshake
Because TCP is full-duplex, each transmission direction must be shut down independently:
Why the TIME_WAIT State (2 MSL)?
The client must remain in TIME_WAIT for (Maximum Segment Lifetime ) to ensure:
- Its final
ACKarrived at the server. If the ACK was lost, the server retransmitsFIN, which the client can re-acknowledge. - Any delayed duplicate segments wandering through the Internet expire before a new connection reuses the same port numbers.
5. TCP Congestion Control Algorithms
TCP dynamically adjusts its Congestion Window (cwnd) based on network feedback:
- Slow Start:
- Begins with small .
- For every ACK received, increases by 1 MSS. The window doubles every Round-Trip Time (RTT) (exponential growth: ).
- Continues until reaches the Slow Start Threshold (
ssthresh).
- Congestion Avoidance:
- Once , growth switches to Additive Increase (Linear):
- increases by only per entire RTT, cautiously probing for extra bandwidth without inducing packet loss.
- Congestion Response:
- Timeout (Severe Congestion):
(Resets all the way back to Slow Start!)
- Triple duplicate ACKs (Mild Congestion - Fast Retransmit & Fast Recovery):
- 3 duplicate ACKs indicate 1 packet was dropped, but later packets are still flowing through.
- The missing packet is immediately retransmitted without waiting for timeout.
- (Fast Recovery skips slow start and resumes linear growth).
6. Comprehensive Comparison: TCP vs. UDP
| Feature | TCP (RFC 793) | UDP (RFC 768) |
|---|---|---|
| Connection Type | Connection-Oriented (3-way handshake) | Connectionless (0 setup delay) |
| Reliability | Guaranteed (ACKs & Retransmissions) | Best-Effort (No ACKs, packets can drop) |
| Data Flow Unit | Byte Stream (No message boundaries) | Discrete Datagram (Preserves boundaries) |
| Header Size | Fixed | |
| Flow Control | Yes (Sliding window via rwnd) |
None |
| Congestion Control | Yes (Slow start, AIMD via cwnd) |
None (Transmits at application rate) |
| Transmission Mode | Strictly Unicast (Point-to-Point) | Unicast, Broadcast, and Multicast |
| Header Checksum | Mandatory | Optional in IPv4, Mandatory in IPv6 |
| Speed / Latency | Higher latency (buffer & retransmission) | Minimal latency |
| Primary Protocols | HTTP, HTTPS, FTP, SSH, SMTP, BGP | DNS, DHCP, VoIP, TFTP, NTP, SNMP, RTSP |
Frequently Asked Questions
QWhy is the Three-Way Handshake necessary in TCP?
It allows both communicating entities to negotiate and agree on initial sequence numbers (ISNs) and buffer parameters while preventing obsolete, delayed connection requests from creating half-open rogue connections.
QWhat is the difference between `rwnd` and `cwnd`?
rwnd (Receive Window) is advertised by the receiver to prevent buffer overflow (Flow Control). cwnd (Congestion Window) is calculated dynamically by the sender based on packet loss to prevent router queue collapse across the network (Congestion Control).
QWhat triggers Fast Retransmit in TCP?
The arrival of 3 duplicate ACKs for the same sequence number. This signals to the sender that a single packet was lost in transit while subsequent packets reached the receiver, prompting an immediate retransmission before timeout.
QWhy does TCP use the TIME_WAIT state?
The side that closes first waits so that its final ACK can be retransmitted if lost, and so that delayed duplicate segments from the old connection die out before the port pair is reused.