Want to understand the three-way handshake and the four-way wave? look here! ! !

1. Introduction to knowledge points

     1. What is a three-way handshake?

Three-way Handshake actually means that when establishing a TCP connection, the client and server need to send a total of 3 packets. The main purpose of the three-way handshake is to confirm whether the receiving and sending capabilities of both parties are normal, and to specify your own initialization sequence number to prepare for subsequent reliable transmission. In essence, it is to connect to the designated port of the server, establish a TCP connection, synchronize the sequence number and confirmation number of both parties, and exchange TCP window size information.

 Essence: It is to confirm the ability of both communicating parties to send and receive data.

   2. What are four waves?

Establishing a connection requires three handshakes, and terminating a connection requires four waves (the four waves are also called a four-way handshake). This is caused by TCP's half-close. The so-called semi-closed actually means that TCP provides the ability for one end of the connection to receive data from the other end after finishing its sending. The teardown of a TCP connection requires sending four packets, so it is called a Four-way handshake. Either the client or the server can actively initiate the wave action.

 Essence: The purpose is to close a connection.

2. The process of three handshakes and four waves:

      1. Three-way handshake process:

  • The first handshake: First, the client sends a message to the server.
    This handshake is of great significance: it shows that the client's sending ability is normal.
  • The second handshake: Then the server receives the message sent to it by the client, and also sends the message to the client at the same time.
    This handshake is also very important: it shows that the server's own receiving capabilities and sending capabilities are normal
  • The third handshake: the client sends a message to the server again.
    This handshake is still important: because it proves that the client’s receiving ability is normal.

Some students may have questions, why do we need to shake hands again at the end? Isn't it purely redundant...

Leak, leak, leak, big leak, special leak. My dear friend,  let me explain it to you like this :

First of all, think about it, when you send a message to your girlfriend asking if she is there ( equivalent to the first handshake )

Then your girlfriend saw it, she received the message you sent, and also responded to your message at the same time ( equivalent to the second handshake )

Soon you will also receive her message back, but if you don't send another message for a long time, she won't know whether you saw the message she sent you, and if you keep her waiting, she will be very angry. Then you're screwed. ( Equivalent to the third handshake )

That’s it, gay friends, do you understand? If you don’t understand, you can try it with your girlfriend, and you will be deeply impressed.

To put it professionally:

When the client communicates with the server, the client sends a message and reaches the server

For the server, the server receives the content you sent and sends a confirmation to the customer service side.

For the client, if the client ignores the confirmation from the server, the server will always wait for messages from the client.

This leads to a waste of time on the server side (for the server, resources such as CPU are a waste), which is not feasible.

That's why you can't shake hands twice

   

                                                                  The picture shows the three-way handshake

       2. The process of waving four times :

  • The first wave: 
    the client sends a connection release message and stops sending data. Release the header of the data message, FIN=1, and its sequence number is seq=u (equal to the sequence number of the last byte of the previously transmitted data plus 1). At this time, the client enters FIN-WAIT-1 (terminates waiting) 1) Status
  • The second wave:
    After receiving the connection release message, the server sends a confirmation message, ACK=1, ack=u+1, and brings its own serial number seq=v. At this time, the server enters CLOSE -WAIT turns off the wait state
  • The third wave:
    After the client receives the confirmation request from the server, the client will enter the FIN-WAIT-2 (termination wait 2) state and wait for the server to send a connection release message. After the server completes sending the last data, After sending a connection release message to the client, the server enters the LAST-ACK (last acknowledgment) state and waits for confirmation from the client.
  • The fourth wave:
    After receiving the connection release message from the server, the client must send a confirmation, ACK=1, ack=w+1, and its serial number is seq=u+1. At this time, the client enters The TIME-WAIT (time waiting) state is entered, but the TCP connection has not been terminated at this time. It must pass 2MSL (the longest message life). When the client revokes the corresponding TCB, the client will enter the CLOSED state. After receiving the confirmation message, the server will immediately enter the CLOSED state. At this point, the TCP connection will be disconnected and completed with four waves.

 So after seeing this, some people still want to ask, why does it only take three times to connect, but four times to disconnect? ? ?

Then let me tell you something about boyfriend and girlfriend. First, imagine this scenario:

If you and your girlfriend meet to go out for fun today, the fun will definitely come to an end. After the date, we must go back to our respective homes and see our respective mothers to see each other again!

Hahahaha, here comes the point,

You: Goodbye, baby, go back ( wave for the first time )

Girlfriend: Okay baby, I’m leaving then ( waving for the second time )

Girlfriend: Then go back, baby ( waving for the third time )

You: Okay, I’m leaving then, bye baby ( waving for the fourth time )

That's it, just like saying goodbye to your girlfriend with as much reluctance as saying goodbye.

 The picture shows four waving hands

Guess you like

Origin blog.csdn.net/nuhao/article/details/130663577