Setting The SO_LINGER Option

Added April 2010

You may set the SO_LINGER socket option to "on" by passing the flag TCPXFLG_LINGER and then putting the desired linger time (in seconds) into the 4th (high byte of B,4) byte of FLAGS during a TCPOP_CONNECT or TCPOP_ACCEPT call. For example:

TCPOP = TCPOP_CONNECT               ! (or TCPOP_ACCEPT)

FLAGS = FLAGS or TCPXFLG_LINGER    ! activate SO_LINGER option

FLAGS = FLAGS + (5 * (2 ** 24))     ! set timer to 5 seconds

The SO_LINGER option affects the behavior of the TCPOP_CLOSE operation, relative to data that has been sent but may not yet have been delivered. There are three possibilities:

By default, the SO_LINGER option is turned off, which means that the close operation returns immediately, but if there is any data still remaining in the socket send buffer, the system will try to deliver it to the remote end.

If SO_LINGER is turned on, and the timer value is 0, then the connection will be aborted when TCPOP_CLOSE is called. Any data still remaining in the socket send buffer will be discarded, and a reset packet (RST) will be sent to the remote end, rather than the normal termination sequence.

If SO_LINGER is turned on, and the timer value is non-zero, then the kernel will "linger" when the socket is closed either all of the data in the send buffer has been sent and acknowledged by the remote end, or the timer expires. If the socket is blocking, TCPOP_CLOSE will not return until one of those conditions is met. (If non-blocking, it will return, possibly with STATUS set to EWOULDBLOCK, but the kernel will still linger as just described.)

Note that the SO_LINGER timer should not be confused with the TCPX.SBR TIMER parameter, which relates to the time until the connection is made or accepted.