Are socket options inherited across accept() from the listening socket?

后端 未结 4 422
北恋
北恋 2020-12-24 11:46

Suppose the listening socket passed to accept has non-default options set on it with setsockopt. Are these options (some or all of them?) inherited

4条回答
  •  一生所求
    2020-12-24 12:28

    The answer is No for POSIX conforming implementations, as I read it.

    From the POSIX-2017 spec for accept():

    The accept() function shall extract the first connection on the queue of pending connections, create a new socket with the same socket type protocol and address family as the specified socket, and allocate a new file descriptor for that socket.

    Note it is explicitly a "new socket", not a "full or partial copy of the socket being unqueued", so should have no options different from the default for that socket type and address family. While the copy behavior may be desirable, this is left as an extension interface a platform may have. I haven't seen that any platform does implement one, however, so it could be added to the standard. It is therefore on the application to use getsockopt()/setsockopt() to copy any attributes, that differ from the defaults, from the queue socket to the returned socket, not the responsibility of the interface, before any use of that socket to send or receive data.

提交回复
热议问题