TCP
TCP (Transmission Control Protocol) provides a reliable, ordered, byte-stream service between processes on hosts; engineers use it for implementing dependable application-layer services and tuning performance (flow/congestion control, timers, and options).
Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.
Why it matters
TCP (short for Transmission Control Protocol) is the main transport protocol used for reliable data transfer on the Internet (web, email, file transfer). Engineers design servers, tune performance, diagnose network problems and implement congestion-control policies using TCP concepts such as the three-way handshake, sequence numbers, retransmission timers, the congestion window, and algorithms like Slow Start and AIMD.
Key ideas
Sequence number: a 32-bit number that labels the position (byte offset) of the first data byte in a TCP segment in the sender's byte stream.
Acknowledgment number (ACK): a 32-bit number meaning “next byte expected” — cumulative acknowledgement of all bytes up to ACK-1.
Window: a flow-control field that advertises how many bytes the receiver can accept (called rwnd for receiver window).
Congestion window (cwnd): sender-side limit to bytes in-flight imposed by congestion control.
MSS (Maximum Segment Size): maximum TCP payload size in bytes the sender will send in one TCP segment.
RTO (Retransmission Timeout): time after which an unacknowledged segment is retransmitted.
SACK (Selective Acknowledgement): TCP option allowing the receiver to inform sender about non-contiguous blocks of data received.
TCP services and properties (exact statement):
- Connection-oriented reliable byte stream: TCP provides an ordered, reliable stream of bytes between endpoints after an explicit connection is established. Assumptions: IP may lose, duplicate, reorder or delay datagrams; TCP uses sequence numbers, acknowledgements and retransmission to present a reliable, in-order stream to the application.
- Full-duplex: both endpoints can send data simultaneously; each direction has its own sequence and ack numbering.
Segment format (write and label this diagram in exams): draw a single rectangle for a TCP segment with these labeled fields left-to-right, top-to-bottom: Source port (16 bits), Destination port (16 bits), Sequence number (32 bits), Acknowledgment number (32 bits), Data offset (4 bits), Reserved (3 bits), Flags (9 bits: NS, CWR, ECE, URG, ACK, PSH, RST, SYN, FIN), Window (16 bits), Checksum (16 bits), Urgent pointer (16 bits), Options (variable, e.g., MSS, SACK-permitted, Timestamps), Data (variable). Note: the header length is Data offset × 32-bit words.
Connection establishment (the three-way handshake) — exact steps to draw and state:
- Client sends segment with SYN, seq = x. (No data.)
- Server replies with SYN+ACK, seq = y, ack = x+1.
- Client sends ACK, seq = x+1, ack = y+1. Connection established.
- Purpose: negotiate initial sequence numbers and ensure both sides can receive and send.
Connection termination: typically a four-segment exchange (FIN/ACK from one side, ACK, then FIN/ACK from other side, final ACK). Alternatively simultaneous FINs can happen. The endpoint that sends the final ACK enters TIME-WAIT for 2×MSL; know key TCP states (LISTEN, SYN-SENT, SYN-RECEIVED, ESTABLISHED, FIN-WAIT-1/2, CLOSE-WAIT, CLOSING, LAST-ACK, TIME-WAIT).
Reliability mechanisms:
- Cumulative ACKs: ack N means all bytes < N received in order.
- Retransmission: after RTO expires for an unacknowledged segment, sender retransmits.
- Fast retransmit: receipt of 3 duplicate ACKs for the same next-byte indicates a likely single packet loss; sender retransmits the missing segment without waiting for RTO.
- SACK option: receiver may send blocks of received data to allow sender to retransmit only missing segments (selective retransmission).
- Checksum: 16-bit one's-complement checksum computed over a pseudo-header (IP src/dst, protocol, TCP length) plus TCP header and data.
Flow control vs congestion control (define and contrast):
- Flow control (rwnd) prevents sender from overwhelming the receiver's buffer. Receiver advertises rwnd; sender ensures bytes in flight ≤ rwnd.
- Congestion control (cwnd + algorithms) prevents global network congestion. Sender ensures bytes in flight ≤ min(rwnd, cwnd).
Congestion-control algorithms (classic TCP Reno/Tahoe principles) — exact textbook rules:
- Slow Start: initialize cwnd = 1 × MSS (or sometimes 2 MSS; modern stacks often use an initial window of 10 MSS per RFC 6928); on each ACK cwnd += MSS (this gives approximately doubling of cwnd each RTT). Continue until cwnd ≥ ssthresh.
- Congestion Avoidance (AIMD): after crossing ssthresh, increase cwnd approximately by 1 MSS per RTT (per-ACK rule: cwnd += (MSS*MSS)/cwnd bytes) — this is the additive increase part.
- On loss detected by timeout: set ssthresh = max(cwnd/2, 2*MSS); set cwnd = 1 × MSS; enter Slow Start (Tahoe and Reno behave the same on timeout).
- On loss detected by 3 duplicate ACKs (fast retransmit):
- Tahoe: set ssthresh = cwnd/2; cwnd = 1 MSS; enter Slow Start.
- Reno: set ssthresh = cwnd/2; set cwnd = ssthresh + 3*MSS; retransmit missing segment (fast retransmit); for each additional duplicate ACK, cwnd += MSS (to allow for packets that left the network) and upon new ACK that acknowledges the retransmitted segment, set cwnd = ssthresh and enter Congestion Avoidance.
- The overall control is AIMD (Additive Increase, Multiplicative Decrease): increase slowly, cut multiplicatively on loss.
RTT & RTO estimation (Jacobson/Karels algorithm): use smoothed RTT and smoothed deviation.
- Maintain EstimatedRTT and DevRTT. Update per new SampleRTT with constants α (commonly 1/8) and β (commonly 1/4): EstimatedRTT ← (1 − α)·EstimatedRTT + α·SampleRTT DevRTT ← (1 − β)·DevRTT + β·|SampleRTT − EstimatedRTT| RTO ← EstimatedRTT + 4·DevRTT. Per RFC 6298, clamp RTO to at least 1 s and initialize RTO to 1 s at connection start.
- Karn's algorithm: do not update RTT estimate from segments that have been retransmitted (ambiguous sample); use exponential backoff on RTO for repeated timeouts.
Important options: MSS (negotiated in SYN), Window scale (for large windows; negotiation in SYN), SACK (selective ack blocks), Timestamps (PAWS and better RTT measurement). Window scale: actual window = advertised_window × 2^scale (shift count up to 14). Other mechanisms often asked in exams: Nagle’s algorithm (coalesces small writes), delayed ACKs, and Silly Window Syndrome with Clark’s solution.
Formulas
MSS = MTU - IP_header_length - TCP_header_length — MSS in bytes; MTU and headers in bytes. Applies when computing maximum TCP payload given link MTU. Typical IP/TCP headers without options are 20 bytes each.
EstimatedRTT = (1 - α)·EstimatedRTT + α·SampleRTT — α dimensionless (typical 1/8), times in seconds.
DevRTT = (1 - β)·DevRTT + β·|SampleRTT - EstimatedRTT| — β dimensionless (typical 1/4), times in seconds.
RTO = EstimatedRTT + 4·DevRTT — timeout in seconds; per RFC 6298 clamp RTO to ≥ 1 s and initialize RTO to 1 s.
Bytes_in_flight ≤ min(rwnd, cwnd) — bytes in flight limit; all in bytes.
Throughput ≈ MSS / (RTT · sqrt(2p/3)) — an approximate upper bound (Mathis formula) where p is steady-state segment loss probability (fraction), MSS in bytes, RTT in seconds; result in bytes/s. (See verify notes below.)
AIMD increase per RTT (congestion avoidance): cwnd ← cwnd + MSS per RTT ≈ cwnd ← cwnd + (MSS*MSS)/cwnd per ACK (both in bytes).
Worked examples
Example 1 — MSS from MTU
- Given: Ethernet MTU = 1500 bytes, IPv4 header without options = 20 bytes, TCP header without options = 20 bytes.
- Formula:
MSS = MTU - IP_header - TCP_header. - Calculation:
MSS = 1500 - 20 - 20 = 1460 bytes. - Answer: MSS = 1460 bytes.
Example 2 — RTO update (Jacobson/Karels)
- Given: initial EstimatedRTT = 100 ms (0.100 s), DevRTT = 10 ms (0.010 s), α = 1/8, β = 1/4. New SampleRTT = 120 ms (0.120 s).
- EstimatedRTT ← 0.875·0.100 + 0.125·0.120 = 0.0875 + 0.015 = 0.1025 s (102.5 ms).
- DevRTT ← 0.75·0.010 + 0.25·|0.120 − 0.1025| = 0.0075 + 0.25·0.0175 = 0.0075 + 0.004375 = 0.011875 s (11.875 ms).
- RTO = 0.1025 + 4·0.011875 = 0.1025 + 0.0475 = 0.1500 s (150.0 ms).
- Answer: New RTO ≈ 150.0 ms.
Example 3 — cwnd growth in Slow Start switching to Congestion Avoidance
- Given: start cwnd = 1 MSS, ssthresh = 8 MSS, measure per-RTT progression.
- RTT0: cwnd = 1 MSS.
- After 1 RTT (ACKs returned): cwnd ≈ 2 MSS (doubled).
- After 2 RTTs: cwnd ≈ 4 MSS.
- After 3 RTTs: cwnd ≈ 8 MSS; cwnd has reached ssthresh → switch to Congestion Avoidance.
- After 4 RTTs (congestion avoidance): cwnd increases by ≈1 MSS per RTT → cwnd ≈ 9 MSS.
- Answer: cwnd sequence per RTT = 1, 2, 4, 8, 9, 10, ... MSS.
Optional numeric throughput example (use Mathis approximation): given MSS = 1460 bytes, RTT = 100 ms = 0.1 s, loss p = 0.001.
- sqrt(2p/3) = sqrt(2·0.001/3) ≈ 0.02582.
- Throughput ≈ 1460 / (0.1 · 0.02582) ≈ 1460 / 0.002582 ≈ 565,776 bytes/s ≈ 4.53 Mb/s.
- Answer: ≈ 565.8 kB/s (≈ 4.53 Mb/s).
Common mistakes
- Thinking TCP guarantees zero loss on the network; TCP only provides reliable delivery to the application via retransmission.
- Updating RTT estimate from retransmitted segments (Karn’s rule forbids this).
- Forgetting header sizes when computing MSS from MTU (IP and TCP headers must be subtracted).
- Misstating the three-way handshake (some write 2-way) or forgetting SYN consumes one sequence number.
- Omitting that ssthresh = max(cwnd/2, 2MSS) (textbooks often require a lower bound of 2MSS).
- Using the Mathis throughput formula without mentioning it is an approximation that assumes TCP Reno-like behavior and steady-state losses.
For your exam
Types of questions:
- Definitions: define TCP, segment, MSS, RTO, SACK, cwnd, rwnd.
- Short notes: explain three-way handshake, TCP header fields, difference between flow and congestion control, SACK, TCP timers.
- Derivations: derive RTO update equations (show Jacobson/Karels steps), show cwnd update per RTT in slow start and congestion avoidance.
- Numericals: compute MSS from MTU, compute new EstimatedRTT/DevRTT/RTO from SampleRTT, trace cwnd growth and behavior after loss.
- Diagrams: draw TCP segment, timeline for three-way handshake, cwnd vs time graph with slow start and congestion-avoidance regions.
What a full-marks answer includes:
- Precise definitions (bold terms on answer sheet if allowed), units, and assumptions.
- Correct formulas and step-by-step numeric substitution with units carried through.
- For diagrams: clearly labelled axes, packets/flags, and sequence/ack numbers.
- For protocols: specify exact flag fields (SYN, ACK, FIN) and sequence number arithmetic (e.g., ack = seq + 1 after SYN).
Quick check
- What does an ACK number of 1001 mean in TCP?
- How does TCP perform flow control?
- What is the purpose of the three-way handshake?
Answers: 1) All bytes up to 1000 have been received in order; next expected byte is 1001. 2) Receiver advertises a receive window (rwnd); sender limits bytes in flight ≤ rwnd. 3) To synchronize sequence numbers and ensure both endpoints can receive and send (establish an ordered, reliable connection).
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?