background : Some days ago the team in the terminal equipment and server long connection business testing, found that a situation: after unplugging the device side of the network cable, and then plug in the network cable, and sometimes can continue to normal long-run requests, and the use or unplug the cable before the long connection. However, sometimes it is not possible to continue the normal long connection request and need to reestablish a new long connection. I am particularly surprised that the first cable disconnected and plugged in after the long connection can be restored, completely overturned I have been holding a so-called "physical connection" concept. What the hell is going on, let's find out.
First of all, I invented the "physical connection" of the term, anyway I am a network programming "veteran". Often give new people and other questions to ask my colleagues to instill a concept, as long as the network cable unplugged, indicating that the physical connection is broken, not to mention the logical TCP long connection, and then plug in the network cable can only establish a new connection to continue the request. I would like to make a simple analogy: I understand the TCP long connection as before we used the telephone, A and B call the process, if one of the phone line is unplugged, the connection is completely broken. Even if we plug in the phone line, it is impossible to automatically resume the call, we have to re-dial.
I found that the connection will be restored after inserting the network cable, I initially thought it was a simple TCP socket multiplexing situation, but found that the device side did not write automatic reconnection logic, which makes me curious and skeptical. So I asked a colleague to cooperate with me to carry out a number of tests, found that after unplugging the network cable for this TCP long connection may occur in two cases.
First to do the groundwork, have done network programming friends should know such a situation
It is not possible to make any sense of the network cable disconnection, unless we configure the operating system's so_keepalive option or apply the heartbeat detection on the application layer. Please refer to the article "Network Programming Explanation: TCP semi-open connection processing."
If the network cable is disconnected for a short time, within the detection interval set by the so_keepalive, and the ends of this period, there are no networking operations for this long connection. When the network cable is connected, the TCP connection can be restored automatically, and then the normal operation will continue.
If the network cable is disconnected for a long time, it exceeds the probe interval set by the so_keepalive, or there are any networking operations for this long connection at both ends. A etimedout or econnreset error occurs when a network cable is connected. You must re-establish a new long connection for network operation.
After this, I can no longer dare to invent nouns, whining ...
This article is from "Friends Forever" blog, please make sure to keep this source http://yaocoder.blog.51cto.com/2668309/1589919
Network programming Explanation: What happens when a TCP connection pulls out a network cable