Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do TCP sockets automatically close after some time if no data is sent?

I have a client server situation where the client opens a TCP socket to the server, and sometimes long periods of time will pass with no data being sent between them. I have encountered an issue where the server tries to send data to the client, and it seems to be successful, but the client never receives it, and after a few minutes, it looks like the client then gets disconnected.

Do I need to send some kind of keep alive packet every once in a while?

Edit: To note, this is with peers on the same computer. The computer is behind a NAT, that forwards a range of ports used to this computer. The client that connects with the server opens the connection via DNS. i.e. it uses the mydomain.net & port to connect.

like image 378
Nick Banks Avatar asked Jul 29 '12 19:07

Nick Banks


People also ask

Do sockets close automatically?

Closing a socket multiple times will not return an error. Sockets will be automatically closed when the last reference is dropped, but you might want to call this function to make sure resources are released as early as possible.

How long does a TCP socket stay open?

Description. Once a TCP connection has been established, that connection is defined to be valid until one side closes it. Once the connection has entered the connected state, it will remain connected indefinitely.

Do TCP connections timeout?

By default, the TCP connection timeout is 15 minutes and the UDP connection timeout 30 seconds. In order to increase the connection timeout you can modify it from the firewall access rules.

How long does a socket connection stay open when idle?

Idle ESTAB is forever These sockets have no running timer by default - they will remain in that state forever, even if the communication is broken. The TCP stack will notice problems only when one side attempts to send something.


1 Answers

On Windows, sockets with no data sent are a big source for trouble in many applications and must be handled correctly.

The problem is, that SO_KEEPALIVE's period can be set system-wide (otherwise, a default is useless two hours) or with the later winsock API.

Therefore, many applications do send some occasional byte of data every now and then (to be disregarded by the peer) only to make the network layer declare disconnection after ACK is not received (after all due retransmissions done by the layer and ack timeout).

Answering your question: no, the sockets do not disconnect automatically.

Yet, you must be careful with the above issue. What complicates it further is that testing this behavior is very hard. For example, if you set everything correctly and you expect to detect disconnection properly, you cannot test it by disconnecting the physical layer. This is because the NIC will sense the carrier loss and the socket layer will signal to close all application sockets that relied on it. A good way to test it is connect two computers with 3 legs and two switches in between, disconnecting the middle leg, thus preventing carrier loss but still physically disconnecting the machines.

like image 186
Pavel Radzivilovsky Avatar answered Sep 28 '22 15:09

Pavel Radzivilovsky