当前位置:网站首页>TCP's three handshakes and four waves
TCP's three handshakes and four waves
2022-07-06 16:05:00 【Programming fish 66】
brief introduction
TCP It's a connection oriented 、 reliable 、 Transport layer communication protocol based on byte stream , Before sending the data , The communicating parties must establish a connection with each other . So-called “ Connect ”, In fact, it is a piece of information about each other saved by the client and the server , Such as ip Address 、 Port number, etc .TCP You can view it as a byte stream , It will deal with IP Lost packets of layers or layers below 、 Repetition and error problems . In the process of establishing the connection , The two sides need to exchange some connection parameters . These parameters can be placed in TCP Head . One TCP The connection consists of a 4 Tuples make up , Two IP Address and two port Numbers . One TCP Connections are usually divided into three phases : Connect 、 The data transfer 、 sign out ( close ). Make a link by shaking hands three times , Close a connection by waving four times . When a connection is made or terminated , The exchanged segments contain only TCP Head , And there's no data .
1. TCP Header structure of message
In understanding TCP Before connecting, let's get to know TCP Header structure of message .
There are several fields in the above figure that need to be highlighted :
(1) Serial number :seq Serial number , Occupy 32 position , Used to identify from TCP Byte stream sent from source to destination , Flag this when the initiator sends data .
(2) Confirm the serial number :ack Serial number , Occupy 32 position , Only ACK Sign bit is 1 when , Verify that the ordinal field is valid ,ack=seq+1.
(3) Sign a : common 6 individual , namely URG、ACK、PSH、RST、SYN、FIN etc. , The specific meaning is as follows :
- CK: Confirm that the serial number is valid .
- FIN: Release a connection .
- PSH: The receiver should send this message to the application layer as soon as possible .
- RST: Reset connection .SYN: Initiate a new connection .
- URG: Pointer to an emergency (urgent pointer) It works . It should be noted that : Do not confirm the serial number ack And in flags ACK Confused. . Confirmation party ack= Initiator seq+1, Pairing at ends .
2. Three handshakes
The essence of triple handshake is to confirm the ability of both sides of communication to send and receive data , I asked the messenger to deliver a letter to the other party , The other party received , Then he will know that my sending ability and his receiving ability are OK . So he wrote back to me , If I receive , I know that my sending ability and his receiving ability are OK , And his sending ability and my receiving ability are OK . However, at this time, he still doesn't know whether his sending ability and my receiving ability can , So I give back one last time , If he receives , Then he knew that his sending ability and my receiving ability were OK . this , Three handshakes .
author :InsaneLoafer
link :https://www.jianshu.com/p/0ad0b864b949
source : Simple books
The copyright belongs to the author . Commercial reprint please contact the author for authorization , Non-commercial reprint please indicate the source .
- The first handshake : The client needs to send a connection request to the server , First, the client randomly generates a starting sequence number ISN( For example 100), The message segment sent by the client to the server contains SYN Sign a ( That is to say SYN=1), Serial number seq=100.
- The second handshake : After the server receives the message from the client , Find out SYN=1, Know that this is a connection request , So the starting sequence number of the client is 100 Save up , And randomly generate a server's starting sequence number ( For example 300). Then reply a message to the client , The reply message contains SYN and ACK sign ( That is to say SYN=1,ACK=1)、 Serial number seq=300、 Confirmation no. ack=101( The serial number sent by the client +1).
- The third handshake : After receiving the reply from the server, the client finds ACK=1 also ack=101, So we know that the server has received the serial number of 100 The message from ; Simultaneous discovery SYN=1, I see. The server has agreed to this connection , So the server's serial number 300 Save it . Then the client replies a message to the server , The message contains ACK Sign a (ACK=1)、ack=301( Server serial number +1)、seq=101( The first handshake sends a message with a sequence number , So this time seq From 101 Start , It should be noted that there is no data to be carried ACK The message does not occupy the sequence number , So the first time you send data later seq still 101). When the server receives the message, it finds ACK=1 also ack=301, We know that the client received the serial number of 300 The message of , In this way, the client side and the server side pass through TCP It establishes the connection .
3. Four waves
The purpose of the four waves is to close a connection
For example, the serial number of client initialization ISA=100, Server initialization sequence number ISA=300.TCP After successful connection, the client sent a total of 1000 Bytes of data , The server sends FIN A total of replies were made before the message 2000 Bytes of data .
- First wave : When the client's data is transferred , The client sends a connection release message to the server ( Of course, when the data is not sent out, you can also send the connection release message and stop sending data ), The release connection message contains FIN Sign a (FIN=1)、 Serial number seq=1101(100+1+1000, Among them 1 It's a serial number used to establish the connection ). Note that the client sends out FIN After the message segment, it just can't send data , But it can also collect data normally ; in addition FIN Even if the message segment does not carry data, it has to occupy a sequence number .
- Second wave : The server receives a message from the client FIN After the message to the client reply confirmation message , The acknowledgement message contains ACK Sign a (ACK=1)、 Confirmation no. ack=1102( client FIN Message sequence number 1101+1)、 Serial number seq=2300(300+2000). At this time, the server is in the shutdown waiting state , Instead of sending it to the client immediately FIN message , It's going to last a while , Because the server may still have data to send .
- Third wave : The server will have the final data ( such as 50 Bytes ) After sending, send the connection release message to the client , The message contains FIN and ACK Sign a (FIN=1,ACK=1)、 The confirmation number is the same as the second wave ack=1102、 Serial number seq=2350(2300+50).
- Fourth wave : The client received a message from the server FIN After the message , Send an acknowledgement message to the server , The acknowledgement message contains ACK Sign a (ACK=1)、 Confirmation no. ack=2351、 Serial number seq=1102. Note that the client does not immediately release the confirmation message TCP Connect , It's going through 2MSL( Longest segment lifetime 2 Multiple duration ) Then release TCP Connect . Once the server receives the confirmation message sent by the client, it will be released immediately TCP Connect , So the end of the server TCP The connection time is earlier than the client .
4. Frequently asked questions
- Why? TCP When you connect, it's 3 Time ?2 Can't you do it again ?
- Because we need to consider the problem of packet loss when connecting , If you just shake hands 2 Time , In the second handshake, if the confirmation message segment sent by the server to the client is lost , At this time, the server is ready to send and receive data ( It can be understood that the server has successfully connected ) According to the , But the client has not received the confirmation message from the server , So the client doesn't know if the server is ready ( The client is not connected successfully ), In this case, the client will not send data to the server , It also ignores the data sent by the server . If it's three handshakes , Even if there is a packet loss, there will be no problem , For example, if you shake hands for the third time, the client sends a confirmation ack Message lost , The server has not received the confirmation within a period of time ack If the message is sent, a second handshake will be repeated , That is, the server will resend SYN Message segment , After receiving the retransmitted message segment, the client will send a confirmation to the server again ack message .
2. Why? TCP When you connect, it's 3 Time , When it was closed, it was 4 Time ?
- Because it can only be disconnected when the client and server have no data to send TCP. And the client sends out FIN Message can only ensure that the client has no data sent , The server does not know whether the data is sent to the client . And the server receives the client's FIN After the message can only reply to the client a confirmation message to tell the client that the server has received your FIN Message , But my server still has some data to send out , After these data are sent, the server can send it to the client FIN message ( Therefore, it is not possible to combine the confirmation message with the FIN The message is sent to the client , It's here one more time ).
3. Why does the client wait after sending the fourth wave confirmation message 2MSL Time to release TCP Connect ?
- Here is also to consider the problem of packet loss , If the message for the fourth wave is lost , The server did not receive the confirmation ack The message will be retransmitted with the third wave , In this way, the longest time a message goes back and forth is 2MSL, So it takes such a long time to confirm that the server has actually received it .
resources & More in-depth readings :
Open the mysterious channel to get information, click the link to join the group chat 【C Language /C++ Programming learning base 】:828339809
边栏推荐
- 【练习-10】 Unread Messages(未读消息)
- 【练习-1】(Uva 673) Parentheses Balance/平衡的括号 (栈stack)
- Hdu-6025-prime sequence (girls' competition)
- Research Report on shell heater industry - market status analysis and development prospect forecast
- STM32 learning record: LED light flashes (register version)
- [analysis of teacher Gao's software needs] collection of exercises and answers for level 20 cloud class
- Opencv learning log 19 skin grinding
- HDU - 6024 building shops (girls' competition)
- Opencv learning log 31 -- background difference
- The most complete programming language online API document
猜你喜欢
随机推荐
Opencv learning log 30 -- histogram equalization
7-1 understand everything (20 points)
编程到底难在哪里?
最全编程语言在线 API 文档
Penetration test (3) -- Metasploit framework (MSF)
[exercise-4] (UVA 11988) broken keyboard = = (linked list)
Gartner: five suggestions on best practices for zero trust network access
Alice and Bob (2021 Niuke summer multi school training camp 1)
VS2019初步使用
Penetration test 2 --- XSS, CSRF, file upload, file inclusion, deserialization vulnerability
D - function (HDU - 6546) girls' competition
Market trend report, technical innovation and market forecast of geosynthetic clay liner in China
CEP used by Flink
socket通讯
Penetration test (1) -- necessary tools, navigation
Analysis of protobuf format of real-time barrage and historical barrage at station B
Opencv learning log 19 skin grinding
7-1 懂的都懂 (20 分)
【练习-7】(Uva 10976)Fractions Again?!(分数拆分)
信息安全-威胁检测-flink广播流BroadcastState双流合并应用在过滤安全日志