TCP

Transmission Control Protocol

A comprehensive study guide for undergraduate computer networks students. Master the protocol that powers the modern internet.

🌐 What is TCP?

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.

Key Characteristics

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.

🔒

Connection-Oriented

Requires establishing a connection (handshake) before data transfer and proper termination after completion.

Reliable Delivery

Uses acknowledgments, sequence numbers, and retransmissions to ensure data arrives correctly.

📊

Flow Control

Implements sliding window mechanism to prevent overwhelming the receiver with too much data.

🚦

Congestion Control

Dynamically adjusts transmission rate based on network conditions to prevent collapse.

🔄

Full-Duplex

Allows simultaneous bidirectional data flow over the same connection.

📦

Byte Stream

Treats data as a continuous stream of bytes without message boundaries.

TCP vs UDP

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

🤝 Connection Management

Three-Way Handshake

TCP uses a three-way handshake to establish a connection. This ensures both sides are ready to communicate and synchronizes sequence numbers.

Client
CLOSED → SYN_SENT
SYN
seq=x
SYN-ACK
seq=y, ack=x+1
ACK
ack=y+1
Server
LISTEN → SYN_RCVD

Why Three-Way?

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.

Connection Termination

TCP connection termination uses a four-way handshake because each side must close the connection independently (full-duplex).

ESTABLISHED

Normal data transfer

FIN_WAIT_1

Sent FIN, waiting for ACK

FIN_WAIT_2

Got ACK, waiting for FIN

CLOSE_WAIT

Got FIN, waiting for app close

CLOSING

Simultaneous close

TIME_WAIT

Waiting 2*MSL

TIME_WAIT State (2MSL Wait)

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

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).

Sliding Window Concept

Sent & ACKed:
1
2
3
Sent, not ACKed:
4
5
Can Send (Window):
6
7
8
Cannot Send:
9
10
📏

Window Size

The 16-bit Window field limits the receiver window to 64KB. Window Scaling (RFC 1323) allows larger windows for high-speed networks.

⏸️

Zero Window

When the receiver's buffer is full, it advertises a zero window. The sender stops transmitting until a non-zero window update is received.

📢

Window Update

Receiver sends window updates as application reads data, allowing sender to continue transmission without waiting.

Silly Window Syndrome (SWS) Prevention

SWS occurs when small data segments are exchanged inefficiently. TCP prevents this using:

🚦 Congestion Control

While flow control prevents receiver overflow, congestion control prevents network overload. TCP treats packet loss as congestion indication and reduces transmission rate.

🐌

Slow Start

Exponential growth phase. cwnd starts at 1 MSS, doubles every RTT until threshold (ssthresh) or loss detected.

Congestion Avoidance

Linear growth phase. After reaching ssthresh, cwnd increases by 1 MSS per RTT (additive increase).

⚠️

Fast Retransmit

On 3 duplicate ACKs, retransmit immediately without waiting for timeout. Indicates possible congestion.

🔄

Fast Recovery

After fast retransmit, stay in congestion avoidance rather than slow start (RFC 2581).

Congestion Window Evolution

Slow Start
Congestion Avoidance
Timeout/Loss

AIMD Principle

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.

Modern TCP Variants

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

🧮 Interactive Tools

Sequence Number Calculator

Next Sequence Number: 1500

Segments needed: 1 | Last byte sent: 1499

Throughput Calculator

Maximum Throughput: 5.24 Mbps

Bandwidth-Delay Product: 8,000 bits | Utilization limited by window

Study Tip

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).

📝 Quick Reference

// TCP Key Formulas and Concepts // Sequence Number Calculation Next_SEQ = ISN + bytes_sent ACK_num = Last_byte_received + 1 // Window Calculations Effective_Window = min(rwnd, cwnd) Throughput ≈ Window_Size / RTT // Timeout Calculation (Jacobson/Karels) Timeout = EstimatedRTT + 4 * DevRTT EstimatedRTT = (1-α)*Old_Est + α*SampleRTT // typically α = 0.125 DevRTT = (1-β)*Old_Dev + β*|SampleRTT - EstimatedRTT| // typically β = 0.25 // Congestion Control Slow Start: cwnd *= 2 every RTT Congestion Avoid: cwnd += MSS every RTT After Loss: ssthresh = cwnd/2, cwnd = 1 (or cwnd/2 for fast recovery)
🔢

Well-Known Ports

HTTP: 80, HTTPS: 443, FTP: 21, SSH: 22, SMTP: 25, DNS: 53

📊

Default Values

MSS: 536 (min), 1460 (Ethernet), Window: 64KB, TTL: varies by OS

⏱️

Timing

MSL: 2 minutes (RFC), Delayed ACK: 200-500ms, Persist Timer: 5-60s