askvity

How is TCP structured?

Published in Networking Protocols 4 mins read

TCP (Transmission Control Protocol) is structured around encapsulating data into segments, each with a header that provides control and sequencing information. This allows for reliable, ordered delivery of data.

TCP Header Structure

TCP wraps each data packet with a header. This header contains mandatory and optional fields that guide the data's transmission and reception. The standard header size is 20 bytes.

Mandatory Fields (20 Bytes)

These fields are always present in the TCP header:

Field Size (Bytes) Description
Source Port 2 The port number of the sending application on the sending host.
Destination Port 2 The port number of the receiving application on the receiving host.
Sequence Number 4 The sequence number of the first data byte in the TCP segment. If the SYN flag is set, this is the initial sequence number (ISN), and the first data byte is ISN+1.
Acknowledgment Number 4 If the ACK flag is set, this field contains the next sequence number the sender expects to receive from the receiver. Acknowledges previously received data.
Data Offset 1 (4 bits) Indicates the size of the TCP header in 32-bit words. This field determines where the data begins. Minimum value is 5 (5 x 4 = 20 bytes) and maximum is 15.
Reserved 1 (3 bits) Reserved for future use. Must be zero.
Flags 1 (9 bits) Control flags that indicate the purpose and state of the TCP segment (e.g., SYN, ACK, FIN, RST, PSH, URG, ECE, CWR, NS).
Window Size 2 The size of the receive window, specifying the number of bytes the sender is willing to accept. Used for flow control.
Checksum 2 An error-detection code calculated over the header and data. Used to verify the integrity of the segment.
Urgent Pointer 2 If the URG flag is set, this field indicates the offset from the current sequence number where urgent data begins.

Details on Key Fields:

  • Sequence Number: Ensures that the data is reassembled in the correct order at the receiving end. TCP uses sequence numbers to track each byte of data transmitted.

  • Acknowledgment Number: Provides feedback to the sender about which data has been successfully received. This enables reliable transmission.

  • Flags: Control the connection process.

    • SYN: Initiates a connection (synchronize).
    • ACK: Acknowledges received data.
    • FIN: Terminates a connection (finish).
    • RST: Resets a connection.
    • PSH: Push data immediately.
    • URG: Indicates urgent data.

Options (Variable Length, present only if the Data Offset > 5)

Following the mandatory fields, the TCP header may include optional fields. These options are included to enable features such as increased buffer sizes, window scaling, or timestamps. The maximum total TCP header size, including options, is 60 bytes. If Options are present, Padding is used to ensure that the TCP header ends on a 32-bit boundary.

Example:

Imagine sending a message "Hello" using TCP. The TCP layer would encapsulate "Hello" within a TCP segment. The header includes the source and destination port numbers, sequence numbers to track the position of "Hello" in the overall communication stream, acknowledgment numbers, and flags that manage the connection state, as well as a checksum for error checking.

In summary, the TCP structure is designed for reliable and ordered data transmission, using a header filled with control information to manage the flow, acknowledgment, and integrity of the data.

Related Articles