Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the process and reason of the three-way handshake in TCP/IP protocol

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

In this issue, Xiaobian will bring you about the three-way handshake process in TCP/IP protocol and what is the reason. The article is rich in content and analyzed and described from a professional perspective. After reading this article, I hope you can gain something.

What do you do with three handshakes?

The three-way handshake establishes the TCP connection's reliability, assigns initial sequence numbers and resources, and begins data transmission after mutual acknowledgement. There are active open (usually client) and passive open (usually server).

TCP uses a 3-way handshake to establish a connection. This handshake initializes the necessary information for transmission reliability and data sequencing. These information includes the initial sequence number in both directions. The acknowledgement number is generated by the initial sequence number. The 3-way handshake is used because the 3-way handshake has prepared the necessary information for transmission reliability and data sequencing. The third handshake does not actually need to be transmitted separately. It can be transmitted together with data. The detailed process is as follows:

In the first step, the Client enters SYN_SENT state and sends a Syn message to the Server side. The SYN flag bit is set to 1 in this scenario, and the Seq number assigned by the Client side will be carried. This sequence number is an integer of U32. The allocation of this value is a random value generated according to time. Usually, it will increase by 1 every 4ms. In addition to this, it also carries an MSS, which is the maximum message segment length, indicating the length of the largest data block transmitted by TCP to the other end.

Step 2: After receiving the SYN message, the server enters the SYN_RCVD state and returns an Ack message to the Client to notify the Client that the server has received the SYN message and passed the acknowledgement. This step includes two parts: one part is to reply to the Syn message of the Client, where ACK=1, Seq number is set to Seq value +1 of the Syn message of the Client; the other part is to actively send the Syn message of the Server side to the Client, Seq number is the corresponding sequence number on the Server side, of course, the Syn flag bit will also be set to 1, and MSS represents the maximum data block length of the Server side.

Step 3: After receiving the message from Step 2, the Client will first change the state of the Client from SYN_SENT to ESTABLISHED. At this time, the Client sends a message to the Server, and the channel in this direction has been successfully established. The Client can send messages to the Server, and the Server can also successfully receive these messages. Secondly, the client needs to reply ACK message to the server, the message contains ACK status is set to 1, Seq number is set to the sequence number of the Server +1. (Note: This step is often combined with the data message initiated by the Client and sent to the server.)

Step 4: After receiving the Ack message, the Server enters the ESTABLISHED state. At this time, the channel connection sent by the Server to the Client is successfully established, and the Server can send data to the Client. The TCP full-duplex connection is established.

The picture comes from the Internet. Please inform us of the infringement and delete it in time.

Why three handshakes?

TCP connection because it is full-duplex, that is, both ends of the Client and Server, send messages in both directions of the connection must be established successfully. Three communications is the minimum number of times a two-way connection is guaranteed. More than three times, then the number of communications is unnecessary, is a waste of resources.

Second time, what would happen? The answer is no. Let's look at the following scenario.

Before we talk about this, we must first understand that TCP is based on IP protocol, and IP protocol is routed, IP protocol can not guarantee that the first data sent first arrives, which depends on the underlying network quality of IP protocol, and the number of routing hops between Client and Server.

After the Client sends Syn message 1, which is called Syn1 here, it is assumed that Syn1 does not reach the Server due to network reasons. At this time, the client has timed out, and then the Client re-initiates SYN message, which is called Syn2 here. As a result, Syn2 arrived at the Server first due to network reasons, and the Server established a connection with the Client based on Syn2. As a result, Syn1 arrived at the Server again not long after, and the Server closed the connection established by Syn2 and re-established a connection. For the Client, the newly established connection is outdated, so the Client will not send any data on this connection, which causes the Server to receive no data for a long time, and the Client's new connection is broken.

What happens when the three shakes fail?

The client does not receive the ACK message after sending SYN, the Client will retransmit, the first retransmission time is between 5.5-6s, the second retransmission will be 24s, unsuccessful will continue to try, Berkeley system after more than 75s, if still unsuccessful, will give up trying to connect. (Note: The retransmission time setting here has something to do with the timer setting at the bottom. Please refer to TCP/IP Detailed Explanation Volume 1. We will not discuss it here.)

If the Server does not receive the last Ack message, the same principle, the Server will also retransmit the Syn+Ack message of the second step.

State transition of three-way handshake?

TCP state change diagram is shown below, the initial state of Client and Server are CLOSED state, where:

Client:

CLOSED----Send Syn Message-->SYN_SENT---Receive SYN+ACK from Server--->ESTABLISHED--Send and Receive Data-->...

Server:

CLOSED---Receive SYN message from Client-->SYN_RCVD----Send SYN+ACK message-->SYN_RCVD---Receive ACK message from Client-->ESTABLISHED---Send and receive data-->...

The functional correspondence between TCP's three-way handshake and Scoket is as follows:

Remarks: Connect triggers a connection request and sends SYN J packet to the server. Connect enters the blocking state. The server monitors the connection request and receives SYN J packet. It calls the accept function to receive the request and send SYN K and ACK J+1 to the client. Accept enters the blocking state. After the client receives SYN K and ACK J+1 from the server, connect returns and confirms SYN K. When the server receives ACK K+1, accept returns. The three-way handshake is completed and the connection is established.

The above is what is the three-way handshake process and reason in TCP/IP protocol shared by Xiaobian. If there is a similar doubt, please refer to the above analysis for understanding. If you want to know more about it, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report