Search code examples
vxworkssockets

How to set keepalive option for induvidual socket in VxWorks


Is there any way to set keepalive for induvidual socket descriptor in vxworks? I read in some documents that "SOL_TCP" option in setsockopt function will do such favors in linux. Is such facility available in VxWorks too? If so please provide related details regarding the same, like what are the include file we need to include and how to use such option etc.


Solution

  • From the VxWorks "Library Reference" manual (can be download):

    OPTIONS FOR STREAM SOCKETS

    The following sections discuss the socket options available for stream (TCP) sockets.

    SO_KEEPALIVE -- Detecting a Dead Connection

    Specify the SO_KEEPALIVE option to make the transport protocol (TCP) initiate a timer to detect a dead connection:

     setsockopt (sock, SOL_SOCKET, SO_KEEPALIVE, &optval, sizeof (optval));
    

    This prevents an application from hanging on an invalid connection. The value at optval for this option is an integer (type int), either 1 (on) or 0 (off). The integrity of a connection is verified by transmitting zero-length TCP segments triggered by a timer, to force a response from a peer node. If the peer does not respond after repeated transmissions of the KEEPALIVE segments, the connection is dropped, all protocol data structures are reclaimed, and processes sleeping on the connection are awakened with an ETIMEDOUT error.

    The ETIMEDOUT timeout can happen in two ways. If the connection is not yet established, the KEEPALIVE timer expires after idling for TCPTV_KEEP_INIT. If the connection is established, the KEEPALIVE timer starts up when there is no traffic for TCPTV_KEEP_IDLE. If no response is received from the peer after sending the KEEPALIVE segment TCPTV_KEEPCNT times with interval TCPTV_KEEPINTVL, TCP assumes that the connection is invalid. The parameters TCPTV_KEEP_INIT, TCPTV_KEEP_IDLE, TCPTV_KEEPCNT, and TCPTV_KEEPINTVL are defined in the file target/h/net/tcp_timer.h.