Detailed Explanation of TCP Sequence Number and Acknowledgment Mechanism

Detailed Explanation of TCP Sequence Number and Acknowledgment Mechanism

Description
The TCP sequence number and acknowledgment mechanism is the core foundation of TCP reliable transmission. Each TCP segment contains Sequence Number and Acknowledgment Number fields, which are used to achieve ordered data transmission, packet loss detection, and retransmission. Understanding this mechanism is crucial for mastering TCP's reliability.

Step-by-Step Explanation of Key Concepts

1. Role of the Sequence Number

  • Definition: The sequence number is a 32-bit unsigned integer that identifies the number of the first data byte in the current segment.
  • Initial Value: During the TCP three-way handshake, both parties randomly generate an Initial Sequence Number (ISN) to prevent old connection packets from interfering with the new connection.
  • Increment Rule: The sequence number increases by 1 for every byte of data sent. For example, if the initial sequence number is 1000 and 500 bytes of data are sent, the sequence number for the next segment will be 1500.
  • Core Functions:
    • Data Ordering: The receiver reassembles out-of-order segments into ordered data based on sequence numbers.
    • Loss Detection: If the receiver finds a gap in the sequence numbers, it can infer that intermediate data is lost.

2. Role of the Acknowledgment Number

  • Definition: The acknowledgment number is a 32-bit unsigned integer that indicates the next sequence number the receiver expects to receive. Its meaning is "all data before this acknowledgment number has been correctly received."
  • Generation Rule: If the receiver has received data with sequence numbers 1000-1499, it sends an acknowledgment number of 1500, indicating it expects data starting from 1500.
  • Cumulative Acknowledgment: TCP uses a cumulative acknowledgment mechanism, meaning the acknowledgment number confirms that all data with smaller sequence numbers has been received. For example, an acknowledgment number of 1500 confirms data with sequence numbers 1000-1499, eliminating the need to acknowledge each segment individually.

3. Collaborative Workflow of Sequence and Acknowledgment Numbers
Illustrated with a specific example (assuming initial sequence numbers: Client=1000, Server=5000):

  • Step 1: Client sends data
    • Client sends a segment: Sequence Number=1000, Data Length=200 bytes.
    • After sending, the client's unacknowledged sequence number range is 1000~1199.
  • Step 2: Server acknowledges receipt
    • After receiving the data, the server checks that the sequence numbers are consecutive (1000-1199) and replies with an acknowledgment segment: Acknowledgment Number=1200 (i.e., the expected sequence number for the next byte).
    • Simultaneously, the server may include data (e.g., a response) with its sequence number starting from its initial sequence number (5000).
  • Step 3: Client processes the acknowledgment
    • Upon receiving acknowledgment number 1200, the client knows that data 1000-1199 has been received and removes it from the send buffer.
    • If no acknowledgment is received before a timeout, the client triggers a retransmission.

4. Conditions Triggering Packet Loss and Retransmission

  • Timeout Retransmission: The sender starts a timer for each sent but unacknowledged segment. If no acknowledgment is received before the timer expires, the segment is retransmitted.
  • Fast Retransmission: When the receiver receives out-of-order data (e.g., sequence number 1200 arrives before 1000), it repeatedly sends the most recent valid acknowledgment number (e.g., sending acknowledgment number 1000 multiple times). Upon receiving three duplicate acknowledgments, the sender immediately retransmits the missing data.

5. Practical Packet Analysis (Wireshark Example Logic)
Assuming a simplified capture of a communication exchange:

  1. Client → Server: Seq=1000, Len=300, Ack=5000 (acknowledging the server's previous data)
  2. Server → Client: Seq=5000, Len=100, Ack=1300 (acknowledging client data 1000-1299)
  3. Client → Server: Seq=1300, Len=200, Ack=5100 (acknowledging server data 5000-5099)
  • Through the increment of sequence and acknowledgment numbers, both parties can synchronize their data sending and receiving progress.

6. Handling Special Scenarios

  • Out-of-Order Segments: The receiver temporarily stores out-of-order data in a buffer and delivers it to the application layer in order once the missing data arrives.
  • Duplicate Segments: The receiver identifies duplicate data via sequence numbers and discards it directly.
  • Synergy with Window Mechanism: The acknowledgment number is accompanied by the window size, notifying the sender of the amount of data that can continue to be sent, preventing receiver buffer overflow.

Summary
The TCP sequence number and acknowledgment mechanism ensure reliable and ordered data transmission by numbering data bytes, using cumulative acknowledgments, and implementing retransmission strategies. This mechanism forms the basis for TCP's flow control, congestion control, and other features, and is also a frequently tested fundamental principle in technical interviews.