2.3. TCP protocol

2.3.1. Introduction

TCP (Transmission Control Protocol, Transmission Control Protocol) is a connection-oriented, reliable, byte stream-based transport layer communication protocol, defined by RFC 793.

2.3.1.1. Three-way handshake

Three-way handshake (Three-Way Handshake) means that when establishing a TCP connection, the client and the server need to send a total of 3 packets to confirm the establishment of the connection.

In the first handshake, the client sets the flag SYN to 1, randomly generates a value seq=s, and sends the data packet to the server. The client enters the SYN_SENT state and waits for the server to confirm.

After the second handshake server receives the data packet, the flag bit SYN=1 knows that the client requests to establish a connection, and the server side sets both flag bits SYN and ACK to 1, ack=s+1, and randomly generates a value seq=k , and send the data packet to the client to confirm the connection request, and the server enters the SYN_RCVD state.

After receiving the confirmation in the third handshake, the client checks whether the ack value is s+1 and whether the ACK flag is 1. If it is correct, the flag ACK is set to 1, ack=k+1, and the packet is sent. To the server, the server checks whether the ack value is k+1 and whether the ACK flag is 1. If it is correct, the connection is established successfully. The client and server enter the ESTABLISHED state and complete the three-way handshake.

2.3.1.2. Four-way handshake

Four-Way Wavehand means that when disconnecting a TCP connection, the client and server need to send a total of 4 packets to confirm the disconnection of the connection.

The first wave of the client sends a FIN to close the data transfer from the client to the server, and the client enters the FIN_WAIT_1 state.

After receiving the FIN for the second wave, the server sends an ACK to the client, confirming that the serial number is the received serial number + 1, and the server enters the CLOSE_WAIT state.

The third wave of the server sends a FIN to close the data transfer from the server to the client, and the server enters the LAST_ACK state.

After the fourth wave of the client receives the FIN, the client enters the TIME_WAIT state, and then sends an ACK to the server, confirming that the serial number is the received serial number + 1, the server enters the CLOSED state, and completes the four waves.

2.3.2. Congestion Control

Congestion refers to the phenomenon that the number of packets in the network is too large, so that the server cannot process them in time, which causes the performance of this part or the entire network to degrade.

TCP adopts congestion control algorithm to reduce or avoid the occurrence of congestion. There are many implementations of TCP congestion algorithm, including Tahoe, Reno, NewReno, Vegas, Hybla, BIC, CUBIC, SACK, Westwood, PRR, BBR, etc.