专注于音视频处理及流媒体开发!

罗索实验室

What exactly does SO_LINGER do?

时间:2017-04-05 18:25来源:developerweb 作者:Cyrus Patel 点击:
SO_LINGER affects the behaviour of the close() operation as described below. No socket operation other than close() is affected by SO_LINGER.
TAG: setsockopt  SO_LINGER  


Contributed by Cyrus Patel
SO_LINGER affects the behaviour of the close() operation as described below. No socket operation other than close() is affected by SO_LINGER.

[highlight]
The following description of the effect of SO_LINGER has been culled
from the setsockopt() and close() man pages for several systems, but
may still not be applicable to your system. The range of differences
in implementation ranges from not supporting SO_LINGER at all; or
only supporting it partially; or having to deal with the
"peculiarities" in a particular implementation. (see portability
notes at end).

Moreover, the purpose of SO_LINGER is very, very specific and only a
tiny minority of socket applications actually need it. Unless you are
extremely familiar with the intricacies of TCP and the BSD socket
API, you could very easily end up using SO_LINGER in a way for which
it was not designed.
[/highlight]

The effect of an setsockopt(..., SO_LINGER,...) depends on what the values in the linger structure (the third parameter passed to setsockopt()) are:

Case 1:  linger->l_onoff is zero (linger->l_linger has no meaning):
            This is the default.

On close(), the underlying stack attempts to gracefully shutdown the connection after ensuring all unsent data is sent. In the case of connection-oriented protocols such as TCP, the stack also ensures that sent data is acknowledged by the peer.  The stack will perform the above-mentioned graceful shutdown in the background (after the call to close() returns), regardless of whether the socket is blocking or non-blocking.

Case 2: linger->l_onoff is non-zero and linger->l_linger is zero:

A close() returns immediately. The underlying stack discards any unsent data, and, in the case of connection-oriented protocols such as TCP, sends a RST (reset) to the peer (this is termed a hard or abortive close). All subsequent attempts by the peer's application to read()/recv() data will result in an ECONNRESET.

Case 3: linger->l_onoff is non-zero and linger->l_linger is non-zero:

A close() will either block (if a blocking socket) or fail with EWOULDBLOCK (if non-blocking) until a graceful shutdown completes or the time specified in linger->l_linger elapses (time-out). Upon time-out the stack behaves as in case 2 above.

---------------------------------------------------------------

Portability note 1: Some implementations of the BSD socket API do not implement SO_LINGER at all. On such systems, applying SO_LINGER either fails with EINVAL or is (silently) ignored. Having SO_LINGER defined in the headers is no guarantee that SO_LINGER is actually implemented.

Portability note 2: Since the BSD documentation on SO_LINGER is sparse and inadequate, it is not surprising to find the various implementations interpreting the effect of SO_LINGER differently. For instance, the effect of SO_LINGER on non-blocking sockets is not mentioned at all in BSD documentation, and is consequently treated differently on different platforms. Taking case 3 for example: Some implementations behave as described above. With others, a non-blocking socket close() succeed immediately leaving the rest to a background process. Others ignore non-blocking'ness and behave as if the socket were blocking. Yet others behave as if SO_LINGER wasn't in effect [as if the case 1, the default, was in effect], or ignore linger->l_linger [case 3 is treated as case 2]. Given the lack of
adequate documentation, such differences are not (by themselves) indicative of an "incomplete" or "broken" implementation. They are simply different, not incorrect.

Portability note 3: Some implementations of the BSD socket API do not implement SO_LINGER completely. On such systems, the value of linger->l_linger is ignored (always treated as if it were zero).

Technical/Developer note: SO_LINGER does (should) not affect a stack's implementation of TIME_WAIT. In any event, SO_LINGER is not the way to get around TIME_WAIT.  If an application expects to open and close many TCP sockets in quick succession, it should be written to use only a fixed number and/or range of ports, and apply SO_REUSEPORT to sockets that use those ports.

Related note: Many BSD sockets implementations also support a SO_DONTLINGER socket option. This socket option has the exact opposite meaning of SO_LINGER, and the two are treated (after inverting the value of linger->l_onoff) as equivalent. In other words, SO_LINGER with a zero linger->l_onoff is the same as SO_DONTLINGER with a non-zero linger->l_onoff, and vice versa.

(责任编辑:jackyhwei)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [/a/201704/17507.html]
本文出处:developerweb 作者:Cyrus Patel 原文
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容