Sub topic 4
The TCP Segment: Header Fields, Sequence and Acknowledgement Numbers
Assume you know the layered network model, IP addresses, ports, and round-trip time (RTT). This chapter focuses on how TCP numbers bytes and uses acknowledgements to provide reliable, ordered delivery.
What a TCP segment contains
A TCP segment is the transport-layer unit carried inside an IP packet. It contains a header and, usually, application data.
| Field | Purpose |
|---|---|
| Source port | Identifies the sending application |
| Destination port | Identifies the receiving application |
| Sequence number | Numbers the first data byte in this segment |
| Acknowledgement number | Identifies the next byte expected |
| Header length | Gives the size of the TCP header |
| Control flags | Indicate operations such as setup, acknowledgement, and termination |
| Receive window | Reports how much more data the receiver can accept |
| Checksum | Detects corruption in the header and data |
| Urgent pointer | Supports urgent data when the URG flag is set |
| Options | Add features such as maximum segment size and timestamps |
The sequence and acknowledgement numbers are the most important fields for following TCP data transfer.
Sequence numbers count bytes
TCP assigns a sequence number to every byte in a data stream. The sequence number in a segment identifies the first byte carried by that segment.
Suppose a sender starts with sequence number 1000 and sends 500 bytes. The segment carries bytes 1000 through 1499. The next segment begins at sequence number 1500.
TCP acknowledgements are cumulative. An acknowledgement number of 1500 means:
- Bytes before 1500 have arrived in order.
- The receiver expects byte 1500 next.
The acknowledgement does not mean that byte 1500 has already arrived.
first_sequence = 1000
data_length = 500
last_sequence = first_sequence + data_length - 1
next_expected = first_sequence + data_length
print(f"Data covers bytes {first_sequence} through {last_sequence}")
print(f"Receiver acknowledges with ACK {next_expected}")A segment can carry data and acknowledge data received in the opposite direction. TCP therefore supports full-duplex communication.
Flags and special sequence numbers
Common TCP control flags include:
- SYN: starts a connection and synchronizes an initial sequence number.
- ACK: makes the acknowledgement field valid.
- FIN: requests an orderly connection close.
- RST: immediately resets a connection.
- PSH: asks the receiver to deliver buffered data to the application.
- URG: indicates urgent data.
SYN and FIN each consume one sequence-number position even though they do not carry application data. This rule is important in handshake and termination calculations.
Reading a segment exchange
Consider a client whose initial sequence number is 5000 and a server whose initial sequence number is 9000.
- The client sends
SYN, SEQ=5000. - The server sends
SYN+ACK, SEQ=9000, ACK=5001. - The client sends
ACK, SEQ=5001, ACK=9001.
The acknowledgements increase by one because each SYN consumes one sequence number. If the client then sends 300 bytes, its data segment starts at sequence number 5001, and the server acknowledges it with ACK=5301.
A receiver may obtain segments out of order, but it normally acknowledges the next missing byte. If it receives bytes beginning at 5301 while still missing byte 5001, it can send a duplicate acknowledgement for 5001. Several duplicate acknowledgements can signal a loss and trigger fast retransmission.
Exercise
A TCP segment has SEQ=2000 and carries 750 bytes. What acknowledgement number should the receiver send if all bytes arrive in order?
Show answer
The bytes are 2000 through 2749, so the next expected byte is 2750. The acknowledgement number is ACK=2750.