当前位置:网站首页>TCP triple handshake
TCP triple handshake
2022-06-24 13:19:00 【Software up】
Preface
TCP When the protocol passed down data , client (Client) And the service side (Server) Will establish a connection , Then segment the files to be transferred , And provide reliable transmission and flow control ! After the data transfer is complete , The current session should also be disconnected , Avoid waste of resources . all TCP The third handshake is the process of establishing a connection , And four waves are the process of disconnecting !
TCP Basic knowledge
Transmission control protocol ( English :Transmission Control Protocol, abbreviation :TCP) It's a connection oriented 、 reliable 、 Transport layer communication protocol based on byte stream
TCP Header format
- Serial number : The random number generated by the computer when establishing the connection is used as the initial value , adopt
SYNThe packet is sent to the receiving host , Every time it's sent , The size of the number of bytes is accumulated once . It is used to solve the network packet disorder problem - Confirm answer signal : Next time [ expect ] The serial number of the data received , After receiving the acknowledgement, the sender can consider that the data before the serial number has been received normally . To solve the problem of no packet loss
- Control bits
ACK: This bit is 1 when , Confirm that the reply field becomes valid ,TCP Except when the connection is initially established SYN This bit outside the package must be set to bit 1
RST: This bit is 1 when , Express TCP When an exception occurs during connection, the connection must be forcibly disconnected
SYN: This bit is 1 when , Indicates that you want to establish a connection , And set the initial value of the serial number in the field of the serial number
FIN: This bit is 1 when , No more data will be sent in the future , Want to disconnect . When communication ends and you want to disconnect , The hosts of both sides can exchange with each other FIN Position as 1 Of TCP paragraph .
Three handshakes
- The first handshake : When establishing a connection , client A send out SYN package (
SEQ_NUMBER=x) To the server B, And enter SYN_SEND state , Waiting for server B confirm . - The second handshake : The server B received SYN package , Customer must be confirmed A Of
SYN(ACK_NUMBER=x+1), At the same time, I also send a SYN package(SEQ_NUMBER=y), namely SYN+ACK package , At this time, the server B Get into SYN_RECV state . - The third handshake : client A Receive server B Of SYN+ACK package , To the server B Send confirmation package
ACK(ACK_NUMBER=y+1), This package has been sent , client A And the server B Get into ESTABLISHED state , Complete three handshakes . - Complete three handshakes , Client and server begin to transmit data .
Four waves
- First wave : The client intends to close the connection , A TCP The first one
FINThe flag bit is set to1, Its serial number is the sequence number of the last byte of the previously transmitted data plus 1(seq=x) - Second wave : After the client receives the message , Send it to the client
ACKReply message ,ACK=1,ack=x+1, And bring your own serial numberseq=y, here , The server enters CLOSE-WAIT( Turn off waiting ) state . - 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 ).
- Third wave : After the server sends the last data , Send the connection release message to the client ,
FIN=1,ack=x+1, Because it's half closed , The server probably sent some more data , Suppose the serial number at this time isseq=z, here , The server is in LAST-ACK( Final confirmation ) state , Wait for the client to confirm . - Fourth wave : After the client receives the connection release message from the server , Confirmation must be sent ,
ACK=1,ack=z+1, And my serial number isseq=h, here , The client enters TIME-WAIT( Time waits ) state . Note that this time TCP The connection has not been released , After the longest message segment life , When the client cancels the corresponding TCB after , Only enter CLOSED state . - 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 .
Why not use two handshakes to connect ?
answer : Three handshakes complete two important functions , Both sides should be ready to send data , Also know that both sides know that each other is ready ! If you change to two handshakes, it will lead to deadlock . Suppose the client sends a connection request to the server , The server received this packet , And sent a confirmation response packet . According to the agreement of two handshakes , The server thinks that the connection has been successfully established , You can start sending data packets . But , When the response signal of the server is lost during transmission , The client does not know whether the server is ready , I don't know what serial number the server will create , The client even doubts whether the server has received its own connection request packet . under these circumstances , The client thinks that the connection has not been established successfully , Any data packets sent by the server will be ignored , Only wait for the connection to confirm the reply group . The server sends out packets after timeout , Send the same packet over and over again . This creates a deadlock .
The second of the four waves 、 What happens when you merge three times ?
answer : Because when the server receives a request from the client to disconnect , There may still be some data not sent out , Then reply first ACK, Indicates that a disconnect request has been received . Wait until the data is sent FIN, Disconnect the data transfer from the server to the client .
Only when all the messages on the server end are sent , To send FIN message , So we can't send . So it takes four waves .
边栏推荐
- 用一个软件纪念自己故去的母亲,这或许才是程序员最大的浪漫吧
- Another prize! Tencent Youtu won the leading scientific and technological achievement award of the 2021 digital Expo
- Quickly understand the commonly used message summarization algorithms, and no longer have to worry about the thorough inquiry of the interviewer
- Definition and use of constants in C language
- 使用 Abp.Zero 搭建第三方登录模块(一):原理篇
- The programmer's graduation project is still bald after a year
- Concept + formula (excluding parameter estimation)
- 【数据库】期末复习(计科版)
- 一文讲透研发效能!您关心的问题都在
- CVPR 2022 | 美團技術團隊精選論文解讀
猜你喜欢

DTU上报的数据值无法通过腾讯云规则引擎填入腾讯云数据库中

Use abp Zero builds a third-party login module (I): Principles

“有趣” 是新时代的竞争力

我真傻,招了一堆只会“谷歌”的程序员!

AGCO AI frontier promotion (6.24)

MySQL foreign key impact

华为AppLinking中统一链接的创建和使用

Pycharm中使用Terminal激活conda服务(终极方法,铁定可以)

线程同步的基石AbstractQueuedSynchronizer详解
![[database] final review (planning Edition)](/img/94/cd2df3a011c4e466df5aaa0f3bd0f2.png)
[database] final review (planning Edition)
随机推荐
爱可可AI前沿推介(6.24)
WPF from zero to 1 tutorial details, suitable for novices on the road
Kubernetes cluster deployment
Reset the password, and the automatic login of the website saved by chrome Google browser is lost. What is the underlying reason?
Quickly understand the commonly used message summarization algorithms, and no longer have to worry about the thorough inquiry of the interviewer
Engage in audio and video development? Several things I have to say about SRT live broadcast protocol
Getting started with the go Cobra command line tool
Nifi from introduction to practice (nanny level tutorial) - environment
如何高效的分析online.log
Sqlserver 2000 has long-lasting vitality
Megacli online management raid installation and use steps
16 safety suggestions from metamask project to solid programmers
Main steps of system test
手机开户后多久才能通过?在线开户安全么?
[programming navigation] the practical code summarized by foreign great God, learned in 30 seconds!
Party, Google's autoregressive Wensheng graph model
一文讲透研发效能!您关心的问题都在
关于被黑数据库那些事
我真傻,招了一堆只会“谷歌”的程序员!
CVPR 2022 | 美團技術團隊精選論文解讀