TCP shakes hands three times and waves four times

Posted by Tarsonis21 on Wed, 12 Jan 2022 06:43:17 +0100

TCP three handshakes and four waves

Quoted articles:

(32 messages) the whole process of tcp three handshakes and four handshakes and why three handshakes_ Rock owl blog - CSDN blog_ tcp the whole process of three handshakes and four waves

What is TCP protocol?

 TCP It is a host to host layer transmission control protocol that provides reliable connection services and establishes a connection by using three handshakes.

Triple Handshake: establishing connection

Four waves: disconnect

What is a bit code?

Bit code, namely tcp flag bit, has six representations:

SYN(synchronous Establish connection)
ACK(acknowledgement Indicates response and confirmation)
PSH(push Indicates yes DATA data transmission)
FIN(finish Close connection
RST(reset Indicates connection reset)
URG(urgent The emergency pointer field value is valid)

What are the three handshakes?

First handshake: sent by the client syn package(syn=x)Go to the server and enter SYN_SEND Status, waiting for server confirmation;

Second handshake: received by the server syn Package, the customer must be confirmed SYN(ack=x+1),Also send one yourself SYN Package( syn=y),Namely SYN+ACK Package, and the server enters SYN_RECV Status;

Third Handshake: the client receives a handshake from the server SYN+ACK Package, send confirmation package to the server ACK(ack=y+1),After the package is sent, the client and server enter ESTABLISHED Status, complete three handshakes

Note: the packet transmitted during the handshake does not contain data. After three handshakes, the client and server officially start transmitting data. Ideally, once a TCP connection is established, the TCP connection will be maintained until either of the communication parties actively closes the connection.
Confirmation number: its value is equal to the sending serial number of the sender + 1 (i.e. the next serial number expected to be received by the receiver).

For example, chestnuts:

Compare the client to a boy and the server to a girl. Explain the "three handshakes" process with their contacts:
(1)The boy likes girls, so he wrote a letter to tell the girl: I love you, please associate with me!;After writing the letter, the boy waited anxiously because he didn't know whether the letter could be successfully conveyed to the girl.
(2)The girl was elated after receiving the boy's love letter. It turned out that we were in love! So I wrote a reply to the boy: I received your love letter and understood your mind. In fact, I like you too! I am willing to associate with you!;
After writing the letter, the girl also waited anxiously, because she didn't know whether the reply could be successfully conveyed to the boy.
(3)The boy was very happy after receiving the reply, because the girl received the love letter and learned from the reply that the girl liked herself and was willing to associate with herself. Then the boy wrote another letter to the girl: I have received your heart and letter. Thank you and I love you!
After the girl received the boy's reply, she was also very happy because the boy received the love letter. Therefore, both boys and girls knew each other's feelings, and then they exchanged happily~~
This is the popular version of "three handshakes". During this period, a total of three letters were exchanged, that is, "three handshakes", to confirm whether the data transmission channels in both directions are normal

Why a third handshake?

In order to prevent the server from opening some useless connections, increasing the server overhead and preventing the invalid connection request message segment from being suddenly transmitted to the server, resulting in errors.
Because the network transmission is delayed (through the network optical fiber and various intermediate proxy servers), in the process of transmission, for example, the client initiates the request of SYN=1 to create a connection (the first handshake).
If the server directly creates the connection and returns the data packet containing SYN, ACK and Seq to the client, the data packet is lost due to network transmission. After the loss, the client has not received the data packet returned by the server.
The client may have set a timeout, and when the timeout expires, the request for connection creation is closed. Then reissue the request to create a connection, and the server does not know. If there is no third handshake to tell the server whether the client has received the data transmitted by the server, the server does not know whether the client has received the information returned by the server.

What are the four waves?

First wave: the active closing party sends a FIN,It is used to close the data transmission from the active party to the passive party, that is, the active party tells the passive Party: I will no longer send you data(Of course, in fin The data sent before the packet is not received ack After confirming the message, the active shutdown party will still resend these data),However, at this time, the active shutdown party can also accept data.

Second wave: passive closing party received FIN After the packet, send a ACK Give it to the other party and confirm that the serial number is the received serial number+1(And SYN Same, one FIN Occupy a sequence number).

Third wave: the passive closing party sends a FIN,It is used to close the data transmission from the passive closing party to the active Closing Party, that is, to tell the active closing party that my data has been sent and will not be sent to you again.

The fourth wave: received by the active Closing Party FIN After, send a ACK To the passive Closing Party, confirm that the serial number is the received serial number+1,So far, complete four waves.

TCP's four wave process (in short): the active shutdown sends the passive shutdown party a message that it will no longer send you data; The passive Closing Party confirms the received message segment of the active Closing Party; The passive shutdown sends the active shutdown party a message that I will not send you data again; The active Closing Party confirms the confirmation of the passive closing party again.

Take a chestnut

Compare the client to a boy and the server to a girl. Through their breakup to illustrate the "four wave" process.
"Waving for the first time ": after seeing people's hearts for a long time, the boy found that the girl had become a look he hated. He couldn't bear it, so he decided to break up, and then wrote a letter to tell the girl.
"The second wave ": after receiving the letter, the girl knew that the boy was going to break up with herself. She was angry and scolded in her heart: what are you? You weren't like this at the beginning! So I immediately wrote a reply to the boy: break up and break up. Give me some time. I'll tidy up your things and give them back to you!
After the boy received the girl's first letter, he understood that the girl knew he wanted to break up with her. Then wait for the girl to pack her things.
"The third wave ": after a few days, the girl sorted out all the things sent by the boy, so she wrote to the boy again: I sorted out your things and take them away quickly. From then on, you and I broke up!
"The fourth wave ": after the boy received the girl's second letter, he knew that the girl had packed up and could officially break up, so he wrote again to tell the girl: I know, I'll get it back!

Why does "shake hands" three times and "wave hands" four times?

The reason why TCP only needs "three handshakes" when establishing a connection is that in the second "handshake", the TCP message sent by the server to the client takes syn and ACK as flag bits. SYN is the request connection flag, indicating that the server agrees to establish a connection; ACK is a confirmation message that tells the client that the server has received its request message.
That is, the SYN connection establishment message and the ACK confirmation received message are transmitted in the same "handshake", so the "three handshakes" are not many or many, which just makes it clear that both parties exchange information with each other.
The reason why TCP needs to "wave four times" when releasing the connection is because FIN releasing the connection message and ACK confirming the received message are transmitted by the second and third "handshake" respectively. Why are connections transmitted together when they are established and separately when they are released?

The whole process of TCP three handshakes and four waves:

Topics: network Network Protocol HCIP TCP/IP