当前位置:网站首页>After a/b machine is connected normally, B machine suddenly restarts. Ask a what is the TCP status at this time? How to eliminate this state in the server program?

After a/b machine is connected normally, B machine suddenly restarts. Ask a what is the TCP status at this time? How to eliminate this state in the server program?

2022-06-11 01:21:00 zhen12321

This is a very interesting problem . ha-ha .

First , be based on tcp Come here , In the course of such a communication , It probably takes a few steps , for example tcp Three and four waves of .
that , The difference between three times and four times is that there is no need to transmit data , Don't have to etc. 1 Round trip time , Save bandwidth .
that ,SYN Mainly used for connection confirmation ,FIN It is mainly used for initialization confirmation when one party abandons the connection ,RET What is the ?
This is the key to the problem .
linux Generally speaking, the kernel will have a lot of time to cover up , To ensure a maximum lifetime of a connection .
stay ubuntu18.04 On , once tcp The maximum lifetime of a connection is several days , Each time a connection is initiated by both parties, a ACK The confirmation of . This confirmation message is critical , It was this link before the confirmation was received , Not disconnected . that ubuntu18.04 Well , The confirmation time is 5 minute .
that , When a party suddenly hangs up , The other party cannot know that the other party hangs up , Because they didn't give it to themselves FIN To break off , If you don't take the initiative to contact each other , Then the connection will be maintained , Until the time of the operating system comes .
Say it again , In fact, it's easy to say how long the operating system has been running , Because that time every time there is a new TCP When the activity comes, it will refresh . While the latter ACK Mainly in the ACK It will refresh itself when it comes . because TCP Links are expensive , So I put forward keep-alive This field is used to reuse as many requests as possible in one connection , It can't be used as a magic medicine to reuse it all the time .
keep-alive You can set the timeout time , This is the timeout of the application layer , Is the time controlled by the application layer , The priority weight is greater than the lower level .
So in B Within one end of the time of hang up and restart ,A Also a bit this connection remains , So it will always exist , know B Restart and naturally enter listen state , This is the time ,B I don't know myself and A There were connections before .
At this point, the only thing that can change the status quo is A,A Take the initiative to send any message to B,B It was detected that the communication was abnormal , Will ask to reset the connection , So send RST to A,A After receiving it, you will reset yourself tcp Various times of connection . then A It is in the critical state of sending messages to the kernel , It will naturally enter SYN_SEND state .

原网站

版权声明
本文为[zhen12321]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206110015558925.html