The three-way handshake and four-way handshake of the TCP protocol in the Adventures of the Datacom Kingdom

Series of Articles:
Adventures in the Datacom Kingdom (2)


Table of contents

foreword

 1. We call TCP a reliable transport layer protocol. Why is it called reliable?

Second, the establishment of TCP - three-way handshake

1. Know in advance what is in the TCP protocol message?

2. The first handshake

In general: PC1 sends a synchronous message to PC2 saying, I want to establish a connection with you

3. The second handshake

In general: PC2 agrees to establish a connection with PC1, and at the same time confirms that it also wants to establish a connection with PC1 and that it has received the synchronization message from PC1

4. The third handshake

In general: PC1 sends a message to PC2 saying: I have received your synchronization message and confirmation message, and the relationship is established.

Three, TCP closed ---- waved four times

1. Why are you waving your hands four times?

2. First wave

Know in advance:

Because two of the four wavings are repeated, but the directions are different, so here we introduce the one-to-one waving from left to right, and the waving is based on the three-way handshake. We will not give an overview of the meaning of the received identifier. We will introduce the four waved hands on the premise that everyone has already understood.

3. Second wave

4. Why are there two repeated waves in the four waves?

5. About the details of waved four times

Summarize


foreword

With the continuous development of Huawei, the technology of data communication is becoming more and more important. Many people have started learning data communication. This article introduces the three-way handshake and four-way handshake of the TCP protocol. Second stop (we are using Huawei's ENSP software in this article).


 1. We call TCP a reliable transport layer protocol. Why is it called reliable?

Because its establishment experienced three handshakes, and its closure experienced four waved hands.

All in all, every step of it needs to be confirmed, and the next step cannot be performed without confirmation, so it is very reliable

This is why TCP is called a reliable protocol

Second, the establishment of TCP - three-way handshake

First look at the picture to understand, and our explanation is based on this picture:

1. Know in advance what is in the TCP protocol message?

First we see the IP header and the TCP header

1. The source IP and destination IP are stored in the IP header.

Source IP: It is the IP address of the sender

Destination IP: It is the IP address of the recipient

2. Exists in the TCP header

Seq : Represents the size of the sent data, which is random

Flags : flag bit, indicating what type of current message is

Explanation: It is what kind of field this flag may fill

There are two message types of Flags 1, SYN 2, ACK

SYN :    means synchronization

Explanation: PC1 wants to establish a connection with PC2, which is synchronization, which is initiated by PC1 and accepted by PC2.

ACK : means confirmation

Explanation: PC1 just sent a message to PC2 to establish a connection, PC2 agreed, and wanted to express that it already knew that PC1 would establish a connection with itself, so there must be a certain confirmation in the reply message to PC1, so that PC1 knows that it has received the packet requesting establishment.

There are two kinds of confirmations sent to PC1: one is hidden and the other is clear text.

2. The first handshake

We analyze the information in the TCP header sent by PC1:

In general: PC1 sends a synchronous message to PC2 saying, I want to establish a connection with you

Seq=a: What does it mean: PC1 will send a piece of data, the serial number is equal to a (Seq=a) , it can be understood that PC1 sent a data of a size to PC2.

Ack=0: What does it mean?: It can be understood that the current confirmation of PC1 is equal to 0

Flags: SYN setting : It means to be synchronized, what does it mean, that is, PC1 needs to establish a connection with PC2

In layman's terms, pc1 and pc2 say, I want to establish a TCP connection with you

When PC2 receives the request establishment message from PC1, it means that the first handshake is over.

3. The second handshake

When this pc2 receives the request to establish a connection from pc1

In general: PC2 agrees to establish a connection with PC1, and at the same time confirms that it also wants to establish a connection with PC1 and that it has received the synchronization message from PC1

We analyze the information in the TCP header sent by PC2:

Seq=b: pc2 will send a sequence number of random size equal to b, which can also be understood as data of size b

Ack=a+1: What does it mean?:

That is to say, PC1 just sent a data of size a to PC2, so PC2 will tell PC1 after receiving it, I have received the data of size a, and then you can start from the size of a+1 Start posting.

for example:

For example, if the data sent by pc1 before is 5, after receiving it, pc2 tells pc1 that you will send me a message from 6 next time, and next time PC1 can send a message to PC2 from 6

Flags: SYN setting : It means that synchronization is also required, that is, PC2 needs to establish contact with PC1

In layman's terms, pc2 and pc1 say that I want to establish a TCP connection with you

Flags: ACK is set : PC2 actively initiates a connection to PC1, and at the same time PC2 confirms the request sent from left to right by PC1.

In layman's terms it is:

PC1 just sent a message to PC2 saying that it wants to establish a connection, PC2 said that I also want to establish a connection with you, and I already know that you want to establish a connection with me l is the meaning of Ack confirmation

4. The third handshake

When this pc1 receives the connection establishment and confirmation message of pc2

We analyze the information in the TCP header sent by PC1:

In general: PC1 sends a message to PC2 saying: I have received your synchronization message and confirmation message, and the relationship is established.

Seq=a+1: Because PC2 just sent me a request to establish a connection with PC1, I told pc1 in the Ack, next time you send me data, you can start sending data from a+1, because PC1 sent me data before is a, pc2 has received all of them, then pc1 will start sending from a+1

Implied meaning: It implies from the side that pc1 has received the handshake message from pc2.

Ack=b+1: What does it mean:

And did PC2 send a piece of data to PC1 just now, the size is b, so PC1 will also confirm this data, b+1 means that PC1 has received the message sent by PC2 from right to left, this The data size is b, so next time PC2 can start sending data to PC1 from b+1,

Flags: ACK set :

Because this message is a confirmation of its second handshake message, the flag bit is only confirmation, that is, Ack, because pc2 has already synchronized with pc1, and pc1 has also synchronized with pc2, and finally pc1 replies with a message Tell pc2 that I have received your synchronization message and confirmation message

So PC1 and PC2 are connected to this formally established

Three, TCP closed ---- waved four times

1. Why are you waving your hands four times?

Because it is from left to right, PC1 needs to disconnect from PC2, and PC2 needs to reply to PC1 to confirm, agreeing to disconnect from PC1, which is reversed from left to right

From right to left, it is the same, PC2 also disconnects from PC1, and then PC1 agrees to disconnect,

The one back and forth of the two times is exactly four times, so it is called four times of waving

The TCP protocol must have a confirmation mechanism. Without confirmation, there is no way to complete the interaction normally.

After the establishment of the three-way handshake through TCP just now, the communication can be normal

After the data transmission is completed, TCP needs to disconnect the TCP connection through four hand-waving mechanisms to release system resources

What we just said has two directions, and it takes two times in total. One is from right to left, and the other is from left to right. So 2+2 is exactly 4, so we call it four waves .

2. First wave

Know in advance:

Because two of the four wavings are repeated, but the directions are different, so here we introduce the one-to-one waving from left to right, and the waving is based on the three-way handshake. We will not give an overview of the meaning of the received identifier. We will introduce the four waved hands on the premise that everyone has already understood.

 

First wave:

Suddenly, pc1 initiates a disconnection request to pc2, and a flag that requests disconnection is called FIN, FIN means the end, and it also brings an ACK confirmation, because of what? , before PC1 sends a request to disconnect from pc2, pc2 must have sent you data, have you received the data from PC1, so does PC1 need to confirm this behavior, so my PC1 not only needs to request disconnection, And the premise of requesting disconnection is that my PC1 has received all the data sent by PC2, which is why the flag bits in the message sent by PC1 have these two fields

Flags: FIN means to disconnect

3. Second wave

 Second wave:

PC1 requests to disconnect from PC2. Generally speaking, PC2 is sure, and it can also disconnect from PC1, so PC2 replies. The data just sent by PC1 must confirm that PC2 has received Ack301, that is, the next time PC2 can start from the size of 301. Start to send data to pc1, so pc2 sends the data size to PC1 from 301, Ack is equal to 102, why because the data sent by PC1 before is 101, then pc2 sends to pc1 from 102, which means that pc2 has received 101, Next time, pc1 can send it from 102 to pc2.

Confirm that pc1 sends a message from 102 to pc2 next time, and then its flag is Flags: ACK, which means that PC2 agrees to the disconnection request sent by pc1 just now

4. Why are there two repeated waves in the four waves?

After the left is disconnected from the right, it does not mean that the right and the left are disconnected. It is two-way, so four waves are required.

The right is similar to the left, so it takes four waves to cleanly disconnect

From right to left, it is the turn of pc2 to send a message request for disconnection to pc1, and the flag bit is the same as the flag bit sent by pc1

Then before I send this request to disconnect, I must confirm the data you sent before, because before the data is sent,

It is unreasonable for you to request disconnection. We need to confirm with you, and then we will request disconnection for you, and then our pc1 will reply to it.

So one back and one back, one back and one back, a total of two times, it becomes four waves

PC1 and PC2 are disconnected after four waved hands

5. About the details of waved four times

  1. An unsolicited disconnection will contain an acknowledgment of the received data before the disconnection. ( flags:fin+ack )
  2. After receiving the request for disconnection, the requested party will reply to the request with a separate confirmation message (the second wave )
  3. As of now this position is disconnected from left to right.
  4. Right to left is similar, so it takes a total of four waves to completely disconnect.

Summarize

Congratulations, you have passed the study of this chapter, the second level of Shutong Kingdom, there are still many levels behind, Shutong Kingdom still has many treasures waiting for you to explore, come on! Just to become a better version of myself.

Guess you like

Origin blog.csdn.net/weixin_73466540/article/details/131404800