Hi all,
I've got a server that runs under MacOS/X and Linux. Under Linux, I've found the following code to be useful in specifying a TCP socket's behavior in the case where a client gets suddenly disconnected from the network:
int arg = 5; // send a keepalive packet after 5 seconds of inactivity
if (setsockopt(fd, SOL_TCP, TCP_KEEPIDLE, &arg, sizeof(arg)) != 0) return B_ERROR;
arg = 5; // send up to 5 keepalive packets out, then disconnect if no response
if (setsockopt(fd, SOL_TCP, TCP_KEEPCNT, &arg, sizeof(arg)) != 0) return B_ERROR;
arg = 2; // send a keepalive packet out every 2 seconds (after the 5 second idle period)
if (setsockopt(fd, SOL_TCP, TCP_KEEPINTVL, &arg, sizeof(arg)) != 0) return B_ERROR;
I'd like to be enable this behavior under MacOS/X as well, so that if a connected client suddenly goes away, the server will close that connection within the next 15 seconds or so. Is there an equivalent to the above for MacOS/X, other than altering the application-level protocol to send dummy data back and forth?
All I can find in Mac's "man tcp" page is TCP_KEEPALIVE, which only lets me specify how long before the first keepalive packet is sent; it doesn't let me specify how many keepalive packets should be sent before disconnecting the TCP session, or the time interval between successive keepalive packets.