How do you get notified about TCP Keepalives last probe?

109 Views Asked by At

I want to be able to detect if the connection between the client and the server is lost within a reasonable time on Linux with in C programming language. I did the following:

  int flags = 1;
  setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &flags, sizeof(flags));
  int idle = 3;
  int intvl = 1;
  int cnt = 5;
  setsockopt(fd, SOL_TCP, TCP_KEEPIDLE, &idle, sizeof(idle));
  setsockopt(fd, SOL_TCP, TCP_KEEPINTVL, &intvl, sizeof(intvl));
  setsockopt(fd, SOL_TCP, TCP_KEEPCNT, &cnt, sizeof(cnt));

I checked it in Wireshark, and this succesfully turned on TCP keepalive.

I read that this feature does not close after the last keepalive probe automatically, so I assume that there is some way I can check this condition to be able to close the socket manually.

I want to do something like this for every opened sockets file descriptor periodically:

if (sentProbes >= cnt) {
  if(fd >= 0){
    close(fd);
  }
  fd = CLOSED_FD;
}

I have tried the answer of this question from Simone, but it did not work. getsockopt returned 0, and error was also 0 even after the last probe.

Is there some kind of signal or flag for this condition or I have to implement my own heartbeat-like feature on application layer?

0

There are 0 best solutions below