3
votes

I have a client/server application written in C using TCP sockets. I wanted to know dead server processes using SO_KEEPALIVE option enabled on client socket. I am using Linux.

I modified the default time from 2 hours to 10 minutes.

echo 600 > /proc/sys/net/ipv4/tcp_keepalive_time

I enabled SO_KEEPALIVE on client socket using setsockopt(). I intentionally killed(kill -9) the server process while it's sending data to client.

As expected, after 10 minutes timeout(plus additional time for probes), client socket got notified (read(scoket,...) returned zero).

However, to my surprise, even if I disable this option on client socket, it still gets notified after the specified timeout(read() returns zero).

Is this behavior by default enabled in Linux?

Also, I felt read() returning zero to be inappropriate, shouldn't read() return some error when the peer is dead?

1
When you kill a Linux process (or if the process crashes) all sockets are cleanly shut down and the connection with the peer is reset. If you want to test real dead clients you would have to firewall off all traffic from the server or unplug the cable or crash the kernel.eckes
You can check by doing a getsockopt...rogerdpack

1 Answers

5
votes

Keepalive causes a connection reset. The only thing that causes read() to return zero is receiving a FIN. Ergo, you received a FIN, not a keepalive termination, and ergo this doesn't show that keepalive is enabled by default in Linux. It would be a violation of RFC 1122.