Transmission Control Protocol (TCP)
TCP is basically designed to the TCP/IP suite and hence the name of the model. When application layer sending a large amount of data, it sends the data to transport layer for TCP or UDP to transport it across the network. TCP first sets up a connection between the source and the destination in a process called three-way handshake. Then it breaks down the data into segments, adds a header to each segment and sends them to the Internet layer.
The TCP header is 20 to 24 bytes in size and the format is shown in Figure 1-9. It is not necessary to remember all fields or their size but most of the fields are discussed below.
Figure 1-9 TCP header
When the Application layer sends data to the transport layer, TCP sends the data across using the following sequence:
TCP uses a process called three-way handshake to establish a connection with the destination. The three-way handshake uses the SYN and ACK flags in the Code Bits section of the header. This process is necessary to initialize the sequence and acknowledgment number fields. These fields are important for TCP and will be discussed in the following.
Figure 1-10 TCP three-way handshake
As shown in Figure 1-10, the source starts the three-way handshake by sending a TCP header to the destination with the SYN flag set. The destination responds back with the SYN and ACK flag sent. Examine in the figure that destination uses the received sequence number plus 1 as the Acknowledgement number. This is because it is assumed that 1 byte of data was contained in the exchange. In the final step, the source responds back with only the ACK bit set. After this, the data flow can commence.
The size of data that can be transmitted across in a single Internet layer PDU is limited by the protocol used in that layer. This limit is called the maximum transmission unit (MTU). The application layer may send data much larger than this limit; hence TCP has to break down the data into smaller chunks called segments. Each segment is limited to the MTU in size. Sequence numbers are used to identify each byte of data. The sequence number in each header signifies the byte number of the first byte in that segment.
The source starts sending data in groups of segments. The Window bit in the header determines the number of segments that can be sent at a time. This is done to avoid irresistible the destination. At the start of the session the window in small but it increases over time. The destination host can also decrease the window to slow down the flow. Hence the window is called the sliding window. When the source has sent the number of segments allowed by the window, it cannot send any further segments till an acknowledgment is received from the destination. Figure 1-11 shows how the window increases during the session. Notice the Destination host increasing the Window from 1000 to 1100 and then to 1200 when it sends an ACK back to the source.
Figure 1-11 TCP Sliding Window and Reliable delivery
Reliable Delivery with Error recovery
When the destination receives the last segment in the agreed window, it has to send an acknowledgment to the source. It sets the ACK flag in the header and the acknowledgment number is set to the sequence number of the next byte expected. If the destination does not receive a segment, it does not send an acknowledgment back. This tells the source that some segments have been lost and it will retransmit the segments. Figure 1-13 shows how windowing and acknowledgment are used by TCP. Notice that when the source does not receive acknowledgment for the segment with sequence number 2000, it retransmits the data. Once it receives the acknowledgment, it sends the next sequence according to the window size.
TCP transmits data in the order it is received from the application layer and uses the sequence number to mark the order. The data may be received at the destination in the wrong order due to network conditions. Thus TCP at the destination orders the data according to the sequence number before sending it to the application layer at its end. This order delivery is part of the benefit of TCP and one of the purposes of the Sequence Number.
After all, data has been transferred, the source initiates a four-way handshake to close the session. To close the session, the FIN and ACK flags are used.