当前位置:网站首页>TCP related knowledge and interview site details
TCP related knowledge and interview site details
2022-07-20 20:22:00 【zhangyubo325】
TCP
TCP Three and four waves of
Three handshakes
① The client sends out the plan to establish TCP Connection request message , In the header of the message SYN position =1, It indicates that the message cannot carry data , But it takes a sequence number (seq).TCP Is oriented to a byte stream , In a TCP Each byte in the byte stream transferred in a connection is numbered in order , This number is the serial number (seq). Select an initial sequence number in the request to establish connection message seq=X, But do not carry data . here ,TCP The client process enters SYN-SENT( Sync sent ) state .
② After the server listening port receives the request , If you agree to establish a connection , Send a confirmation to the client . In the confirmation message segment SYN Bit and ACK All bits are set to 1, It also consumes a serial number and selects an initial serial number seq=Y.ACK position =1 It indicates that the confirmation number in the message is valid , The confirmation number is ack=X+1, Indicates that the next sequence number you want the client to transmit is X+1( Because the client has sent a serial number in the first step x Message of ). here TCP The server process enters SYN-RCVD( Sync received ) state .
③ After the client receives the confirmation from the server , Also send a confirmation to the server , Therefore, the... Of the confirmation message segment is first set ACK Set as 1, Confirmation no. ack=Y+1, Indicates that you want the next sequence number transmitted on the server side to be Y+1( Because the server has sent a serial number in the previous step y Message of ), And my serial number is seq=X+1. here ,TCP Connection established , Both sides enter ESTABLISHED( Connection established ) state .
SYN: Request link FIN: break link ACK: reply RST: Reset connection PSH: Push data URG: Pointer to an emergency
Use popular words to describe the above three handshake processes !
① client : The server , Can we establish a connection ?
② The server : B: yes, you can , Let's make a connection !
③ client : received , Make a connection ! Then build TCP The connection is successful if it is four handshakes , It's like this
① client : The server , Can we establish a connection ?② The server : B: yes, you can , Let's make a connection !③ client : received , Let's make a connection !④ The server : received , Let's make a connection !
1 Bitstream protocol
Suitable for extremely bad network conditions
back off n agreement
Back to n individual All of them will be retransmitted later
Choose retransmission protocol
Which one is lost Retransmit which
Why does the connection handshake need three times
Why should there be a third handshake , because b I wonder if this bag is a The sultry bag , May be a One of the multiple Teaser packets sent , This is a late bag , There is no need to accept .“ Three handshakes ” Is the purpose of “ In order to prevent the invalid connection request message segment from suddenly transferring to the server , So there's a mistake ”
Two handshakes , The client knows that the server has the ability to receive and send information , But the server only knows that the client has the ability to send information , But I don't know that the client has the ability to receive information , If you add the second 3 The second handshake , The server knows that the client has the ability to receive information ( Because the client receives the message and replies to the server ).
Now consider such a situation , Due to network congestion , The first connection request message segment sent by the client ( The first handshake ) In some network nodes for a long time , It will take some time to reach the server .
Then, because the client does not receive the confirmation message from the server , The client considers this connection request message segment ( The first handshake ) It doesn't work , Then resend the connection request message , This time, I didn't stay in the network because of network congestion , Successfully sent to the server , Because it's two handshakes , So after shaking hands twice , The connection is established .
The client thinks that the initial request connection message stuck in the network due to network congestion has failed , It doesn't actually fail , Just stay in the network due to the congestion of the network , here , The network is not blocked , It's unblocked again , The message successfully arrived at the server , The server thinks that the client should communicate with the server TCP The connection of , So the server responds to the connection request message , Agree to establish a connection . Assume no third handshake , So as long as the server sends out a confirmation , A new connection is established .
Because now the client does not send out the request to establish the connection , Therefore, the server-side confirmation will not be ignored , It will not send data to the server . But the server side thinks that the new transportation connection has been established , And has been waiting for the client to send data . Many resources on the server side are wasted .
therefore , A third handshake is needed to solve the problem , The third handshake is the confirmation message sent by the client to the server , If the server does not receive the reply confirmation message from the client after the specified time , Therefore, resources will not be allocated to clients , therefore , This connection has been abandoned , It won't consume the resources on the server .
Four waves
Simply put, don't write , Don't read and confirm , Stop writing and let others confirm .
TIME_WAIT Two functions
- Ensure that the connection is terminated normally
- Allow old repeating segments to disappear in the network
Why? TIME-WAIT Status needs to pass 2msl( Maximum message lifetime ) Before returning CLOSE state
Although all four messages have been sent , Can enter the CLOSE state , But the network may have the illusion of unreliability , Maybe the last one ACK The loss of ,TIME-WAIT Status is used to resend messages that may be lost .
wait for 2msl The meaning of : Reliable termination TCP Connect 、 Promise to be late TCP The message has enough time to be recognized and discarded ;
1、 Guarantee A Sent ACK Be able to get to B, This message may be lost , be in LAST-ACK Of B I can't receive the message sent to myself FIN and ACK Message confirmation ,B Over time retransmission FIN and ACK message , So in 2msl Received this retransmission in time FIN+ACK message , next A Retransmission once ;
2、 In the meantime , Define the connector for this connection ( Customer's IP Address and port number , Server's IP Address and port number ) It can no longer be used , This connection can only be in 2msl It can be used only after it is finished ;
3、 In order to make old packets disappear in the network due to expiration , Each specific TCP You must select a message maximum lifetime MSL, It is the longest time in the network before any message field is discarded .
Why wave four times
Refer to the three grip mobile phone system , It takes at least three waves , If only three times , The client sends the data request to disconnect , And the server may not also send data , If you return at the same time ACK and FIN To the client , disconnect , May cause data damage ; If sent first ACK, Wait again B Send the data after sending FIN and ACK, The integrity of the transmitted data can be guaranteed .
tcp It's full duplex mode , Received FIN It means that no data will be sent again , But you can still send data .
TCP How to realize flow control
TCP Use sliding window protocol for flow control . Imagine sending data at a high rate on the sending side and receiving it at a low rate on the receiving side , In order to ensure that the data is not lost , Flow control is clearly needed , Coordinate the working rhythm of both sides of communication . So called sliding window , Can be understood as the size of the buffer that the receiver can provide .TCP Use a sliding window to tell the sender how much buffer it can provide for the data it sends , This protocol can speed up data transmission , Improve network throughput .
TCP How to implement congestion control
Congestion control is also called window based congestion control , So , The sender maintains a window called a congestion window cwnd(congestion window) State variable of , The size of the congestion window depends on how congested the network is , And changing dynamically , The sender makes its sending window equal to congestion window ( notes : Here we assume that the receiver always has enough cache space , Therefore, the size of the transmission window depends on the congestion degree of the network , In fact, the real window value = min( Receiver window , Congestion window ) The sender controls the congestion window cwnd The principle of : As long as the network is not congested , The congestion window can be increased , In order to send out more groups . But as long as there is congestion or possible congestion in the network , We have to reduce the congestion window , To reduce the number of packets injected into the network , So that the congestion of link network .
How to know that there is congestion in the network ?
- When congestion occurs in the network , The router will drop the packet , Therefore, as long as the sender does not receive the confirmation message that should arrive in time , In other words, as long as there is a timeout , We can guess that the network may be congested ,( Now the transmission quality of communication lines is generally very good , The probability of discarding due to transmission and output errors is very small ( Far less than 1%)) therefore , The basis for judging network congestion is whether there is a timeout .
- The receiver establishes a fast retransmission mechanism , If a bag is lost , Then continue to send retransmission request for the subsequent packet ; Once the sender receives three identical acknowledgments , I knew that there was an error after the package , Retransmit the bag immediately ;
边栏推荐
猜你喜欢
随机推荐
Source Insight使用技巧
发力5G笔记本市场,闻泰科技加入“鸿鹄计划”
uniapp自定义应用退出执行内容
性能优化:SPA单页应用首屏加载速度慢怎么解决?
Detailed explanation of histogram2d() function of numpy
我有 7种 实现web实时消息推送的方案,7种!
The meaning of learning (I) the root cause of love problems comes from ignorance
numpy的histogram2d()函数详解
IPv4和IPv6的区别
HPA of kubernetes
STL 笔记(一):知识体系
PyGame official document [translation] - pygame freetype
NextTick 原理分析
Step by step towards responsive programming (III) - common functional interfaces - function < T, R>
【云驻共创】与最年轻的云服务HCIE一起设计华为云存储架构(上篇)
js判断是对象还是数组
Club + development process + mental process + reasons for failure + on the road again
TR069 event type (meaning of event code)
Win11查看文件资源管理器选项卡的方法
golang面试-代码编写题1-14