Is TCP keepalive (with small timeouts) preventing client from hanging on recv, after the server is dead?
The scenario:
Server and client running on separate machines:
During the client sleep, the server is plugged off, now:
Setting KEEPALIVE options:
void TCPclient::setkeepalive()
{
int optval;
socklen_t optlen = sizeof(optval);
/* Check the status for the keepalive option */
if(getsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &optval, &optlen) < 0) {
throw std::string("getsockopt");
}
optval = 1;
optlen = sizeof(optval);
if(setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &optval, optlen) < 0) {
close(s);
exit(EXIT_FAILURE);
}
optval = 2;
if (setsockopt(sock, SOL_TCP, TCP_KEEPCNT, &optval, optlen) < 0) {
throw std::string("setsockopt");
}
optval = 15;
if (setsockopt(sock, SOL_TCP, TCP_KEEPIDLE, &optval, optlen) < 0) {
throw std::string("setsockopt");
}
optval = 15;
if (setsockopt(sock, SOL_TCP, TCP_KEEPINTVL, &optval, optlen) < 0) {
throw std::string("setsockopt");
}
}
linux 3.2.0-84-generic
Keepalive becomes active when the line has been idle for 15 secs. In your case Keepalive kick off timeout is 15 secs, the sleep is 10 secs, which means "Hello server" will be the next command to be sent after the server is killed.
Your Linux will try to retransmit the message several times. Keepalive still won't be triggered. The connection will break after the limit of retries is reached - that will take 10-30 minutes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With