📡 Tutorial 6: TCP Fundamentals and Segment Structure (Advanced)

University‑level treatment – COMP347 (TrustOpen University)

Table of Contents

🎯 Learning Objectives

After completing this tutorial, you should be able to:

🔍 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:

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:

Figure 1: TCP Segment Header

 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          |       Destination Port        |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                        Sequence Number                        |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                    Acknowledgment Number                      |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|  Data |           |U|A|P|R|S|F|                               |
| Offset| Reserved  |R|C|S|S|Y|I|            Window             |
|       |           |G|K|H|T|N|N|                               |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|           Checksum            |         Urgent Pointer        |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                    Options (if any)                           |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                             Data                              |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

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

📘 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:

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:

Sender events:

Receiver events:

📘 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:

These mechanisms improve efficiency, especially for interactive traffic.

📘 9. TCP Options Overview

TCP options extend the header beyond 20 bytes. Common options include:

📝 Quiz

Test your understanding with these 35 questions. Answers are hidden below each.

  1. What is the minimum size of the TCP header (without options)?
    Answer20 bytes (Data Offset = 5).
  2. What is the maximum size of the TCP header?
    Answer60 bytes (Data Offset = 15, since 15 * 4 = 60).
  3. Which field in the TCP header is used to identify the sending process?
    AnswerSource Port.
  4. What is the purpose of the Sequence Number field?
    AnswerIt contains the byte number of the first data byte in the segment.
  5. What does the Acknowledgment Number indicate?
    AnswerThe next byte expected from the other side; cumulative acknowledgment.
  6. What is the role of the SYN flag?
    AnswerIt is used to synchronize sequence numbers during connection establishment.
  7. What is the role of the FIN flag?
    AnswerIt indicates that the sender has no more data to send (used for connection termination).
  8. What is the role of the ACK flag?
    AnswerIt indicates that the Acknowledgment field is valid.
  9. What is the Window field used for?
    AnswerIt advertises the receiver’s available buffer space (flow control).
  10. What is the Checksum field used for?
    AnswerError detection over the TCP segment and pseudo‑header.
  11. What is a pseudo‑header in TCP?
    AnswerA header constructed from IP fields (source/dest IP, protocol, TCP length) used in checksum computation, not transmitted.
  12. What is the Initial Sequence Number (ISN) and how is it chosen?
    AnswerThe ISN is the starting sequence number; it is chosen randomly to prevent security attacks and confusion with old connections.
  13. What is a cumulative ACK?
    AnswerAn ACK that acknowledges all bytes up to the acknowledgment number minus one.
  14. How does TCP estimate the Round‑Trip Time (RTT)?
    AnswerUsing SampleRTT (time from send to ACK), then exponential smoothing: EstimatedRTT = (1-α)*EstimatedRTT + α*SampleRTT.
  15. What is DevRTT and why is it used?
    AnswerDevRTT is the smoothed deviation of RTT; it is used to set a conservative timeout (Timeout = EstimatedRTT + 4*DevRTT).
  16. What is Karn’s algorithm?
    AnswerIt ignores SampleRTT for retransmitted segments and doubles the RTO to avoid spurious retransmissions.
  17. What is the default value of α in RTT estimation?
    Answer0.125.
  18. What is the default value of β in DevRTT estimation?
    Answer0.25.
  19. How does TCP handle a timeout?
    AnswerIt retransmits the oldest unacknowledged segment and restarts the timer.
  20. What happens when TCP receives a duplicate ACK?
    AnswerIt increments a duplicate ACK counter; after 3 duplicate ACKs, it performs fast retransmit.
  21. What is the purpose of flow control in TCP?
    AnswerTo prevent the sender from overwhelming the receiver’s buffer.
  22. How does the receiver indicate its available buffer space?
    AnswerVia the Window field (rwnd) in each ACK.
  23. What is a zero‑window probe?
    AnswerA segment with 1 byte of data sent by the sender when the advertised window is 0 to check if the window has opened.
  24. What is Nagle’s algorithm?
    AnswerAn algorithm that reduces small packet transmissions by buffering small data until an ACK arrives or the segment reaches the MSS.
  25. What is the purpose of delayed ACKs?
    AnswerTo reduce the number of ACK packets by waiting up to 500 ms to piggyback on outgoing data.
  26. What is Silly Window Syndrome?
    AnswerA condition where the receiver advertises a tiny window, causing the sender to send tiny segments, wasting bandwidth.
  27. How can Silly Window Syndrome be mitigated on the receiver side?
    AnswerBy not advertising a window smaller than the MSS or a threshold.
  28. How can Silly Window Syndrome be mitigated on the sender side?
    AnswerBy using Nagle’s algorithm and not sending small segments.
  29. What TCP option is used to support windows larger than 65535 bytes?
    AnswerWindow Scaling (RFC 1323).
  30. What TCP option allows selective retransmission?
    AnswerSACK (Selective Acknowledgment, RFC 2018).
  31. What is the MSS option used for?
    AnswerTo negotiate the maximum segment size that a receiver can accept.
  32. What is the purpose of the Timestamps option?
    AnswerTo measure RTT more accurately and to protect against sequence number wrap‑around (PAWS).
  33. What does the PSH flag do?
    AnswerIt instructs the receiver to deliver data to the application immediately (rarely used).
  34. What does the RST flag do?
    AnswerIt resets (aborts) the connection.
  35. Is TCP’s checksum mandatory?
    AnswerYes, it is mandatory in TCP.

