Transport Layer · Computer Networks

Transmission Control Protocol (TCP)

Watch lectures

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:

  1. Connection-Oriented: A logical end-to-end connection is established via a Three-Way Handshake before any user data is exchanged.
  2. 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.
  3. Full-Duplex Communication: Data can travel in both directions simultaneously between two established endpoints.
  4. End-to-End Flow Control: Prevents a fast sender from overrunning the receiver's memory buffer using a dynamic Advertised Receive Window (rwnd).
  5. 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:

 0                   1                   2                   3
 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|          Source Port (16)     |       Destination Port (16)   |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                        Sequence Number (32)                   |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                    Acknowledgment Number (32)                 |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|  Data |           |U|A|P|R|S|F|                               |
| Offset|  Reserved |R|C|S|S|Y|I|            Window Size (16)   |
| (4 b) |   (6 b)   |G|K|H|T|N|N|                               |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|           Checksum (16)       |       Urgent Pointer (16)     |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                    Options (0 to 40 bytes)                    |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

Critical Header Fields:

  1. 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).
  2. Acknowledgment Number (32 bits): Carries the byte number of the next byte expected from the other party (cumulative acknowledgment).
  3. Data Offset / HLEN (4 bits): Specifies the total header size in 4-byte (32-bit) words. Minimum value is 55 (5×4=20 bytes5 \times 4 = 20\text{ bytes}), maximum is 1515 (15×4=60 bytes15 \times 4 = 60\text{ bytes}).
  4. 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).
  5. 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).
  6. 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:

  CLIENT (Active Open)                                     SERVER (Passive Open)
         |                                                           |  [ LISTEN ]
         |=== 1. SYN (seq = x) =====================================>|
         |    "Let's connect! My initial sequence number is x"       |  [ SYN_RCVD ]
         |                                                           |
         |<== 2. SYN + ACK (seq = y, ack = x + 1) ===================|
         |    "Agreed! My ISN is y, and I acknowledge receipt of x"  |
  [ ESTABLISHED ]                                                    |
         |=== 3. ACK (seq = x + 1, ack = y + 1) ====================>|
         |    "Acknowledged! Connection is now established"          |  [ ESTABLISHED ]
         |                                                           |
         |<============= FULL DUPLEX DATA TRANSFER =================>|

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:

  CLIENT (Active Close)                                    SERVER (Passive Close)
         |                                                           |  [ ESTABLISHED ]
         |=== 1. FIN (seq = u) =====================================>|
         |    "I am finished sending data"                           |  [ CLOSE_WAIT ]
  [ FIN_WAIT_1 ]                                                     |
         |<== 2. ACK (ack = u + 1) ==================================|
  [ FIN_WAIT_2 ]                                                     |  (Server can still send
         |                                                           |   remaining outbound data)
         |<== 3. FIN (seq = v, ack = u + 1) =========================|
  [ TIME_WAIT ]  "I am also finished sending data"                   |  [ LAST_ACK ]
         |                                                           |
         |=== 4. ACK (ack = v + 1) =================================>|
         |                                                           |  [ CLOSED ]
  [ Waits 2*MSL (120s) ]
         |
  [ CLOSED ]

Why the TIME_WAIT State (2 MSL)?

The client must remain in TIME_WAIT for 2×MSL2 \times \text{MSL} (Maximum Segment Lifetime ≈2 minutes\approx 2 \text{ minutes}) to ensure:

  1. Its final ACK arrived at the server. If the ACK was lost, the server retransmits FIN, which the client can re-acknowledge.
  2. 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:

Effective Window=min⁡(cwnd,rwnd)\text{Effective Window} = \min(\text{cwnd}, \text{rwnd})
 cwnd (in MSS)
   ^
16 |                     Threshold (ssthresh = 16)
   |                    +-------------------------
 8 |          . '       | Congestion Avoidance (Linear: +1 MSS per RTT)
 4 |       . '          |
 2 |    . '             |
 1 |  .                 |
   +--------------------+----------------------------------------> Time
      Slow Start
      (Exponential: 2^n)
  1. Slow Start:
    • Begins with small cwnd=1 to 10 MSScwnd = 1\text{ to }10\text{ MSS}.
    • For every ACK received, cwndcwnd increases by 1 MSS. The window doubles every Round-Trip Time (RTT) (exponential growth: 1→2→4→8…1 \rightarrow 2 \rightarrow 4 \rightarrow 8 \dots).
    • Continues until cwndcwnd reaches the Slow Start Threshold (ssthresh).
  2. Congestion Avoidance:
    • Once cwnd≥ssthreshcwnd \geq ssthresh, growth switches to Additive Increase (Linear):
    • cwndcwnd increases by only +1 MSS+1\text{ MSS} per entire RTT, cautiously probing for extra bandwidth without inducing packet loss.
  3. Congestion Response:
    • Timeout (Severe Congestion):
ssthresh←cwnd2,cwnd←1 MSSssthresh \leftarrow \frac{cwnd}{2}, \quad cwnd \leftarrow 1\text{ MSS}

(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.
    • ssthresh←cwnd2,cwnd←ssthresh+3 MSSssthresh \leftarrow \frac{cwnd}{2}, \quad cwnd \leftarrow ssthresh + 3\text{ MSS} (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 20−60 bytes20 - 60\text{ bytes} Fixed 8 bytes8\text{ bytes}
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

7. Key Exam Questions & Takeaways

  1. Why 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.
  2. What 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).
  3. What 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.
Official AppAndroid App

PYQDeck for Android

Solve BEU previous-year exam papers anywhere, anytime. Fast search, offline paper viewing, and step-by-step solutions designed specifically for your phone.

← All Computer Networks modules