Popular understanding of three handshakes and four waves (old friend attachment style)

First, let's look at a picture and what
write picture description here
the picture describes:

  1. A: Hello brother, I want to establish a connection from you and you, send a request for connection to the B server, and then wait for the answer from the opposite brother (which contains the synchronization sequence field that I want to confirm the connection with: it can be understood as , our two mobile phone numbers (syn); enter the syn waiting state)
  2. B: I received your request (using the ACK = 1 flag to inform), but I want to confirm whether the request is yours, so I will send you the phone number you gave me and send mine back to you. The connection method (synchronization sequence syn) is also sent (generally referred to as: syn+ack ack is used to confirm the status of syn used to send itself)
  3. A: After receiving the package from the server, let him know that I have received yours. The information you said is correct. Let’s shake hands (in the established state)

Theoretical aspects:

In the three-way handshake, the server maintains this unconnected queue, specifically for steps 2 and 3, that is, a queue composed of the client's syn packet (connection information); the first syn packet is thrown into the queue, and then sent to the queue. When the client sends a confirmation packet, the entry will be marked in the syn-recv state. When the client responds to the confirmation, the entry will be deleted from the queue.

Reference: https://blog.csdn.net/qq_18425655/article/details/52163228
Field explanation:

Two serial numbers and three flags:

(1) Sequence number: seq sequence number, occupying 32 bits, used to identify the byte stream sent from the TCP source end to the destination end, which is marked when the initiator sends data.
(2) Confirmation sequence number: ack sequence number, occupying 32 bits, only when the ACK flag bit is 1, the confirmation sequence number field is valid, ack=seq+1.
(3) Flag bits: a total of 6, namely URG, ACK, PSH, RST, SYN, FIN, etc., the specific meanings are as follows:
(A) URG: Urgent pointer is valid.
(B) ACK: Confirm that the serial number is valid.
(C) PSH: The receiver should deliver this message to the application layer as soon as possible.
(D) RST: Reset the connection.
(E)SYN: Initiate a new connection.
(F) FIN: Release a connection.

Why 3 handshakes