🛠️ Exercises

Apply your knowledge with these 20 exercises. Solutions are provided below each.

  1. Exercise 1: Header Size
    A TCP segment has Data Offset = 8. How many bytes of options are present?
    SolutionHeader length = 8 * 4 = 32 bytes. Base header = 20 bytes, so options = 12 bytes.
  2. 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?
    SolutionACK = 1000 + 200 = 1200.
  3. 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?
    SolutionClient’s ACK = 7001.
  4. Exercise 4: RTT Estimation
    Given SampleRTT values: 100, 120, 110 ms. α=0.125, initial EstimatedRTT=100. Compute EstimatedRTT after each sample.
    SolutionAfter 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.
  5. Exercise 5: Timeout Calculation
    If EstimatedRTT=120 ms, DevRTT=30 ms, what is the TimeoutInterval?
    SolutionTimeout = 120 + 4*30 = 240 ms.
  6. Exercise 6: Window Advertising
    Receiver buffer size = 8192 bytes, LastByteRead = 2000, LastByteReceived = 5000. What is the advertised window?
    Solutionrwnd = buffer size - (LastByteReceived - LastByteRead) = 8192 - (5000-2000) = 8192 - 3000 = 5192 bytes.
  7. Exercise 7: Zero‑Window
    If rwnd=0, can the sender send data? What does it do?
    SolutionIt stops sending data, but may send a zero‑window probe (1 byte) to check if the window has opened.
  8. Exercise 8: Nagle’s Algorithm
    An interactive application sends 1 byte at a time. How does Nagle’s algorithm affect it?
    SolutionIt may buffer the byte until an ACK arrives, increasing latency; can be disabled with TCP_NODELAY.
  9. Exercise 9: Delayed ACK Effect
    A receiver delays ACKs for 200 ms. How does this affect the sender’s RTT estimation?
    SolutionIt increases the measured RTT, which may cause the sender to set a larger timeout, reducing performance.
  10. Exercise 10: SWS Prevention
    Suppose a receiver has only 100 bytes free. It should not advertise a window smaller than what?
    SolutionTypically not less than the MSS (e.g., 1460 bytes) to avoid SWS.
  11. Exercise 11: PSH Flag
    When would an application set the PSH flag?
    SolutionTo force delivery of data to the application immediately, e.g., for interactive telnet or when a message boundary is important.
  12. Exercise 12: Urgent Pointer
    The URG flag is set. What does the Urgent Pointer field contain?
    SolutionAn offset from the sequence number indicating the end of urgent data.
  13. Exercise 13: TCP Checksum
    Why does TCP include a pseudo‑header in the checksum?
    SolutionTo protect against misdelivery (e.g., if the packet is delivered to the wrong IP address), binding the segment to the IP layer.
  14. Exercise 14: Window Scaling
    If the window scaling factor is 2, what is the effective window size if the advertised window is 65535?
    SolutionEffective = 65535 * 2^2 = 262140 bytes.
  15. Exercise 15: SACK
    How does SACK improve TCP performance?
    SolutionIt allows the receiver to tell the sender which non‑contiguous blocks of data have been received, enabling selective retransmission of only lost packets.
  16. Exercise 16: Timestamp Option
    What is the Timestamp option used for (two purposes)?
    Solution1. Accurate RTT measurement. 2. PAWS (Protection Against Wrapped Sequence numbers).
  17. 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?
    SolutionThe sender limits unacknowledged data to min(cwnd, rwnd). Assuming cwnd is large, it can have up to 8 KB.
  18. Exercise 18: ACK Piggybacking
    If data is flowing in both directions, why might an ACK be piggybacked?
    SolutionTo reduce the number of separate ACK packets, saving bandwidth and improving efficiency.
  19. Exercise 19: Duplicate ACK and Fast Retransmit
    How many duplicate ACKs are needed to trigger fast retransmit?
    Solution3 duplicate ACKs (i.e., 4 total ACKs for the same byte).
  20. Exercise 20: Data Offset
    If the Data Offset field has value 10, what is the header length? How many bytes of options?
    SolutionHeader 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.

  1. 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 Answer32 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.
  2. Problem 2: RTT Estimation and Retransmission
    Derive the formula for TimeoutInterval and explain why 4*DevRTT is used instead of a fixed constant.
    Sample AnswerTimeout = EstimatedRTT + 4*DevRTT. This accounts for variability; the factor 4 provides a conservative timeout that reduces spurious retransmissions.
  3. Problem 3: Flow Control vs. Congestion Control
    Distinguish between flow control and congestion control. Why are both needed?
    Sample AnswerFlow control prevents receiver overload; congestion control prevents network overload. Both are needed because the receiver and network have separate constraints.
  4. Problem 4: Nagle’s Algorithm Impact
    For a real‑time gaming application, would you enable Nagle’s algorithm? Justify.
    Sample AnswerNo, Nagle’s algorithm can add latency; gaming requires low latency, so it should be disabled via TCP_NODELAY.
  5. Problem 5: Delayed ACK Trade‑offs
    Discuss the trade‑offs of delayed ACKs. When are they beneficial and when do they cause problems?
    Sample AnswerThey reduce ACK overhead but can increase RTT estimates and delay recovery from losses. Beneficial for bulk data; problematic for interactive traffic.
  6. Problem 6: Silly Window Syndrome Avoidance
    Describe the receiver‑side and sender‑side mechanisms to avoid Silly Window Syndrome.
    Sample AnswerReceiver: do not advertise a window smaller than MSS; sender: use Nagle’s algorithm or clamp small segments.
  7. Problem 7: Window Scaling Implementation
    How does window scaling work? What is the shift value and how is it negotiated?
    Sample AnswerWindow scaling uses a shift count (0–14) sent as an option. The effective window = advertised window * 2^shift. It is negotiated during the handshake.
  8. Problem 8: SACK and Performance
    Compare TCP with and without SACK under high loss rates. Why does SACK improve performance?
    Sample AnswerWithout SACK, TCP relies on cumulative ACKs and fast retransmit, which may retransmit more than necessary. SACK allows selective retransmission, improving efficiency.
  9. Problem 9: TCP Checksum and Offloading
    Explain the concept of checksum offloading. What are the benefits and potential issues?
    Sample AnswerOffloading moves checksum computation to the NIC, reducing CPU load. Issues include potential mis‑offloads for fragmented packets and debugging complexity.
  10. Problem 10: Urgent Data in TCP
    Describe how urgent data is handled in TCP. Is it still used?
    Sample AnswerUrgent data is marked with the URG flag and Urgent Pointer. It is obsolete in modern implementations.
  11. Problem 11: PSH Flag Usage
    Why is the PSH flag rarely used today?
    Sample AnswerModern TCP implementations use buffering and the application can control flushing; PSH is not needed.
  12. 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 AnswerTo ensure interoperability; little‑endian hosts must convert to network byte order before sending and convert back on receipt.
  13. 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 AnswerThroughput ≤ buffer/RTT. To saturate 1 Gbps (125 MB/s) with RTT 0.05 s, buffer ≥ 125 MB/s * 0.05 s = 6.25 MB.
  14. Problem 14: TCP Options and Middleboxes
    Why do some firewalls or load balancers strip TCP options? What are the consequences?
    Sample AnswerThey may strip options to reduce complexity, but this can break window scaling or SACK, degrading performance.
  15. Problem 15: PAWS (Protection Against Wrapped Sequence Numbers)
    Explain how the Timestamp option enables PAWS. Why is PAWS necessary?
    Sample AnswerPAWS 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

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.

COMP347 – Computer Networks (Rev. 10) · TrustOpen University · Based on Kurose & Ross, Computer Networking: A Top‑Down Approach, 9th ed. (2025).