The linger structure maintains information about a specific socket that specifies how that socket should behave when data is queued to be sent and the closesocket function is called on the socket.
typedef struct linger {
u_short l_onoff;
u_short l_linger;
} LINGER, *PLINGER, *LPLINGER;
l_onoff
Specifies whether a socket should remain open for a specified amount of time after a closesocket function call to enable queued data to be sent. This member can have one of the following values.
l_linger
The linger time in seconds. This member specifies how long to remain open after a closesocket function call to enable queued data to be sent. This member is only applicable if the l_onoff member of the linger structure is set to a nonzero value.
This value is set if the setsockopt function is called with the optname parameter set to SO_LINGER. The optval parameter passed to the setsockopt function must contain a linger structure that is copied to the internal linger structure maintained for the socket.
The l_onoff member of the linger structure determines whether a socket should remain open for a specified amount of time after a closesocket function call to enable queued data to be sent. Somewhat confusing is that this member can be modified in two ways:
The l_linger member of the linger structure determines the amount of time, in seconds, a socket should remain open. This member is only applicable if the l_onoff member of the linger structure is nonzero.
To enable a socket to remain open, an application should set the l_onoff member to a nonzero value and set the l_linger member to the desired time-out in seconds. To disable a socket from remaining open, an application only needs to set the l_onoff member of the linger structure to zero.
If an application calls the setsockopt function with the optname parameter set to SO_DONTLINGER to set the l_onoff member to a nonzero value, the value for the l_linger member is not specified. In this case, the time-out used is implementation dependent. If a previous time-out has been established for a socket (by enabling SO_LINGER), this time-out value should be reinstated by the service provider.
Note that enabling a nonzero timeout on a nonblocking socket is not recommended.
The getsockopt function can be called with the optname parameter set to SO_LINGER to retrieve the current value of the linger structure associated with a socket.
Header: Declared in winsock2.h.
Library: Use ws2_32.lib.