We assume that the request sent for the first time arrives at the server because the network delay is very slow, and then the client thinks that the server does not hold me, and then silently closes the request waiting for the connection and walks away (like chasing the goddess); but the fact Woolen cloth? The goddess (server) saw it very late because of various reasons, and then said that I accepted you, and let us get married according to your request! However, A has already gone away, and A will not receive this request at all (in the second handshake, the server opens the connection and waits for the client's response), then the girl, thinking that the other party has received it, will keep waiting, so that B's Resources will be wasted (when creating a connection, space waste and port consumption);
but if it is three connections, waiting for the client's confirmation to establish the connection, it will not happen, the goddess (server) agrees, but A lacks There has been no next action, resulting in waste of resources;

waved four times

write picture description here

This is the same as saying goodbye to a polite friend: (a: client b: server)
1. At first, A wanted to go home and leave, but what? I'm afraid that B still has something to explain, so what? I have to say hello to B first, I have to leave, and I ask to stop talking (at this time, the connection between a and B is not disconnected, and communication can still be carried out);
2. Agree with A's request, say yes, but I may have more here Some words (data) are not finished. Wait until I'm done talking to you before you go.
3. After B has finished speaking the last words (data), let me know, you and I can break up (B waits for a response and closes the connection (the notified party))
4. A said yes, I understand, 88;( B walks away after learning and closes his own connection)

The last stage 4 will have A reluctant state (time_wait):
why? He was afraid that B would have something to say if he turned around suddenly (the network latency is high, and some data will be received after a while). Why wait for 2MSL?
Since there is a loss of the last acknowledgment shutdown request, the maximum segment lifetime is set to resend ack packets that may be lost. PS: The timeout also disappeared, it doesn't matter if it is sent or not

After the client and the server establish a TCP connection through three-way handshake, when the data transmission is completed, the TCP connection must be disconnected. For the disconnection of TCP, there is a mysterious "four waves of hands".

The first wave: Host 1 (can be either the client or the server), set the Sequence Number and Acknowledgment Number, and send a FIN segment to Host 2; at this point, Host 1 enters the FIN_WAIT_1 state; this means Host 1 There is no data to send to host 2;
the second wave: host 2 receives the FIN segment sent by host 1, and returns an ACK segment to host 1. The Acknowledgment Number is the Sequence Number plus 1; host 1 enters the FIN_WAIT_2 state ;host 2 tells host 1 that I "agree" to your shutdown request;
third wave: host 2 sends a FIN segment to host 1, requesting to close the connection, and host 2 enters the LAST_ACK state;
fourth wave: host 1 After receiving the FIN segment sent by host 2, it sends an ACK segment to host 2, and then host 1 enters the TIME_WAIT state; after receiving the ACK segment from host 1, host 2 closes the connection; at this time, host 1 waits for If no reply is received after 2MSL, it proves that the server has been shut down normally. Well, host 1 can also close the connection.

Why wave four times

Why did you break up four times? The TCP protocol is a connection-oriented, reliable, byte stream-based transport layer communication protocol. TCP is full-duplex mode, which means that when host 1 sends a FIN segment, it just means that host 1 has no data to send, and host 1 tells host 2 that all its data has been sent; however, At this time, host 1 can still accept data from host 2; when host 2 returns an ACK segment, it means that it already knows that host 1 has no data to send, but host 2 can still send data to host 1; when host 2 also When the FIN segment is sent, it means that host 2 has no data to send, and will tell host 1 that I have no data to send, and then they will happily terminate the TCP connection. If you want to correctly understand the principle of the four breakups, you need to understand the state changes during the four breakups.

FIN_WAIT_1: This state needs to be explained well. In fact, the real meaning of the FIN_WAIT_1 and FIN_WAIT_2 states is to wait for the other party's FIN message. The difference between these two states is: the FIN_WAIT_1 state is actually when the SOCKET is in the ESTABLISHED state, it wants to actively close the connection and sends a FIN message to the other party. At this time, the SOCKET enters the FIN_WAIT_1 state. When the other party responds to the ACK message, it enters the FIN_WAIT_2 state. Of course, under normal circumstances, the other party should immediately respond to the ACK message no matter what the situation is. Therefore, the FIN_WAIT_1 state is generally more difficult to see. The FIN_WAIT_2 status can sometimes be seen with netstat. (active party)
FIN_WAIT_2: This state has been explained in detail above. In fact, SOCKET in the state of FIN_WAIT_2 indicates a semi-connection, that is, one party requests to close the connection, but also tells the other party that I still have some data to send to you. (ACK message) and close the connection later. (active party)
CLOSE_WAIT: The meaning of this state actually means that it is waiting to close. How to understand it? When the other party closes a SOCKET and sends a FIN message to itself, your system will undoubtedly respond with an ACK message to the other party, and then enter the CLOSE_WAIT state. Next, what you really need to consider is to see if you still have data to send to the other party. If not, then you can close the SOCKET and send a FIN message to the other party, that is, close the connection. So you are in the CLOSE_WAIT state, what needs to be done is to wait for you to close the connection. (Passive side)
LAST_ACK: This state is relatively easy to understand. It is a passive shutdown party that waits for the other party's ACK message after sending the FIN message. When the ACK message is received, it can enter the CLOSED available state. (passive side)
TIME_WAIT: Indicates that the FIN message from the other party is received and an ACK message is sent. After 2MSL, it can return to the CLOSED available state. If in the FINWAIT1 state, when a message with both the FIN flag and the ACK flag is received from the other party, it can directly enter the TIME_WAIT state without going through the FIN_WAIT_2 state. (active side)
CLOSED: Indicates that the connection is interrupted.

Guess you like