当前位置:网站首页>What would happen if disconnecting during the process of TCP connection?

What would happen if disconnecting during the process of TCP connection?

2022-08-10 22:31:00 TABE_

In response to this issue, it is necessary to discuss in different scenarios:

  1. After unplugging the network cable, there is data transmission;
  2. After unplugging the network cable, there is no data transmission;

Data transmission after unplugging the network cable

After the client unplugs the network cable, the data packet sent by the server to the client will not receive any response. After waiting for a certain period of time, The server will trigger a timeout retransmission mechanism, retransmit data packets that do not receive a response.

If the client just plugs the network cable back in while the server is retransmitting the message.Since unplugging the network cable does not change the TCP connection status of the client, and it is still in the ESTABLISHED state, the client can normally receive the data packet sent by the server, and then the client will return an ACK response packet.At this point, the TCP connection between the client and the server still exists, and it feels like nothing happened.

But if the client has not plugged the network cable back in the process of retransmitting the message from the server.When the number of times the server retransmits the message over time reaches a certain threshold, the kernel will determine that there is a problem with the TCP, and then tell the application that there is a problem with the TCP connection through the Socket interface, so the TCP connection on the server will be disconnected.After the client is plugged back into the network cable, if the client sends data to the server, since the server has no TCP connection with the same quadruple ancestor as the client, the server kernel willReply to the RST message, and the client will release the TCP connection after receiving it.At this point, both the client and server TCP connections have been disconnected.

No data transfer after unplugging the network cable

  1. If the TCP keepalive mechanism is not enabled by both parties, after the client unplugs the network cable, if the client does not plug in the network cable, the TCP connection state between the client and the server will always exist.
  2. If both parties have enabled the TCP keepalive mechanism, then after the client unplugs the network cable, if the client does not plug back the network cable, the TCP keepalive mechanism will detect that the other party's TCP connection is not alive, so it will disconnect the TCP connection.However, if the client plugs back the network cable during the TCP detection period, the original TCP connection between the two parties can still exist normally.

KeepAlive mechanism: When there is no data transmission in the connection for a period of time, TCP automatically sends a message with empty data to the other party. If the other party responds to this message, it means that the other party is still online and connected.It can continue to be maintained. If the other party does not return a message and retries many times, the connection is considered lost, and there is no need to maintain the connection.

原网站

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