A comprehensive study guide for undergraduate computer networks students. Master the protocol that powers the modern internet.
TCP (Transmission Control Protocol) is a connection-oriented, reliable transport layer protocol that provides ordered, error-checked delivery of data between applications running on hosts communicating via an IP network.
TCP is the backbone of reliable internet communication, ensuring that data sent from one application to another arrives completely, in order, and without errors. It operates at the Transport Layer (Layer 4) of the OSI model.
Requires establishing a connection (handshake) before data transfer and proper termination after completion.
Uses acknowledgments, sequence numbers, and retransmissions to ensure data arrives correctly.
Implements sliding window mechanism to prevent overwhelming the receiver with too much data.
Dynamically adjusts transmission rate based on network conditions to prevent collapse.
Allows simultaneous bidirectional data flow over the same connection.
Treats data as a continuous stream of bytes without message boundaries.
| Feature | TCP | UDP |
|---|---|---|
| Connection | Connection-oriented | Connectionless |
| Reliability | Reliable (acknowledgments) | Unreliable (best-effort) |
| Ordering | Ordered delivery | No ordering guarantees |
| Speed | Slower (overhead) | Faster (minimal overhead) |
| Use Cases | Web, Email, FTP | Streaming, DNS, Gaming |
| Header Size | 20-60 bytes | 8 bytes |
The TCP header contains 10 mandatory fields and an optional extension field (options). The total header size ranges from 20 bytes (minimum) to 60 bytes (maximum with options).
TCP uses a three-way handshake to establish a connection. This ensures both sides are ready to communicate and synchronizes sequence numbers.
The three-way handshake prevents delayed duplicate connection initiations from causing confusion. It ensures both parties agree on initial sequence numbers and confirms full-duplex communication capability.
TCP connection termination uses a four-way handshake because each side must close the connection independently (full-duplex).
Normal data transfer
Sent FIN, waiting for ACK
Got ACK, waiting for FIN
Got FIN, waiting for app close
Simultaneous close
Waiting 2*MSL
After closing, TCP waits for 2 Maximum Segment Lifetimes (typically 2-4 minutes) to ensure the last ACK was received and to prevent old duplicate segments from being accepted in new connections.
Flow control prevents the sender from overwhelming the receiver with data. TCP uses a sliding window mechanism where the receiver advertises its available buffer space (rwnd - receive window).
The 16-bit Window field limits the receiver window to 64KB. Window Scaling (RFC 1323) allows larger windows for high-speed networks.
When the receiver's buffer is full, it advertises a zero window. The sender stops transmitting until a non-zero window update is received.
Receiver sends window updates as application reads data, allowing sender to continue transmission without waiting.
SWS occurs when small data segments are exchanged inefficiently. TCP prevents this using:
While flow control prevents receiver overflow, congestion control prevents network overload. TCP treats packet loss as congestion indication and reduces transmission rate.
Exponential growth phase. cwnd starts at 1 MSS, doubles every RTT until threshold (ssthresh) or loss detected.
Linear growth phase. After reaching ssthresh, cwnd increases by 1 MSS per RTT (additive increase).
On 3 duplicate ACKs, retransmit immediately without waiting for timeout. Indicates possible congestion.
After fast retransmit, stay in congestion avoidance rather than slow start (RFC 2581).
Additive Increase Multiplicative Decrease (AIMD): TCP conservatively increases window size (additive) but aggressively decreases it (multiplicative) upon congestion detection. This provides stability in the network.
| Variant | Key Feature | Use Case |
|---|---|---|
| TCP Reno | Fast Retransmit + Fast Recovery | Standard implementation |
| TCP New Reno | Improved recovery from multiple losses | High loss networks |
| TCP CUBIC | Cubic window growth function | High BDP networks (Linux default) |
| TCP BBR | Bottleneck Bandwidth and RTT | Google's model-based approach |
| TCP Vegas | Delay-based congestion detection | Avoids congestion before loss |
Segments needed: 1 | Last byte sent: 1499
Bandwidth-Delay Product: 8,000 bits | Utilization limited by window
Remember that TCP sequence numbers count bytes, not segments! If ISN=1000 and you send 500 bytes, the next sequence number is 1500. ACK numbers indicate the next expected byte (last received + 1).
HTTP: 80, HTTPS: 443, FTP: 21, SSH: 22, SMTP: 25, DNS: 53
MSS: 536 (min), 1460 (Ethernet), Window: 64KB, TTL: varies by OS
MSL: 2 minutes (RFC), Delayed ACK: 200-500ms, Persist Timer: 5-60s