First, let’s familiarize ourselves with the classic TCP/IP model.

Their roles are as follows:

1) Network interface layer: the main function is to bind IP address to physical address of computer, and realize the conversion of high and low potential between binary stream and computer hardware. 2) Network layer: the main function is to link two physical machines through IP addresses to realize the transmission of IP packets; 3) Transport layer: The peer entities on the source host and target host can communicate with each other. Two different quality-of-service protocols are defined at the transport layer. Transmission Control Protocol (TCP) and User Datagram Protocol (UDP). ; 4) Application layer: it is responsible for transmitting various final forms of data and is the layer directly dealing with users. Typical protocols are HTTP and FTP, etc.

Today let’s focus on the main TCP protocols in the TCP model.

In computer communication, TCP protocol is an essential link in order to realize reliable network communication. So how does TCP enable reliable communication? This starts with the classic three-way handshake.

A three-way handshake means that the client and server must send at least three packets (or more if the network times out) to establish a TCP connection.





In addition, the ack sequence number refers to the SEQ of the next received packet. The receiver uses SEQ and LEN to confirm whether the packet is valid, whether it is discarded, whether it is put into the normal packet sequence or into the out-of-order packet sequence.

So let’s see why TCP is a three-way handshake, not two, one or four.

First handshake:

A sends a packet to B. It is obviously impossible to establish a connection with only one handshake, since A does not even know the authenticity of B.

Second handshake:

B Sends a connection confirmation request after receiving the connection request. If the connection is successfully established after two handshakes, a problem occurs: A sends a request packet to B. Due to network reasons, B does not receive the packet for a long time. After receiving the packet, B returns a packet to A. In this case, a will drop the packet due to time timeout. In the case of a two-handshake, user B successfully establishes the connection and waits for user A to transmit data. This can lead to a waste of server resources.

Third handshake:

After receiving the connection confirmation request from USER B, user A verifies that the data is correct and sends a request to user A. User B verifies that the data is correct after receiving the request. If yes, the connection is successfully established.

The three-way handshake can avoid packet loss caused by network timeout. The server and client must receive correct ACK messages respectively to indicate that the connection is established. If no ACK message is received, the timeout retransmission mechanism is enabled. Of course, if the network is really poor, so that the connection cannot be established, it is not possible to always retransmit. The operating system has a field for setting retransmission times to avoid invalid retransmissions.

Why not shake hands four times:

In this case, the server sends ACK and SYN separately, so it is four times. Later, it is optimized, so it is three times.

Waved.

When the client and server through three handshakes to establish a TCP connection, you can carry out data communication, communication is finished, the two sides can be disconnected, disconnected will involve our four waves. Let’s take a quick look at TCP’s four waves.

First wave:

The client sends a packet with the FIN flag bit set to 1 and SEQ set to M.

Second wave:

After receiving the FIN packet from the client, the server sends an ACK packet with the ACK value m+1 to the client. The server tells the client that I “agree” to your request. At this point, the client cannot send data and the server cannot receive any more data. The server can also send data to the client.

Third wave:

The server sends a FIN packet to the client to close the connection and enters the LAST_ACK state.

Fourth wave:

The client receives the FIN packet from the server and sends an ACK packet to the server. Then the client enters the TIME_WAIT state. After receiving the packet segment from the client, the server closes the connection. If the client does not receive a reply after waiting for 2MSL, it indicates that the Server is shut down properly.

The reason for the quadruple wave is that the TCP connection is full duplex and needs to be closed in both directions, so both directions need to send a close request and a confirmation request.

The following describes an important mechanism of TCP reliability, timeout retransmission.

Timeout retransmission is an important mechanism for TCP to ensure data reliability. The principle is that a timer is started after a certain data is sent. If no ACK packet is received within a certain period of time, the data is resend until the data is successfully sent. Three-way handshake, normal data transfer, and any timeout during the four-way handshake will trigger retransmission.

So how does TCP define timeouts, and that brings up two new things. One is RTT and the other is RTO. Those of you who are interested can go down and learn about it, but I won’t go into details here.

Conclusion: TCP protocol is a very complex protocol, this article is only a simple introduction to a very small point of knowledge due to the length of the reason, not a deep level of introduction of TCP, if you have more doubts, welcome to leave a comment.