BSD Sockets Interface Programmer's Guide
68 Chapter 3
Advanced Topics for Stream Sockets
Socket Options
Table 3-1 Summary Information for Changing Socket Buffer Size
SO_LINGER
SO_LINGER controls the actions taken when a close is executed on a
socket that has unsent data. This option can be cleared by toggling. The
default is off.
The linger timeout interval is set with a parameter in the setsockopt
call. The only useful values are zero and nonzero:
• If l_onoff is zero, close returns immediately, but any unsent data is
transmitted (after close returns).
• If l_onoff is nonzero and l_linger is zero, close returns immediately,
and any unsent data is discarded.
• If l_onoff is nonzero and l_linger is nonzero, close does not return
until all unsent data is transmitted (or the connection is closed by the
remote system).
In the default case (SO_LINGER is off), close is not blocked. The socket
itself, however, goes through graceful disconnect, and no data is lost.
Here is an example:
int result;
struct linger linger;
linger.l_onoff = 1;
/*0 = off (l_linger ignored), nonzero = on */
linger.l_linger =1;
/*0 = discard data, nonzero = wait for data sent */
result = setsockopt(s, SOL_SOCKET, SO_LINGER, &linger,
sizeof(linger));
SocketType (Protocol) stream (TCP)
When Buffer Size
Increase Allowed
at any time
When Buffer Size
Decrease Allowed
only prior to establishing a
connection
Maximum Buffer Size 262144 bytes