TCP sockets automatically close after a while if no data is sent?

I have a situation with the client server, when the client opens a TCP socket to the server, and sometimes long periods of time pass without sending data between them. I ran into a problem when the server tries to send data to the client, and it seems successful, but the client never received it, and after a few minutes it looks like the client is disconnecting.

Do I need to send a keep alive packet from time to time?

Change Please note: this happens with peers on the same computer. The computer is located behind NAT, which forwards the number of ports used for this computer. A client that connects to the server opens a connection through DNS. i.e. it uses mydomain.net and port to connect.

+6
source share
4 answers

On Windows, sockets without sending data are a big source of problems in many applications and must be handled correctly.

The problem is that the SO_KEEPALIVE period can be set at the system scale (otherwise it will be useless by default for two hours) or with the later winsock API.

Therefore, many applications sometimes and individually send some random data byte (to ignore the peer) only so that the network layer declares the disconnect after the ACK has not been received (after all proper retransmissions by the layer and ack timeout) .

Answering your question: no, sockets are not automatically disabled.

However, you must be careful with the above problem. What makes it even more complicated is that testing this behavior is very difficult. For example, if you configured everything correctly, and you expect to detect a shutdown properly, you cannot test it by disabling the physical layer. This is due to the fact that the network adapter will perceive carrier loss, and the socket level will signal to close all application sockets that relied on it. A good way to check this is to connect two computers with three legs and two switches between them, disconnecting the middle leg, thereby preventing loss of media, but still physically shutting down the machines.

+7
source

It is safer to use the Keep-alive parameter ( SO_KEEPALIVE under linux ) to prevent disconnection due to inactivity, but this can lead to the creation of additional packages.

This sample code does this under linux:

int val = 1; .... // After creating the socket if (setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, (char *)&val, sizeof(val))) fprintf(stderr, "setsockopt failure : %d", errno); 

Sincerely.

0
source

There is a timeout built into TCP, but you can adjust it, see the SendTimeout and ReciveTimeout Socket classes , but I have a suspicion that this is not your problem. A NAT router can also have an expiration time for TCP connections before it removes it from the port forwarding table. If traffic does not pass on the router during this timeout, it blocks all incoming traffic (since it clears the forwarding information from its memory so that it does not know which computer sends the traffic), the outgoing connection will probably also have a different source port, so that the server cannot recognize it as the same connection.

-1
source

TCP sockets do not close automatically. However, TCP connections. But if this happens between peers in the same computer, the connection should never be dropped until both peers exist and their sockets are open.

-2
source

Source: https://habr.com/ru/post/921618/


All Articles