Transport Layer · Computer Networks

Transmission Control Protocol (TCP)

Watch lectures
SVGVector Architecture Diagram (SVG)
COMPUTER NETWORKS · Module 4★ High PriorityTransmission Control Protocol (TCP)IT Sem 6 · Code 100602 · Reliable byte streamMNEMONICTCP = connection-oriented, reliable byte stream: SYN, SYN+ACK, ACK to start.rwnd protects the receiver, cwnd protects the network: window = min(cwnd, rwnd).Core BreakdownServicesRFC 793: connection-oriented, reliable, full-duplex byte-stream transport.Each byte has a 32-bit sequence number; ACKs are cumulative.Header20-60 bytes: ports, seq, ack, HLEN, flags URG ACK PSH RST SYN FIN, window,checksum, urgent pointer, options. HLEN counts 4-byte words (5 to 15).3-way openSYN (seq=x), SYN+ACK (seq=y, ack=x+1), ACK (ack=y+1); a SYN uses 1 number.Agrees initial sequence numbers and blocks stale duplicate requests.4-way closeFIN, ACK, FIN, ACK: each direction is closed on its own (full-duplex).The active closer waits TIME_WAIT = 2 x MSL (about 2 min) before CLOSED.Flow controlReceiver advertises rwnd in the window field so a fast sender cannot overrun it.Sender keeps at most min(cwnd, rwnd) unacknowledged bytes in flight.CongestionSlow start doubles cwnd per RTT up to ssthresh, then +1 MSS per RTT (additive).Timeout: ssthresh = cwnd/2, cwnd = 1. 3 dup ACKs: fast retransmit / recovery.Diagram: Handshake and cwndClientServer1 SYN seq=x2 SYN+ACK seq=y ack=x+13 ACK ack=y+1 → ESTABLISHEDcwndssthreshtimeoutSlow start x2/RTTCong. avoid +1 MSS/RTT⚠️ Exam Traps & Viva Favorites① Why a three-way handshake?Both sides agree on initial sequence numbersand a stale duplicate SYN cannot open aphantom connection.② rwnd vs cwnd?rwnd: set by receiver (flow control). cwnd:set by sender from loss (congestion control).Sender uses the smaller of the two.③ What is TIME_WAIT for?Lets the last ACK be resent if lost, and letsold duplicates die (2 x MSL) before the sameports are reused.Tip: SYN, SYN+ACK, ACK · 3 dup ACKsMaster Revision Table(learn it row by row!)ItemValue / ruleMeaningPitfallHeader size20 - 60 bytesHLEN 5 to 15 words of 4 bytesUDP is only 8 bytesSYN / FINUse 1 sequence numberEven with zero data bytesA bare ACK uses noneFlagsURG ACK PSH RST SYN FIN6 control bits in the headerRST aborts a connectionSlow startcwnd doubles every RTTExponential until ssthreshName misleads: it is fastTimeoutssthresh = cwnd/2, cwnd = 1Severe congestion: restartDiffers from 3 dup ACKs3 dup ACKsFast retransmit + recoverycwnd = ssthresh + 3 MSSNo slow-start restartQUICK FACTSWindow: effective = min(cwnd, rwnd) · TIME_WAIT = 2 x MSL (about 120 s)States: LISTEN, SYN_RCVD, ESTABLISHED, FIN_WAIT_1/2, CLOSE_WAIT, LAST_ACK, TIME_WAITMandatory checksum over header, data and pseudo-header · HTTP 80, SSH 22, SMTP 25VERIFIED★ Golden rule: handshake to connect, windowsto control, ACKs to guarantee delivery.

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:

SVGVector Architecture Diagram (SVG)
0 8 16 24 31 Source Port (16) Destination Port (16) Sequence Number (32) Acknowledgment Number (32) Data Offset 4 bits Reserved 6 bits URG ACK PSH RST SYN FIN Window Size (16) Checksum (16) Urgent Pointer (16) Options (0 to 40 bytes) TCP header = 20 bytes minimum, up to 60 bytes with options

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:

MermaidDiagram
Rendering diagram…

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:

MermaidDiagram
Rendering diagram…

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})
SVGVector Architecture Diagram (SVG)
16 8 4 2 1 cwnd (in MSS) Threshold ssthresh = 16 MSS Slow Start - exponential growth 2^n Congestion Avoidance - linear +1 MSS per RTT Time
  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

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 2×MSL2 \times \text{MSL} 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.

Budget AppAndroid App

Pocketly on Android

Log daily food, travel, and hostel expenses instantly. Set monthly spending limits and see where your money goes without complex spreadsheets.

← All Computer Networks modules