SUMMARY setsockopt

From: Joe Welfeld (
Date: Thu Apr 14 1994 - 19:54:03 CDT

original post

> Greetings managers,
> I am trying to find out exactly how the
> setsockopt SO_KEEPALIVE, functions.
> I believe it transmits a request (maybe an echo?)
> for some timeout time. (how long?) and what happens
> when there is no response (retrys?) ? I read through
> Answerbook, and man pages but there was not enough info.
> Thanks in advance,
> Joe Welfeld

Thanks to all who responded. Summary follows:

----- Begin Included Message -----

To prevent tcp from hanging if packets are lost, each connection maintains
a set of timers to recover. There are four basic timers - the retransmit
timer, the persist timer, the keepalive timer, and the 2msl timer.

The keepalive timer will monitor an idle connection (no packets going back
and forth) to be sure the peer still exists. If you set this option, a
keepalive timer is set to expire which sends a keepalive packet. You
will either get an acknowledgement or a reset from a living peer. If a
few keepalive packets (about 8) are sent without an acknowledgement, the
connection is dropped. The keepalive packet is not an icmp echo. It is a
tcp packet with the sequence number set to one less than snd_una.

Normally you set this option on servers to accommodate clients that
crash without first closing the connection. Keeps your server from
developing an apparent file descriptor leak.

I think the keepalive timer is about 2 hours, but I'm not absolutely
certain. Timer values are defined in /usr/include/netinet/tcp_timer.h
in SunOS 4.1.3. Most of what I said is explained there. Check it
out for more info.

- Nate Itkin
- Portland Technology Development, Intel Corporation      Aloha, Oregon
- E-mail:

----- End Included Message -----

----- Begin Included Message -----

This is really used to keep track of the connection on the other side. To see if it dies when your not using it. For example if you have an open connection and do not use it for a while, but want to be sure its there when you need it then you would use this option.

What results when you timeout and no response?

If the other end does not respond to "messages" (keepalive), the connection is considered broken and the so_error variable is set to ETIMEOUT.

How to set th timeout value?

At creation, socket option SO_RCVTIMEO which takes int data type, I think.

//// (. .) +--------------------------oOO--(_)--OOo--------------------------+ Jim Redpath SRI International, Menlo Park CA Center for Technology Transfer Software Engineer and Integration Fort Gordon, GA Field Site Phone: (706) 798-3111 +-----------------------------------------------------------------+

----- End Included Message -----

----- Begin Included Message -----

After the socket has been idle for a couple of hours, it sends an empty TCP segment. If the connection is still valid the other system should acknowledge it. If the other system was rebooted during the idle time it will respond with a Reset segment, and the socket is marked as being invalid. If the other system doesn't respond it will retry for a few minutes; if it times out, the socket is marked invalid. If the process is in a read() or select() on the socket when it becomes invalid, it will return (read() will return an error code, while select() will indicate that the socket is ready). If the process wasn't already in read(), the next I/O operation on the socket will return an error code.

-- Barry Margolin System Manager, Thinking Machines Corp. {uunet,harvard}!think!barmar

----- End Included Message -----

This archive was generated by hypermail 2.1.2 : Fri Sep 28 2001 - 23:08:59 CDT