how I will use setsockopt and getsockopt with KEEP_ALIVE in linux c programming to determine broken tcp/ip connection?
c, linux, tcp
Solution
From the TCP man page:
To set or get a TCP socket option, call `getsockopt(2)` to read or `setsockopt(2)` to write the option with the option level argument set to `IPPROTO_TCP`.
Here are the relevant options:
`TCP_KEEPCNT` (since Linux 2.4)
The maximum number of keepalive probes TCP should send before dropping the connection. This option should not be used in code intended to be portable.
`TCP_KEEPIDLE` (since Linux 2.4)
The time (in seconds) the connection needs to remain idle before TCP starts sending keepalive probes, if the socket option SO_KEEPALIVE has been set on this socket. This option should not be used in code intended to be portable.
`TCP_KEEPINTVL` (since Linux 2.4)
The time (in seconds) between individual keepalive probes. This option should not be used in code intended to be portable.
Example:
int keepcnt = 5;
int keepidle = 30;
int keepintvl = 120;
setsockopt(sock, IPPROTO_TCP, TCP_KEEPCNT, &keepcnt, sizeof(int));
setsockopt(sock, IPPROTO_TCP, TCP_KEEPIDLE, &keepidle, sizeof(int));
setsockopt(sock, IPPROTO_TCP, TCP_KEEPINTVL, &keepintvl, sizeof(int));
Problem
how I will use setsockopt and getsockopt in linux c programming to determine broken tcp/ip connection?