This is the 12th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

During the development of daily, send an asynchronous request is often do, it is usually an HTTP request, and it is the HTTP request through a TCP channel to pass between the client and the server, so the establishment of the TCP channel is the precondition of communication, this article is ready to talk about the establishment of the TCP channel, Aka the so-called “three handshakes”, so cut the crap and do it!

What is the TCP

To understand the three-way handshake, we need to understand THE Transmission Control Protocol (TCP), a reliable, connection-oriented transport-layer communication Protocol based on byte streams. It is a transport protocol specifically designed to provide reliable end-to-end byte streams over unreliable Internet networks. An Internet is very different from a single network because different parts of the Internet may have very different topologies, bandwidths, delays, packet sizes, and other parameters. TCP is designed to dynamically adapt to these characteristics of the Internet and to be robust in the face of various failures

Since it needs to provide a reliable connection, it certainly requires extra work to do so, which brings us to the topic of this article (three-way handshake), and how does this process work in detail

Explain the process of the three-way handshake

The basic purpose of the three-way handshake is for both the client and the server to confirm that the sending and receiving capabilities of the other side are normal. Understanding this basic purpose, you can understand why it is designed so

The whole process is divided into five steps

  • In the first step, both the client and the server are in the CLOSE state. At this point, the client actively opens the connection, and the server passively opens the connection. The server state changes to LISTEN
  • In the second step, the client randomly initializes a client_ISN, sets the SEQUENCE number to the sequence number field in the TCP header, sets the SYN flag to 1, indicating a SYN packet. Then, the client sends the SYN packet to the server, and the client state changes to SYN_SENT
  • Step 3: After receiving the SYN packet from the client, the server randomly initializes its server_ISN and places it in the sequence number field of the TCP header. At the same time, the server places client_ISN + 1 in the acknowledge reply number field of the TCP header. The SYN and ACK fields are set to 1, and the packet is sent to the client. The status of the server changes to SYN_RCVD
  • Step 4: After the client receives the packet from the server, this is the second handshake. At this time, the client sends an acknowledgement packet to the server. First, the ACK field of the TCP header is set to 1, and then the acknowledgement number field is set to server_ISN +1. The client state is ESTABLISHED
  • Step 5: After the server receives the reply packet sent by the client, this is the third handshake and the state changes to ESTABLISHED. At this point, the client and the server can officially communicate

Through the analysis of the above steps, we can know that the first handshake can let the server know that the sending ability of the client is normal, the second handshake can let the client know that the sending and receiving ability of the server is normal, and the third handshake can let the server know that the receiving ability of the client is normal. At this point, we can make the communication parties confirm that the other side’s sending and receiving capabilities are normal, and then we can carry out reliable communication

conclusion

TCP requires three handshakes to establish, but four waves to destroy