# TCP Header
## Structure of the TCP Header
The TCP header precedes the data payload in every TCP segment. The minimum size of the TCP header is 20 bytes, and it can be up to 60 bytes when options are included. Understanding the TCP header is essential for understanding how TCP implements its reliability, flow control, and connection management features.
## TCP Header Fields
The source port field is 16 bits wide and identifies the application on the sending host. Port numbers range from 0 to 65535. Client applications typically use ephemeral port numbers above 1024 that are assigned dynamically. Server applications listen on well-known port numbers below 1024.
The destination port field is 16 bits wide and identifies the application on the receiving host. Common destination ports include 80 for HTTP, 443 for HTTPS, 22 for SSH, and 25 for SMTP.
The sequence number field is 32 bits wide. In a data segment, it contains the sequence number of the first byte of data in the segment. The sequence number identifies the position of this data in the overall data stream being transmitted. Initial sequence numbers are chosen randomly to prevent security attacks that exploit predictable sequence numbers.
The acknowledgment number field is 32 bits wide. When the ACK flag is set, this field contains the sequence number of the next byte the sender expects to receive from the other end, effectively acknowledging all bytes up to that sequence number.
The data offset field is 4 bits wide and indicates the length of the TCP header in 32-bit words, which is necessary because the header length varies when options are present.
The reserved field is 3 bits and must be zero.
The flags field contains 9 one-bit flags that control TCP behavior. URG indicates the urgent pointer field is significant. ACK indicates the acknowledgment field is significant. PSH requests that buffered data be pushed to the receiving application. RST resets the connection. SYN synchronizes sequence numbers during connection establishment. FIN indicates the sender has finished sending data.
The window size field is 16 bits and specifies the number of bytes the sender is willing to receive, which is the receive window used for flow control.
The checksum field is 16 bits and provides error detection for the TCP header and data.
The urgent pointer field is 16 bits and is valid only when the URG flag is set. It points to the last byte of urgent data.
The options field can contain various optional TCP extensions including Maximum Segment Size negotiation, window scale factor, selective acknowledgment, and timestamps.Back to Subject