当前位置:网站首页>Explain TCP's three handshakes in detail
Explain TCP's three handshakes in detail
2022-04-23 14:45:00 【Luo Luo's 1024】
TCP Definition and characteristics
Definition
TCP It's connection-oriented ( Connection oriented ) Of 、 Reliable transport layer communication protocol based on byte stream .TCP Package user data into report segments , After sending, a timer will be started , Then confirm the data received at the other end 、 Reorder out of order data 、 Discard duplicate data
characteristic
- TCP Is a connection oriented transport control layer protocol
- Every one of them TCP Connections can have only two endpoints , Every one of them TCP Connections can only be point-to-point
- TCP Provide reliable delivery of services
- TCP Provide full duplex communication . Data is transmitted independently in both directions , therefore , Each end of the connection must maintain the serial number of the transmitted data in each direction .
- Byte stream oriented . For the meaning of byte stream : Although the application and TCP Interaction is a block of data at a time , But the data handed over by the application is just a series of unstructured byte streams
TCP message
Before starting three handshakes , look down TCP Data structure of message
- TCP The first one
- TCP Data section

Focus on TCP Head structure , as follows

Find out what serial number and confirmation number are
- Sequence number
It means that we ( The sender ) here , This packet The first place in the data part of should be in the whole data stream The location of the .( Pay attention to the use of “ should ”. Because for transmission without data , Such as ACK, Although it has a seq, But this transmission is throughout data stream It doesn't take up a place in . So the next actual data transmission , Will still be sent from the last time ACK Of data packets seq Start )
- Acknowledge number
It means expecting the other party ( The receiving party ) Next time sequence number How much is the
Three handshakes
Three handshakes (Three-way Handshake) In fact, it means building a TCP When the connection , Need client and server to send in total 3 A package . The main purpose of three handshakes is to confirm whether the receiving and sending capabilities of both parties are normal 、 Specify your own initialization serial number to prepare for later reliable transfer .

The first handshake : Sign a SYN = 1, Randomly generate a serial number seq1 = x
The second handshake : Sign a SYN ,ACK = 1, Confirmation no. ack = x + 1, Randomly generate a serial number seq2=y
The third handshake : Sign a ACK = 1, Confirmation no. ack = y + 1,seq2= x + 1
SYN/FIN Although there is no data, But the next transmission will packet seq Add a , however , about ACK The transmission of , Won't let the next transmission packet Add one , in other words , The next actual data transmission , Still sent from the last time ACK Of data packets seq Start calculating
Network packet capture analysis
The specific connection process is given above , But partial theory , Let's have a real fight , Analysis through packet capture

See what you do with each handshake ( It mainly depends on the serial number , Confirmation no. )



There's a picture, there's a truth , I'm not afraid you don't believe it
Reference link :https://www.jianshu.com/p/15754b4e9458
common problem
Question 1 : Why not two handshakes , It's three handshakes
In the first two handshakes, the client can confirm that the reception and transmission of the server are normal ( One back and forth ), However, the server does not know whether the sending ability of the client is normal , that TCP It's impossible to talk about the reliability of , So we need a third handshake to confirm the sending and receiving ability of both sides , In order to ensure that TCP Reliability of connection .
Question two :TCP Can all three handshakes carry data ? If not , So which handshake can carry data , Why can't others carry data ?
Suppose the first handshake, the client carries data to the server , The server parses and stores the connection information , If you have a lot of data , The server should allocate enough memory for storage , If there are hackers while(1000000000) To create a new connection , The result is predictable , The server will take up a lot of memory .
The third handshake , At this point, for the client , Connection established , There is no problem for the client to carry data
Question 3 : In transit , What if the message is lost
- The first handshake message is lost
The client sends SYN message , And then into SYN_SENT state .
The client fails to receive the information from the server SYN-ACK message , It will trigger the timeout retransmission mechanism of the client .
stay Linux in , Client's SYN The maximum number of retransmissions of the message is determined by /proc/sys/net/ipv4/tcp_syn_retries Kernel parameter control , This parameter can be customized , The default value is usually 5. Each timeout is the last time 2 times . When the fifth timeout retransmission , Will continue to wait 32 second , If the server still does not respond ACK, The client will no longer send SYN package , Then disconnect TCP Connect .
- The second handshake message is lost
The second handshake , The server will enter SYN_RCVD state
Client angle : The message sent by the client's first handshake did not get a reply , Then the client will feel its own SYN Message lost , Then the client will trigger the timeout retransmission mechanism , Retransmission SYN message .
Server angle : send out SYN_ACK A timer will be started after the message , If the message is not answered , The timeout retransmission mechanism will be triggered , Retransmission SYN-ACK message , The number of retransmissions is determined by /proc/sys/net/ipv4/tcp_synack_retries control , The default is 5 Time .
- The third handshake message is lost
Server angle : send out SYN_ACK A timer will be started after the message , If the time set by the timer is exceeded, the client does not receive ACK, It will be reissued SYN_ACK package . from /proc/sys/net/ipv4/tcp_synack_retries control , The default is 5 Time .
Client angle : Wound up , Now I am ACK message , Have unique permissions , So I won't repeat it
Question 4 : When the message fails or is discarded
- The semi connection queue of the server (syns quene) Full of , The client has been timeout retransmission SYN message , Until the maximum number of retransmissions is reached
- The connection queue of the server (accept quene) Full of

TCP Full connection and semi connection queues
When called by the server listen() When the function listens to the port , The kernel will listen for each socket Create two queues
- Semi connected queues (syn queue): The client sends SYN package , The server will reply after receiving it SYN+ACK after , Server access SYN_RCVD state , At this time socket Will be placed in the semi connected queue .
- Full connection queue (accept queue): When the server receives the ACK after ,socket It will be moved from the semi connected queue to the full connected queue . When calling accpet Function , The available... Is returned from the head of the full connection queue socket Give the user process .
stay 4.3 Kernel before version ,SYN The maximum size of the queue used to be
net.ipv4.tcp_max_syn_backlogTo configure the , But it's no longer used .
Now usenet.core.somaxconnTo represent at the same time SYN Queue and Accept The maximum size of the queue
View a port ( That is, a service ) The connection condition of
ss command
ss yes Socket Statistics Abbreviation . seeing the name of a thing one thinks of its function ,ss Commands can be used to get socket Statistics , It can show and netstat Similar content .ss The advantage is that it can show more details about TCP And connection status information , And ratio netstat Faster and more efficient .
# ss
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port
tcp ESTAB 0 0 10.0.2.10:ssh 10.0.2.2:52316
Recv-Q: Semi connected queues
Send-Q: Full connection queue
版权声明
本文为[Luo Luo's 1024]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231426114313.html
边栏推荐
- The initial C language framework is suitable for review and preliminary understanding
- SHT11传感器的温度湿度监控报警系统单片机Proteus设计(附仿真+论文+程序等)
- I/O复用的高级应用之一:非阻塞 connect———使用 select 实现(也可以用 poll 实现)
- Upgrade of openssh and modification of version number
- Solve the problem of SSH configuration file optimization and slow connection
- Contraction mapping theorem
- I thought I could lie down and enter Huawei, but I was confused when I received JD / didi / iqiyi offers one after another
- raised exception class EAccexxViolation with ‘Access violation at address 45EFD5 in module 出错
- 51单片机+LCD12864液晶显示的俄罗斯方块游戏,Proteus仿真、AD原理图、代码、论文等
- 【工厂模式详解】工厂方法模式
猜你喜欢

直流可调稳压电源的Proteus仿真设计(附仿真+论文等资料)

LotusDB 设计与实现—1 基本概念

电子秤称重系统设计,HX711压力传感器,51单片机(Proteus仿真、C程序、原理图、论文等全套资料)

Electronic perpetual calendar of DS1302_ 51 single chip microcomputer, month, day, week, hour, minute and second, lunar calendar and temperature, with alarm clock and complete set of data

do(Local scope)、初始化器、内存冲突、Swift指针、inout、unsafepointer、unsafeBitCast、successor、

AT89C52 MCU frequency meter (1Hz ~ 20MHz) design, LCD1602 display, including simulation, schematic diagram, PCB and code, etc

Don't you know the usage scenario of the responsibility chain model?

capacitance

抑郁症治疗的进展

Chapter 7 of JVM series -- bytecode execution engine
随机推荐
你还不知道责任链模式的使用场景吗?
八路抢答器系统51单片机设计【附Proteus仿真、C程序、原理图及PCB文件、元器件清单和论文等】
8.5 循环神经网络简洁实现
DS1302的电子万年历_51单片机,年月日、星期、时分秒、农历和温度,带闹钟,全套资料
四层和八层电梯控制系统Proteus仿真设计,51单片机,附仿真和Keil C代码
UML项目实例——抖音的UML图描述
GIS数据处理-cesium中模型位置设置
数组模拟队列进阶版本——环形队列(真正意义上的排队)
MDS55-16-ASEMI整流模块MDS55-16
DVWA之暴力破解(Brute Force)Low-->high
Select receives both normal data and out of band data
单相交交变频器的Matlab Simulink建模设计,附Matlab仿真、PPT和论文等资料
一款不错的工具:aardio
全连接层的作用是什么?
Don't you know the usage scenario of the responsibility chain model?
OC 转 Swift 条件编译、标记、宏、 Log、 版本检测、过期提示
MySQL error packet out of order
你還不知道責任鏈模式的使用場景嗎?
ASEMI整流模块MDQ100-16在智能开关电源中的作用
LM317的直流可调稳压电源Multisim仿真设计(附仿真+论文+参考资料)