TCP Segment
TCP Segment
A segment is the basic packet used by TCP at the transport layer. TCP receives a stream of bytes from the application layer, divides the stream into manageable units, and adds a TCP header to each unit. The resulting packet is called a TCP segment.
A TCP segment has two main parts:
┌─────────────────────────────────────────────┐ │ TCP Header │ │ 20–60 bytes │ ├─────────────────────────────────────────────┤ │ Application Data │ │ Variable │ └─────────────────────────────────────────────┘
The TCP header is:
- 20 bytes minimum when there are no options
- 60 bytes maximum when options are present
Up to 40 bytes can be used for options and padding.
1. Source Port Address
The source port address is a 16-bit field.
It identifies the port number of the application process that is sending the segment.
For example:
Client application Port = 50000 ↓ TCP segment Source Port = 50000
This allows the receiving TCP to identify the source process.
2. Destination Port Address
The destination port address is also a 16-bit field.
It identifies the port number of the application process that should receive the segment.
For example, for an HTTP connection:
Source Port = 50000 Destination Port = 80
Here, port 80 identifies the destination application.
Thus, the source and destination port numbers provide process-to-process communication.
3. Sequence Number
The sequence number is a 32-bit field.
TCP is a stream-oriented protocol, so every byte of data transmitted in a connection is numbered.
The sequence number in a TCP segment identifies:
The number assigned to the first data byte contained in that segment.
For example, suppose the data is divided into five segments:
| Segment | Sequence Number | Data Byte Range |
|---|---|---|
| Segment 1 | 10,001 | 10,001–11,000 |
| Segment 2 | 11,001 | 11,001–12,000 |
| Segment 3 | 12,001 | 12,001–13,000 |
| Segment 4 | 13,001 | 13,001–14,000 |
| Segment 5 | 14,001 | 14,001–15,000 |
Notice that the sequence number identifies the first byte, not the segment number.
During connection establishment, each party generates an Initial Sequence Number (ISN), normally using a random number generator.
4. Acknowledgment Number
The acknowledgment number is also a 32-bit field.
It specifies:
The byte number that the receiver expects to receive next.
For example, suppose a receiver has successfully received bytes through byte 11,000.
It sends:
ACK = 11,001
This means:
"I have received everything up to byte 11,000, and I expect byte 11,001 next."
Therefore:
Last byte successfully received = 11,000 Next byte expected = 11,001 Acknowledgment number = 11,001
TCP acknowledgments are cumulative.
Piggybacking
TCP can carry acknowledgment information along with data in the same segment. This is called piggybacking.
TCP Segment ┌──────────────┬──────────────┐ │ Data + ACK │ Application │ │ information │ Data │ └──────────────┴──────────────┘
5. Header Length (HLEN)
The header length field is 4 bits.
It indicates the length of the TCP header in units of 4-byte words.
The TCP header can be between 20 and 60 bytes.
Therefore:
- Minimum HLEN = 5
- Maximum HLEN = 15
So:
| HLEN | Header size |
|---|---|
| 5 | 20 bytes |
| 10 | 40 bytes |
| 15 | 60 bytes |
6. Control Flags
They are:
| Flag | Meaning |
|---|---|
| URG | Urgent pointer is valid |
| ACK | Acknowledgment field is valid |
| PSH | Request for push |
| RST | Reset the connection |
| SYN | Synchronize sequence numbers |
| FIN | Terminate the connection |
One or more flags can be set at the same time.
These flags are used for:
- connection establishment,
- connection termination,
- connection abortion,
- acknowledgment,
- flow control,
- controlling the mode of data transfer.
For example, SYN is associated with establishing a TCP connection, while FIN is used for terminating a connection.
7. Window Size
The window size field is 16 bits.
It specifies the amount of data, in bytes, that the receiver is currently willing to accept.
The maximum value represented by 16 bits is:
Therefore, the maximum window size in this field is:
65,535 bytes
This value is normally called the:
Receiving Window (rwnd)
The receiver determines this value, and the sender must obey it.
Example
Suppose the receiver advertises:
Window size = 10,000 bytes
The sender should not have more than the allowed amount of unacknowledged data outstanding according to that advertised receiving window.
This mechanism is important for TCP flow control.
8. Checksum
The checksum is a 16-bit field.
It is used for error detection.
TCP checksum calculation is similar to the checksum calculation used in UDP, but there is an important difference:
TCP checksum is mandatory.
TCP uses a pseudoheader as part of the checksum calculation.
The pseudoheader includes information such as:
- source IP address,
- destination IP address,
- protocol value,
- TCP total length.
For TCP, the protocol field has the value:
6
This helps ensure that the segment belongs to TCP and is associated with the correct source and destination information.
9. Urgent Pointer
The urgent pointer is a 16-bit field.
It is valid only when the URG flag is set.
It is used when a TCP segment contains urgent data.
The urgent pointer provides a value that is added to the sequence number to determine the number of the last urgent byte in the data portion of the segment.
10. Options
The TCP header can contain up to 40 bytes of options.
Therefore, the header size can increase from:
20 bytes → 60 bytes
when options are present.
Options provide additional information and capabilities beyond the basic TCP header.
TCP Segment Header — At a Glance
┌───────────────────┬───────────────────┐ │ Source Port │ Destination Port │ │ 16 bits │ 16 bits │ ├───────────────────────────────────────┤ │ Sequence Number │ │ 32 bits │ ├───────────────────────────────────────┤ │ Acknowledgment Number │ │ 32 bits │ ├──────────┬──────────┬─────────────────┤ │ HLEN │ Reserved │ Control Flags │ ├──────────┴──────────┴─────────────────┤ │ Window Size │ │ 16 bits │ ├───────────────────────────────────────┤ │ Checksum │ │ 16 bits │ ├───────────────────────────────────────┤ │ Urgent Pointer │ │ 16 bits │ ├───────────────────────────────────────┤ │ Options + Padding │ │ 0–40 bytes │ ├───────────────────────────────────────┤ │ Data │ └───────────────────────────────────────┘
Encapsulation of a TCP Segment
TCP receives data from the application layer and creates a segment.
The encapsulation process is:
Application Layer │ │ Application data ↓ TCP │ │ TCP Header + Data ↓ TCP Segment │ ↓ Network Layer │ │ IP Header + TCP Segment ↓ IP Datagram │ ↓ Data-Link Layer │ │ Frame ↓ Physical Network
So, the hierarchy is:
Application data → TCP segment → IP datagram → Data-link frame
At the receiving side, the reverse process occurs:
Frame ↓ IP Datagram ↓ TCP Segment ↓ Application Data
TCP removes its header, processes the sequence and acknowledgment information, performs the necessary error/flow-control operations, and delivers the appropriate byte stream to the application.
Most Important Points to Remember
For students, the following are the key points about a TCP segment:
- A TCP packet is called a segment.
- The TCP header is 20–60 bytes.
- Source and destination ports identify the communicating processes.
- Sequence number identifies the first byte in the segment.
- Acknowledgment number identifies the next byte expected.
- TCP acknowledgments are cumulative.
- HLEN specifies the TCP header length in 4-byte words.
- TCP has six important control flags: URG, ACK, PSH, RST, SYN, FIN.
- Window size supports flow control and can represent up to 65,535 bytes in the basic field.
- Checksum is mandatory in TCP.
- Urgent pointer is meaningful when URG is set.
- TCP segments are encapsulated inside IP datagrams before transmission.
Comments
Post a Comment