📡 Tutorial 6: TCP Fundamentals and Segment Structure (Advanced)
University‑level treatment – COMP347 (TrustOpen University)
🎯 Learning Objectives
After completing this tutorial, you should be able to:
- Describe the TCP service model: connection‑oriented, reliable, ordered, full‑duplex, byte‑stream.
- Explain each field in the TCP segment header, including flags, window, checksum, and options.
- Analyze the semantics of sequence and acknowledgment numbers in TCP’s byte‑stream abstraction.
- Calculate RTT estimates and timeout intervals using SampleRTT, EstimatedRTT, and DevRTT.
- Describe the sender and receiver events in TCP’s reliable data transfer (timeouts, ACKs, duplicate ACKs).
- Explain flow control via the advertised window (rwnd) and zero‑window probing.
- Evaluate the impact of Nagle’s algorithm, delayed ACKs, and Silly Window Syndrome on performance.
- Identify common TCP options (MSS, Window Scaling, SACK, Timestamps).
🔍 Overview
TCP (Transmission Control Protocol) is the workhorse of the Internet, providing reliable, ordered, and error‑checked delivery of a byte‑stream between applications. This tutorial delves into the TCP segment format, the role of sequence and acknowledgment numbers, the estimation of round‑trip times for timeout management, and the flow control mechanism that prevents a sender from overwhelming a receiver. We also explore advanced topics that influence TCP’s behaviour in real‑world networks, such as Nagle’s algorithm, delayed acknowledgments, and the Silly Window Syndrome. Understanding these fundamentals is essential for grasping TCP’s more complex congestion control and connection management features covered later.
📘 1. TCP Overview and Service Model
TCP provides a connection‑oriented, reliable, full‑duplex byte‑stream service to applications. Key characteristics:
- Connection‑oriented: A logical connection is established before data exchange using a three‑way handshake.
- Reliable: Uses acknowledgments, sequence numbers, and retransmissions to recover from packet loss, corruption, and reordering.
- Ordered: Data is delivered to the application in the exact order it was sent.
- Full‑duplex: Data can flow in both directions simultaneously.
- Byte‑stream: TCP does not preserve message boundaries; it delivers a continuous stream of bytes.
- Flow control: Prevents sender from overwhelming receiver.
- Congestion control: Prevents network overload (covered in later tutorials).
TCP is defined in RFC 793 (1981) and has been extended by numerous RFCs (e.g., RFC 1323 for window scaling, RFC 2018 for SACK).
📘 2. TCP Segment Structure – Detailed Field Analysis
A TCP segment consists of a header (minimum 20 bytes, up to 60 bytes with options) followed by data. The header format is:
- Source Port (16 bits): Port of the sending process.
- Destination Port (16 bits): Port of the receiving process.
- Sequence Number (32 bits): The byte number of the first data byte in this segment (if SYN flag set, then ISN).
- Acknowledgment Number (32 bits): The next byte expected from the other side (cumulative ACK).
- Data Offset (4 bits): Header length in 32‑bit words; minimum 5 (20 bytes).
- Reserved (3 bits): Must be zero.
- Flags (9 bits, but commonly 6 control bits):
- NS (ECN‑nonce) – used for explicit congestion notification.
- CWR (Congestion Window Reduced).
- ECE (ECN Echo).
- URG (Urgent pointer field is valid).
- ACK (Acknowledgment field is valid).
- PSH (Push function – deliver data to application immediately).
- RST (Reset the connection).
- SYN (Synchronize sequence numbers – used during connection setup).
- FIN (No more data from sender – used during teardown).
- Window (16 bits): Advertised receive window size (rwnd) in bytes – flow control.
- Checksum (16 bits): Error detection over the TCP header, data, and a pseudo‑header (similar to UDP). Mandatory in TCP.
- Urgent Pointer (16 bits): Offset from sequence number to urgent data; used only with URG flag.
- Options (variable): Common options include MSS, Window Scaling, SACK, Timestamps, No‑Operation (NOP), and End‑of‑Option (EOL).
2.1 The Pseudo‑Header for Checksum
TCP, like UDP, uses a pseudo‑header containing source IP, destination IP, protocol (6 for TCP), and TCP length. This ensures that the checksum protects against misrouting.
2.2 Flags and Their Uses
- SYN: Synchronize sequence numbers during connection establishment.
- ACK: Indicates the Acknowledgment field is valid; set for almost all segments except the initial SYN.
- FIN: Used to close a connection.
- RST: Aborts the connection.
- PSH: Informs the receiver to push data to the application; rarely used.
- URG: Marks urgent data (obsolete).
📘 3. Sequence Numbers and Acknowledgment Numbers
TCP views data as a stream of bytes. Each byte is assigned a sequence number. The Sequence Number field in a segment is the number of the first byte in that segment. The Acknowledgment Number is the sequence number of the next byte expected by the receiver; all bytes up to this number minus one have been received.
Initial Sequence Number (ISN): Chosen randomly at connection setup to avoid sequence number prediction attacks and to prevent old segments from interfering with new connections. The ISN is exchanged via SYN and SYN‑ACK.
Cumulative ACK: An ACK for byte n acknowledges all bytes up to n-1.
Example: If sender sends 100 bytes with seq=1000, the receiver will ACK with acknowledgment number 1100.
📘 4. Round‑Trip Time (RTT) and Timeout Estimation
TCP sets a retransmission timeout (RTO) to detect packet loss. The RTO is based on measured RTT. TCP maintains:
- SampleRTT: Measured time from sending a segment until receiving an ACK for that segment (only for segments sent once, not retransmitted).
- EstimatedRTT: Smoothed RTT using exponential averaging:
EstimatedRTT = (1 - α) * EstimatedRTT + α * SampleRTT, with α typically 0.125.
- DevRTT: Smoothed deviation:
DevRTT = (1 - β) * DevRTT + β * |SampleRTT - EstimatedRTT|, with β typically 0.25.
- TimeoutInterval (RTO):
TimeoutInterval = EstimatedRTT + 4 * DevRTT.
This provides a conservative timeout that adapts to network variability.
Karn’s algorithm: When a retransmission occurs, ignore SampleRTT for that segment; instead, double the RTO to avoid spurious retransmissions.
📘 5. TCP Reliable Data Transfer – Sender and Receiver Behaviour
TCP’s reliable data transfer is a hybrid of GBN and SR:
- Uses cumulative ACKs (like GBN).
- Sender maintains a sliding window limited by min(cwnd, rwnd).
- On timeout: retransmit the oldest unacknowledged segment, restart timer.
- On receipt of duplicate ACK (same ACK number repeated): a fast retransmit is triggered after 3 duplicate ACKs (similar to SR selective retransmission).
- Receiver: accepts out‑of‑order segments and buffers them, but only ACKs the last in‑order byte (cumulative).
Sender events:
- Data from application: if window allows, segment and send; if not, buffer.
- ACK receipt: if cumulative ACK acknowledges new data, advance send window; if duplicate ACK, increment duplicate ACK count and possibly fast retransmit.
- Timeout: retransmit oldest unacknowledged segment, restart timer.
Receiver events:
- On receipt of in‑order segment: deliver data, send cumulative ACK.
- On receipt of out‑of‑order segment: buffer, send duplicate ACK for last in‑order.
- On receipt of segment with missing data: send ACK for next expected.
📘 6. Flow Control: The Advertised Window (rwnd)
Flow control prevents the sender from overwhelming the receiver’s buffer. The receiver advertises a receive window (rwnd) in every ACK. The sender limits the amount of unacknowledged data to min(cwnd, rwnd). This is a pure end‑to‑end flow control mechanism.
Zero‑window: If rwnd = 0, the sender stops sending. To avoid deadlock, the sender periodically sends a zero‑window probe (a single byte) to check if the window has opened.
📘 7. TCP Buffering and Window Management
TCP uses a send buffer to store data sent but not yet acknowledged, and a receive buffer to store received but not yet read data. The advertised window is computed as: rwnd = receive buffer size - (last byte read - last byte received).
When the application reads data, the window opens, allowing the sender to send more.
📘 8. Advanced Topics: Nagle’s Algorithm, Delayed ACKs, Silly Window Syndrome
8.1 Nagle’s Algorithm
Described in RFC 896, Nagle’s algorithm reduces the number of small packets sent over the network. It works as follows: if there is unacknowledged data in flight, the sender buffers small outgoing segments until either an ACK arrives or the segment reaches the MSS. This helps avoid the small‑packet problem, which can degrade network efficiency.
For interactive applications (e.g., Telnet), Nagle’s algorithm can introduce latency; it can be disabled with the TCP_NODELAY socket option.
8.2 Delayed ACKs
To reduce the number of ACK packets, a receiver may delay sending an ACK for up to 500 ms, hoping to piggyback it on outgoing data. This can reduce overhead but may interfere with the sender’s RTT estimation and cause spurious retransmissions.
8.3 Silly Window Syndrome (SWS)
SWS occurs when a receiver advertises a tiny window (e.g., 1 byte) and the sender sends tiny segments, leading to inefficient use of the network. Both sides take measures:
- Receiver: should not advertise a window smaller than MSS or a certain threshold.
- Sender: should not send small segments (Nagle’s algorithm helps).
These mechanisms improve efficiency, especially for interactive traffic.
📘 9. TCP Options Overview
TCP options extend the header beyond 20 bytes. Common options include:
- MSS (Maximum Segment Size): Maximum payload size the sender can receive; set during handshake.
- Window Scaling (RFC 1323): Allows windows larger than 65535 bytes by scaling the advertised window.
- SACK (Selective Acknowledgment) (RFC 2018): Enables selective retransmission.
- Timestamps (RFC 1323): Used for RTT measurement and PAWS (Protection Against Wrapped Sequence numbers).
- NOP (No‑Operation): Padding option to align other options.
- EOL (End of Options): Marks the end of the options list.
📝 Quiz
Test your understanding with these 35 questions. Answers are hidden below each.
- What is the minimum size of the TCP header (without options)?
Answer
20 bytes (Data Offset = 5).
- What is the maximum size of the TCP header?
Answer
60 bytes (Data Offset = 15, since 15 * 4 = 60).
- Which field in the TCP header is used to identify the sending process?
Answer
Source Port.
- What is the purpose of the Sequence Number field?
Answer
It contains the byte number of the first data byte in the segment.
- What does the Acknowledgment Number indicate?
Answer
The next byte expected from the other side; cumulative acknowledgment.
- What is the role of the SYN flag?
Answer
It is used to synchronize sequence numbers during connection establishment.
- What is the role of the FIN flag?
Answer
It indicates that the sender has no more data to send (used for connection termination).
- What is the role of the ACK flag?
Answer
It indicates that the Acknowledgment field is valid.
- What is the Window field used for?
Answer
It advertises the receiver’s available buffer space (flow control).
- What is the Checksum field used for?
Answer
Error detection over the TCP segment and pseudo‑header.
- What is a pseudo‑header in TCP?
Answer
A header constructed from IP fields (source/dest IP, protocol, TCP length) used in checksum computation, not transmitted.
- What is the Initial Sequence Number (ISN) and how is it chosen?
Answer
The ISN is the starting sequence number; it is chosen randomly to prevent security attacks and confusion with old connections.
- What is a cumulative ACK?
Answer
An ACK that acknowledges all bytes up to the acknowledgment number minus one.
- How does TCP estimate the Round‑Trip Time (RTT)?
Answer
Using SampleRTT (time from send to ACK), then exponential smoothing: EstimatedRTT = (1-α)*EstimatedRTT + α*SampleRTT.
- What is DevRTT and why is it used?
Answer
DevRTT is the smoothed deviation of RTT; it is used to set a conservative timeout (Timeout = EstimatedRTT + 4*DevRTT).
- What is Karn’s algorithm?
Answer
It ignores SampleRTT for retransmitted segments and doubles the RTO to avoid spurious retransmissions.
- What is the default value of α in RTT estimation?
Answer
0.125.
- What is the default value of β in DevRTT estimation?
Answer
0.25.
- How does TCP handle a timeout?
Answer
It retransmits the oldest unacknowledged segment and restarts the timer.
- What happens when TCP receives a duplicate ACK?
Answer
It increments a duplicate ACK counter; after 3 duplicate ACKs, it performs fast retransmit.
- What is the purpose of flow control in TCP?
Answer
To prevent the sender from overwhelming the receiver’s buffer.
- How does the receiver indicate its available buffer space?
Answer
Via the Window field (rwnd) in each ACK.
- What is a zero‑window probe?
Answer
A segment with 1 byte of data sent by the sender when the advertised window is 0 to check if the window has opened.
- What is Nagle’s algorithm?
Answer
An algorithm that reduces small packet transmissions by buffering small data until an ACK arrives or the segment reaches the MSS.
- What is the purpose of delayed ACKs?
Answer
To reduce the number of ACK packets by waiting up to 500 ms to piggyback on outgoing data.
- What is Silly Window Syndrome?
Answer
A condition where the receiver advertises a tiny window, causing the sender to send tiny segments, wasting bandwidth.
- How can Silly Window Syndrome be mitigated on the receiver side?
Answer
By not advertising a window smaller than the MSS or a threshold.
- How can Silly Window Syndrome be mitigated on the sender side?
Answer
By using Nagle’s algorithm and not sending small segments.
- What TCP option is used to support windows larger than 65535 bytes?
Answer
Window Scaling (RFC 1323).
- What TCP option allows selective retransmission?
Answer
SACK (Selective Acknowledgment, RFC 2018).
- What is the MSS option used for?
Answer
To negotiate the maximum segment size that a receiver can accept.
- What is the purpose of the Timestamps option?
Answer
To measure RTT more accurately and to protect against sequence number wrap‑around (PAWS).
- What does the PSH flag do?
Answer
It instructs the receiver to deliver data to the application immediately (rarely used).
- What does the RST flag do?
Answer
It resets (aborts) the connection.
- Is TCP’s checksum mandatory?
Answer
Yes, it is mandatory in TCP.
🛠️ Exercises
Apply your knowledge with these 20 exercises. Solutions are provided below each.
- Exercise 1: Header Size
A TCP segment has Data Offset = 8. How many bytes of options are present?
Solution
Header length = 8 * 4 = 32 bytes. Base header = 20 bytes, so options = 12 bytes.
- Exercise 2: Sequence and ACK Numbers
Host A sends a TCP segment with sequence number 1000, 200 bytes of data. What ACK number should Host B send?
Solution
ACK = 1000 + 200 = 1200.
- Exercise 3: ISN Exchange
In the three‑way handshake, client sends SYN with seq=5000. Server replies with SYN‑ACK with seq=7000, ACK=5001. What is the client’s ACK number in the third segment?
Solution
Client’s ACK = 7001.
- Exercise 4: RTT Estimation
Given SampleRTT values: 100, 120, 110 ms. α=0.125, initial EstimatedRTT=100. Compute EstimatedRTT after each sample.
Solution
After 1: 100. After 2: 0.875*100 + 0.125*120 = 87.5+15=102.5. After 3: 0.875*102.5 + 0.125*110 = 89.6875+13.75=103.4375 ms.
- Exercise 5: Timeout Calculation
If EstimatedRTT=120 ms, DevRTT=30 ms, what is the TimeoutInterval?
Solution
Timeout = 120 + 4*30 = 240 ms.
- Exercise 6: Window Advertising
Receiver buffer size = 8192 bytes, LastByteRead = 2000, LastByteReceived = 5000. What is the advertised window?
Solution
rwnd = buffer size - (LastByteReceived - LastByteRead) = 8192 - (5000-2000) = 8192 - 3000 = 5192 bytes.
- Exercise 7: Zero‑Window
If rwnd=0, can the sender send data? What does it do?
Solution
It stops sending data, but may send a zero‑window probe (1 byte) to check if the window has opened.
- Exercise 8: Nagle’s Algorithm
An interactive application sends 1 byte at a time. How does Nagle’s algorithm affect it?
Solution
It may buffer the byte until an ACK arrives, increasing latency; can be disabled with TCP_NODELAY.
- Exercise 9: Delayed ACK Effect
A receiver delays ACKs for 200 ms. How does this affect the sender’s RTT estimation?
Solution
It increases the measured RTT, which may cause the sender to set a larger timeout, reducing performance.
- Exercise 10: SWS Prevention
Suppose a receiver has only 100 bytes free. It should not advertise a window smaller than what?
Solution
Typically not less than the MSS (e.g., 1460 bytes) to avoid SWS.
- Exercise 11: PSH Flag
When would an application set the PSH flag?
Solution
To force delivery of data to the application immediately, e.g., for interactive telnet or when a message boundary is important.
- Exercise 12: Urgent Pointer
The URG flag is set. What does the Urgent Pointer field contain?
Solution
An offset from the sequence number indicating the end of urgent data.
- Exercise 13: TCP Checksum
Why does TCP include a pseudo‑header in the checksum?
Solution
To protect against misdelivery (e.g., if the packet is delivered to the wrong IP address), binding the segment to the IP layer.
- Exercise 14: Window Scaling
If the window scaling factor is 2, what is the effective window size if the advertised window is 65535?
Solution
Effective = 65535 * 2^2 = 262140 bytes.
- Exercise 15: SACK
How does SACK improve TCP performance?
Solution
It allows the receiver to tell the sender which non‑contiguous blocks of data have been received, enabling selective retransmission of only lost packets.
- Exercise 16: Timestamp Option
What is the Timestamp option used for (two purposes)?
Solution
1. Accurate RTT measurement. 2. PAWS (Protection Against Wrapped Sequence numbers).
- Exercise 17: Send Buffer
A sender has a send buffer of 16 KB. The receiver advertises rwnd=8 KB. How much unacknowledged data can the sender have?
Solution
The sender limits unacknowledged data to min(cwnd, rwnd). Assuming cwnd is large, it can have up to 8 KB.
- Exercise 18: ACK Piggybacking
If data is flowing in both directions, why might an ACK be piggybacked?
Solution
To reduce the number of separate ACK packets, saving bandwidth and improving efficiency.
- Exercise 19: Duplicate ACK and Fast Retransmit
How many duplicate ACKs are needed to trigger fast retransmit?
Solution
3 duplicate ACKs (i.e., 4 total ACKs for the same byte).
- Exercise 20: Data Offset
If the Data Offset field has value 10, what is the header length? How many bytes of options?
Solution
Header length = 10 * 4 = 40 bytes. Options = 40 - 20 = 20 bytes.
📚 Homework
These advanced problems require synthesis, research, and quantitative analysis. Sample answers are provided below.
- Problem 1: TCP Header Design
Explain why the Sequence and Acknowledgment fields are 32 bits each. What is the maximum number of bytes that can be sent before wrap‑around at 1 Gbps?
Sample Answer
32 bits allows up to 2^32 = 4.29 GB. At 1 Gbps, wrap‑around occurs after 4.29 GB / 125 MB/s ≈ 34 seconds. This is why the Timestamp option is used for PAWS.
- Problem 2: RTT Estimation and Retransmission
Derive the formula for TimeoutInterval and explain why 4*DevRTT is used instead of a fixed constant.
Sample Answer
Timeout = EstimatedRTT + 4*DevRTT. This accounts for variability; the factor 4 provides a conservative timeout that reduces spurious retransmissions.
- Problem 3: Flow Control vs. Congestion Control
Distinguish between flow control and congestion control. Why are both needed?
Sample Answer
Flow control prevents receiver overload; congestion control prevents network overload. Both are needed because the receiver and network have separate constraints.
- Problem 4: Nagle’s Algorithm Impact
For a real‑time gaming application, would you enable Nagle’s algorithm? Justify.
Sample Answer
No, Nagle’s algorithm can add latency; gaming requires low latency, so it should be disabled via TCP_NODELAY.
- Problem 5: Delayed ACK Trade‑offs
Discuss the trade‑offs of delayed ACKs. When are they beneficial and when do they cause problems?
Sample Answer
They reduce ACK overhead but can increase RTT estimates and delay recovery from losses. Beneficial for bulk data; problematic for interactive traffic.
- Problem 6: Silly Window Syndrome Avoidance
Describe the receiver‑side and sender‑side mechanisms to avoid Silly Window Syndrome.
Sample Answer
Receiver: do not advertise a window smaller than MSS; sender: use Nagle’s algorithm or clamp small segments.
- Problem 7: Window Scaling Implementation
How does window scaling work? What is the shift value and how is it negotiated?
Sample Answer
Window scaling uses a shift count (0–14) sent as an option. The effective window = advertised window * 2^shift. It is negotiated during the handshake.
- Problem 8: SACK and Performance
Compare TCP with and without SACK under high loss rates. Why does SACK improve performance?
Sample Answer
Without SACK, TCP relies on cumulative ACKs and fast retransmit, which may retransmit more than necessary. SACK allows selective retransmission, improving efficiency.
- Problem 9: TCP Checksum and Offloading
Explain the concept of checksum offloading. What are the benefits and potential issues?
Sample Answer
Offloading moves checksum computation to the NIC, reducing CPU load. Issues include potential mis‑offloads for fragmented packets and debugging complexity.
- Problem 10: Urgent Data in TCP
Describe how urgent data is handled in TCP. Is it still used?
Sample Answer
Urgent data is marked with the URG flag and Urgent Pointer. It is obsolete in modern implementations.
- Problem 11: PSH Flag Usage
Why is the PSH flag rarely used today?
Sample Answer
Modern TCP implementations use buffering and the application can control flushing; PSH is not needed.
- Problem 12: TCP and Network Byte Order
Why are all TCP header fields transmitted in network byte order (big‑endian)? What is the implication for little‑endian hosts?
Sample Answer
To ensure interoperability; little‑endian hosts must convert to network byte order before sending and convert back on receipt.
- Problem 13: Send Buffer Size and Throughput
Derive the relationship between send buffer size, RTT, and achievable throughput. What is the minimum buffer size needed to saturate a 1 Gbps link with RTT=50 ms?
Sample Answer
Throughput ≤ buffer/RTT. To saturate 1 Gbps (125 MB/s) with RTT 0.05 s, buffer ≥ 125 MB/s * 0.05 s = 6.25 MB.
- Problem 14: TCP Options and Middleboxes
Why do some firewalls or load balancers strip TCP options? What are the consequences?
Sample Answer
They may strip options to reduce complexity, but this can break window scaling or SACK, degrading performance.
- Problem 15: PAWS (Protection Against Wrapped Sequence Numbers)
Explain how the Timestamp option enables PAWS. Why is PAWS necessary?
Sample Answer
PAWS allows the receiver to reject old segments that appear to have a valid sequence number after wrap‑around. It uses timestamps to compare, ensuring only recent segments are accepted.
📌 Summary
- TCP provides a reliable, connection‑oriented, full‑duplex byte‑stream service.
- The segment header includes source/destination ports, sequence and acknowledgment numbers, flags, window, checksum, urgent pointer, and options.
- Sequence numbers enable ordered delivery and duplicate detection; acknowledgment numbers are cumulative.
- RTT estimation uses exponential smoothing to adapt to network variability, setting a timeout based on EstimatedRTT and DevRTT.
- TCP’s reliable data transfer combines cumulative ACKs with fast retransmit for efficiency.
- Flow control uses the advertised window to prevent receiver buffer overflow.
- Advanced mechanisms like Nagle’s algorithm, delayed ACKs, and SWS avoidance improve network efficiency.
- Options such as Window Scaling, SACK, and Timestamps extend TCP’s capabilities.
In the next tutorial, we will delve into TCP Reliable Data Transfer and Flow Control in more operational detail, including sender and receiver FSM and timeout management.