TCP keep alives

I have a problem with the TCP connection being dropped. The far end drops the connection after a few minutes.

I believe I need to send TCP keepalives periodically to stop the connection from dropping, but I don't see a way to do this. I tried sending a zero length buffer to tcp_send() (with the 3rd param set to 0). That didn't seem to do anything.

Then I tried sending one byte of garbage data periodically, and this keeps the connection up.

I don't want to send garbage. How can I send a proper keepalive (an empty packet with the ACK flag set)?

Dave

Parents
  • It is currently not possible to send 0-length packets with TCP. Also TcpNet does not support keep-alive mechanism. When a keep-alive timer has expired, TCP closes the connection, if not already done by far end.

    A solution is (as you have already figured out) to send a dummy TCP data packet with at least 1 byte or to close a TCP connection and open a new one when data needs to be send out. This is how our HTTP server, Telnet server and other TCP based applications work. We do not keep TCP connections alive for long time. We simply open a TCP connection, transfer some data and then close the connection.

Reply
  • It is currently not possible to send 0-length packets with TCP. Also TcpNet does not support keep-alive mechanism. When a keep-alive timer has expired, TCP closes the connection, if not already done by far end.

    A solution is (as you have already figured out) to send a dummy TCP data packet with at least 1 byte or to close a TCP connection and open a new one when data needs to be send out. This is how our HTTP server, Telnet server and other TCP based applications work. We do not keep TCP connections alive for long time. We simply open a TCP connection, transfer some data and then close the connection.

Children
More questions in this forum