乐闻世界logo
搜索文章和话题

What fields does the TCP header contain? What is the purpose of each field?

2月21日 17:07

TCP Header Structure Explained

TCP header is the core part of TCP protocol, containing key information for controlling TCP connections and data transmission.

TCP Header Fields

1. Source Port (16 bits)

  • Purpose: Identify the port number of the sender
  • Range: 0-65535
  • Example: HTTP client uses random port, server uses port 80

2. Destination Port (16 bits)

  • Purpose: Identify the port number of the receiver
  • Range: 0-65535
  • Example: HTTP server listens on port 80

3. Sequence Number (32 bits)

  • Purpose: Identify the position of data in the stream
  • Feature: Each byte has a sequence number, sequence number is the byte offset
  • Initial Value: Randomly generated when connection is established
  • Purpose: Ensure data arrives in order, detect lost packets

4. Acknowledgment Number (32 bits)

  • Purpose: Sequence number of the next byte expected to receive
  • Condition: Valid only when ACK flag is 1
  • Feature: Cumulative acknowledgment, all bytes before acknowledgment number have been received

5. Data Offset (4 bits)

  • Purpose: Indicate the length of TCP header (in 32-bit words)
  • Range: 5-15, corresponding to 20-60 bytes
  • Calculation: Header length = Data Offset × 4 bytes

6. Reserved (6 bits)

  • Purpose: Reserved for future use
  • Value: Must be set to 0

7. Flags (6 bits)

  • URG (Urgent): Urgent pointer is valid
  • ACK (Acknowledgment): Acknowledgment number is valid
  • PSH (Push): Receiver should deliver data to application layer as soon as possible
  • RST (Reset): Reset connection
  • SYN (Synchronize): Synchronize sequence numbers, used to establish connection
  • FIN (Finish): Sender has finished sending data, used to close connection

8. Window Size (16 bits)

  • Purpose: Advertise available buffer size of receiver
  • Unit: Bytes
  • Purpose: Flow control, prevent sending too fast causing buffer overflow

9. Checksum (16 bits)

  • Purpose: Detect errors in header and data during transmission
  • Calculation Range: TCP header, data and pseudo-header
  • Pseudo-header: Contains source IP, destination IP, protocol number, TCP length

10. Urgent Pointer (16 bits)

  • Purpose: Point to the last byte of urgent data
  • Condition: Valid only when URG flag is 1
  • Usage: Used for urgent data transmission

11. Options (Variable Length)

  • Purpose: Provide additional functions
  • Common Options:
    • MSS (Maximum Segment Size)
    • Window Scale Factor
    • Timestamp
    • SACK (Selective Acknowledgment)

Header Structure Diagram

shell
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 | Padding | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Data | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  • What are the minimum and maximum lengths of TCP header?
  • Why do we need pseudo-header to calculate checksum?
  • What is the purpose of window scale factor?
标签:TCP