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

Three-way handshake and four waving of TCP/IP links

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

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

one。 Overview figure:

Establishing a TCP requires a three-way handshake to establish, while disconnecting requires a four-way handshake. The whole process is shown in the following figure:

Let's take a look at how to establish a connection.

First, the client sends a connection request message, and the Server segment replies to the ACK message after accepting the connection, and allocates resources for the connection. After receiving the ACK message, the Client terminal also sends the ACK message to the Server segment and allocates resources, so that the TCP connection is established.

So how to disconnect? The simple process is as follows:

two。 Note:

(1)。 Flag bit:

Tcp flag bit, there are 6 kinds of marks:

SYN (synchronous set up online)

ACK (acknowledgement confirmation)

PSH (push Transport)

FIN (end of finish)

RST (reset reset)

URG (urgent Emergency)

Sequence number (sequence number) (own)

Acknowledge number (confirmation number) (of the other party)

For the specific role, please refer to the 6th edition of "computer Network" Xie Xiren.

(2)。 Link closing side: the disconnection side can be Client side or Server side.

Suppose the client initiates a request to disconnect, that is, to send a FIN message. When Server receives the FIN message, it means "I have no data to send to you on Client", but if you still have data to send, you don't have to close Socket in a hurry, you can continue to send data. So you send ACK first, "tell the client that I have received your request, but I am not ready, please continue to wait for my message." At this time, the Client side enters the FIN_WAIT state and continues to wait for the FIN message on the server side. When the Server side determines that the data has been sent, it sends a FIN message to the client side, "tell the client side, all right, I have finished sending the data, and I am ready to close the connection." After receiving the FIN message, the Client side "knows that the connection can be closed, but he still does not trust the network, for fear that the Server side does not know to shut it down, so it enters the TIME_WAIT state after sending the ACK. If the Server side does not receive the ACK, it can retransmit it." When the server receives the ACK, it "knows it's ready to disconnect". After waiting for 2MSL, the Client still does not receive a reply, which proves that the Server has been shut down normally. Well, my Client can also close the connection. The Ok,TCP connection is closed!

three。 State machine:

The state experienced by the Client side of the whole process is as follows:

The process experienced by the Server side is as follows: reprint please indicate: blog.csdn.net/whuslei

[note] in the TIME_WAIT state, if the last ACK sent by the TCP client side is lost, it will be resent. The time required in the TIME_WAIT state depends on the implementation method. Typical values are 30 seconds, 1 minute, and 2 minutes. After waiting, the connection is officially closed and all resources (including the port number) are released.

[question 1] Why is it a three-way handshake when connected and a four-way handshake when it is closed?

Answer: because when the Server receives the SYN connection request message from the client, it can send the SYN+ACK message directly (SYN and ACK are in the same message). The ACK message is used to reply, and the SYN message is used to synchronize. However, when the connection is closed, when the Server side receives the FIN message, it is likely that the SOCKET will not be closed immediately, so it can only reply an ACK message first, telling the client, "I received the FIN message you sent." Only when all the messages on my Server side have been sent, can I send FIN messages, so I can't send them together. So you need a four-step handshake.

[question 2] Why does the TIME_WAIT state need to go through 2MSL (maximum segment lifetime) to return to the CLOSE state?

A: although according to reason, after all four messages have been sent, we can directly enter the CLOSE state, but if the network is unreliable, the last ACK can be lost. So TIME_WAIT status is used to resend ACK messages that may be lost.

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

Network Security

Wechat

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

12
Report