当前位置:网站首页>TCP's understanding of three handshakes and four waves
TCP's understanding of three handshakes and four waves
2022-07-05 06:21:00 【Xiao Zhu, Xiao Zhu will never admit defeat】
Interview frequently asked !!TCP Three handshakes and four waves understand
List of articles
- One 、TCP Header field
- Two 、 Three handshakes process understanding
- 3、 ... and 、 Understanding the process of four waves
- Four 、 Frequently asked questions
- 1. Why three handshakes when connecting , It's four handshakes when it's closed ?
- 2. Why? TIME_WAIT Status needs to pass 2MSL( Maximum segment lifetime ) To return to CLOSE state ?
- 3. Why not connect with two handshakes ?
- 4. If a connection has been established , But what to do if the client suddenly fails ?
One 、TCP Header field
Have a look first TCP Of Header field
: Serial number seq
: Occupy 4 Bytes , Used to mark the order of data segments ,TCP Put a sequence number on all the data bytes sent in the connection , The number of the first byte is randomly generated locally ; After numbering the bytes , Each segment is assigned a sequence number ; Serial number seq It is the data number of the first byte in this message segment .
Confirmation no. ack
: Occupy 4 Bytes , Expect to receive the sequence number of the first data byte of the next message segment of the other party ; The serial number indicates the number of the first byte of data carried by the message segment ; The confirmation number refers to the number of the next byte expected to be received ; So the number of the last byte of the current message segment +1 This is the confirmation number .
confirm ACK
: Occupy 1 position , Only when the ACK=1 when , The confirmation number field is valid .ACK=0 when , Invalid confirmation number
Sync SYN
: Used to synchronize the serial number when the connection is established . When SYN=1,ACK=0 It means : This is a connection request segment . If you agree to connect , In the response segment SYN=1,ACK=1. therefore ,SYN=1 Indicates that this is a connection request , Or connect to receive messages .SYN This flag is only in TCP It's only set when building production connection 1, After handshake SYN The flag bit is set 0.
End FIN
: Used to release a connection .FIN=1 Express : The data of the sender of this message segment has been sent , And asks to release the transport connection
PS
:ACK、SYN and FIN These capitalized words indicate the sign bits , Its value is either 1, Or 0;ack、seq Lowercase words indicate serial numbers .
Two 、 Three handshakes process understanding
The first handshake
: When establishing a connection , The client sends syn package (syn=j) To the server , And enter SYN_SENT state , Wait for server to confirm ;SYN: Sync sequence number (Synchronize Sequence Numbers
).
The second handshake
: Server received syn package , Must confirm customer's SYN(ack=j+1), At the same time, I also send a SYN package (syn=k), namely SYN+ACK package , At this time, the server enters SYN_RECV state ;
The third handshake
: Client receives server's SYN+ACK package , Send confirmation package to server ACK(ack=k+1), This package has been sent , Client and server access ESTABLISHED(TCP Successful connection ) state , Complete three handshakes .
3、 ... and 、 Understanding the process of four waves
1) The client process sends the connection release message , And stop sending data . Release data message header ,FIN=1, Its serial number is seq=u( It is equal to the sequence number of the last byte of the previously transmitted data plus 1), here , Client access FIN-WAIT-1( Stop waiting 1) state . TCP Regulations ,FIN Even if the message segment does not carry data , Also need to consume a serial number .
2) The server receives the connection release message , Send confirmation message ,ACK=1,ack=u+1, And bring your own serial number seq=v, here , The server enters CLOSE-WAIT( Turn off waiting ) state .TCP The server informs the high-level application process , The client is released in the direction of the server , It's half closed , That is, the client has no data to send , But if the server sends data , The client still has to accept . It's going to last a while , That is the whole CLOSE-WAIT The duration of the State .
3) After the client receives the confirmation request from the server , here , The client enters FIN-WAIT-2( Stop waiting 2) state , Wait for the server to send the connection release message ( Before that, you need to accept the last data sent by the server ).
4) After the server sends the last data , Send the connection release message to the client ,FIN=1,ack=u+1, Because it's half closed , The server probably sent some more data , Suppose the serial number at this time is seq=w, here , The server is in LAST-ACK( Final confirmation ) state , Wait for the client to confirm .
5) After the client receives the connection release message from the server , Confirmation must be sent ,ACK=1,ack=w+1, And my serial number is seq=u+1, here , The client enters TIME-WAIT( Time waits ) state . Note that this time TCP The connection has not been released , Must go through 2∗∗MSL( Maximum segment life ) After , When the client cancels the corresponding TCB after , Only enter CLOSED state .
6) As long as the server receives the confirmation from the client , Enter immediately CLOSED state . Again , revoke TCB after , It's the end of this TCP Connect . You can see , End of server TCP The connection time is earlier than the client .
Four 、 Frequently asked questions
1. Why three handshakes when connecting , It's four handshakes when it's closed ?
answer : Because when Server End receipt Client Terminal SYN After connecting the request message , Sure Direct transmission SYN+ACK message . among ACK Messages are used to answer ,SYN Messages are used to synchronize
Of . But when you close the connection , When Server End receipt FIN When the message , It's not likely to shut down immediately SOCKET, So you can only reply one first ACK message , tell Client End ,“ You sent it FIN I received the message ”. Only when I Server All messages are sent , I can send FIN message , So we can't send . So it takes four steps to shake hands .
2. Why? TIME_WAIT Status needs to pass 2MSL( Maximum segment lifetime ) To return to CLOSE state ?
answer : Although according to reason , All four messages have been sent , We can go straight into CLOSE Status quo , But we have to pretend that the Internet is unreliable , Maybe the last one ACK The loss of . therefore TIME_WAIT State is used to resend what may be lost ACK message
. stay Client Send the last ACK reply , But it's time to ACK May be lost .Server If not received ACK, Will be sent repeatedly FIN fragment . therefore Client Can't close now , It has to confirm Server Received this ACK.Client Will send out ACK Then go to TIME_WAIT state .Client A timer will be set , wait for 2MSL Time for . If it is received again within that time FIN, that Client Will be reissued ACK And wait again 2MSL. So-called 2MSL It's two times. MSL(Maximum Segment Lifetime).MSL Refers to the maximum lifetime of a segment in the network ,2MSL It's the maximum time required for a send and a reply . If until 2MSL,Client Didn't receive it again FIN, that Client infer ACK Has been successfully received , End TCP Connect .
Other answers :
TIME_WAIT The state lasts 2MSL( Maximum message lifetime ), about 4 Minutes to convert to CLOSE state . because TIME_WAIT It's going to be a long time , Therefore, the server should try to reduce the active closing of the connection ,TIME_WAIT The main functions of :
(1) Resend lost ACK message , Ensure that the connection is closed reliably :
- Because of the network and so on , There is no guarantee of the last wave ACK The message must be transmitted to the other party , If ACK The loss of , The other party will retransmit over time
FIN, The active shutdown will respond again ACK In the past ; without TIME_WAIT
state , Direct closure , It's retransmitted by the other side FIN The message is responded with a RST message , this RST Will be passively closed end resolved as an error . meanwhile , The server can't shut down normally because it can't receive the information from the client .
(2) Ensure that the duplicate data segment of this connection disappears from the network :
- If there are two connections , The first connection is normally closed , The second same connection is established immediately ; If some data of the first connection is still stuck in the network , The delay data doesn't arrive until a new connection is established , Will interfere with the second connection , wait for
2MSL The last connected message data can disappear in the network .
3. Why not connect with two handshakes ?
answer :3 The second handshake accomplishes two important functions , Both Both parties are ready to send data
( Both sides know that they are ready to ), Also want to Both parties are allowed to negotiate the initial serial number , This serial number is sent and confirmed during handshake
.
Now change three handshakes to just two , Deadlock It's possible . As an example , Consider computers S and C Communication between , Assume C to S Send a connection request packet ,S Received this group , Concurrent Send confirmation response group . According to the agreement of two handshakes ,S Think the connection has been successfully established , You can start sending data packets . But ,C stay S In case that the reply packet of is lost in the transmission , Will not know S Are you ready to , I do not know! S What kind of serial number to create ,C Even doubted S Whether to receive your own connection request grouping . under these circumstances ,C Think the connection has not been established successfully , Will ignore S Any data sent... Points Group , Only wait for the connection to confirm the reply group . and S After the outgoing packet times out , Send the same packet over and over again . This creates a deadlock .
Other answers :
(1) The purpose of the third handshake is to confirm whether the receiving and sending capabilities of both parties are normal , The initialization serial numbers of both sides of the synchronous connection ISN, Prepare for reliable transmission in the future . In the two handshakes, only the server confirms the starting serial number of the client , But the client does not confirm the initial serial number of the server , The reliability of transmission cannot be guaranteed .
(2) The third handshake can prevent the invalid connection request message segment from being suddenly transmitted to the server , Cause the server to establish a connection incorrectly , Waste server connection resources .
The first connection request message segment sent by the client is not lost , But in a network node for a long time , So that it will not arrive until some time after the connection is released Server. Originally, this is a message segment that has already failed , but Server After receiving this invalid connection request message segment :
- Suppose you don't use “ Three handshakes ”, So as long as Sever Send a confirmation , A new connection is established . But now Client No connection request was made , So I won't pay any attention to Server The confirmation of , Nor to Server send data . and Server But think that a new connection has been established , And kept waiting Client Send data , such ,Server A lot of resources are wasted
- And use “ Three handshakes ” agreement , as long as Server Can 't get it from Client The confirmation of , You know Client There is no request to create , There will be no connection .
4. If a connection has been established , But what to do if the client suddenly fails ?
answer :TCP There is also a survival timer
, obviously , If the client fails , The server can't wait , Waste resources in vain . Every time the server receives a request from the client, it will reset the timer , The time is usually set to 2 Hours , If you haven't received any data from the client in two hours , The server will send a detection segment , After every 75 Send once in minutes . If you send it in a row 10 Detection messages still don't respond , The server thinks the client is down , Then close the connection .
Reference resources :
Interview frequently asked !!TCP Three handshakes and four waves understand
边栏推荐
- MySQL advanced part 1: triggers
- What is socket? Basic introduction to socket
- SQL三种连接:内连接、外连接、交叉连接
- Filter the numbers and pick out even numbers from several numbers
- Open source storage is so popular, why do we insist on self-development?
- MySQL advanced part 2: storage engine
- [rust notes] 15 string and text (Part 1)
- Network security skills competition in Secondary Vocational Schools -- a tutorial article on middleware penetration testing in Guangxi regional competition
- 求组合数 AcWing 888. 求组合数 IV
- P2575 master fight
猜你喜欢
[2021]IBRNet: Learning Multi-View Image-Based Rendering Qianqian
Is it impossible for lamda to wake up?
MySQL advanced part 2: optimizing SQL steps
阿里新成员「瓴羊」正式亮相,由阿里副总裁朋新宇带队,集结多个核心部门技术团队
Error ora-28547 or ora-03135 when Navicat connects to Oracle Database
Series of how MySQL works (VIII) 14 figures explain the atomicity of MySQL transactions and the principle of undo logging
Alibaba established the enterprise digital intelligence service company "Lingyang" to focus on enterprise digital growth
1.14 - assembly line
Redis publish subscribe command line implementation
SQLMAP使用教程(二)实战技巧一
随机推荐
4. Object mapping Mapster
Leetcode divide and conquer / dichotomy
Quickly use Amazon memorydb and build your own redis memory database
MySQL advanced part 2: SQL optimization
快速使用Amazon MemoryDB并构建你专属的Redis内存数据库
__ builtin_ Popcount() counts the number of 1s, which are commonly used in bit operations
Leetcode-1200: minimum absolute difference
Is it impossible for lamda to wake up?
Gauss Cancellation acwing 884. Solution d'un système d'équations Xor linéaires par élimination gaussienne
SPI 详解
LeetCode-61
Daily question 1189 Maximum number of "balloons"
Data visualization chart summary (II)
MySQL advanced part 2: MySQL architecture
Matrixdb V4.5.0 was launched with a new mars2 storage engine!
Chart. JS - Format Y axis - chart js - Formatting Y axis
博弈论 AcWing 894. 拆分-Nim游戏
Chapter 6 relational database theory
How to understand the definition of sequence limit?
P2575 master fight