5

If I were to create a TCP socket with the TCP_NODELAY option enabled and use it to listen for new connections, would new sockets returned by accept also have TCP_NODELAY enabled?

Different sockets can have differently configured options, though it would make sense that sockets returned by accept inherit their options from the listening socket that spawned them. Is this implementation-dependent?

Wingblade
  • 9,585
  • 10
  • 35
  • 48
  • The first question to be answered here is whether setting TCP_NODELAY works at all on a listening socket. What happened when you tried it? – user207421 Dec 26 '17 at 09:55
  • The second question to be answered is where did you get the idea that 'options like TCP_NODELAY are enabled on a per-socket basis', which rules out the idea of inheritance from the listening socket altogether, which is certainly not correct. – user207421 Dec 26 '17 at 10:20
  • @EJP doing `setsockopt` immediately after `listen` returns no error and subsequent `getsockopt` indicates the parameter is set. If this behaviour is implementation-dependent this may not be the case on all systems, which is why I'm asking (otherwise I would've just tested it myself). – Wingblade Dec 26 '17 at 10:22
  • @EJP "This flag (TCP_NODELAY) is an option that can be enabled on a per-socket basis and is applied when you create a TCP socket." is a quote from [this answer](https://stackoverflow.com/a/17843292/1300737). The function `setsockopt` is applied to a specific socket (the one it takes as an argument), and different sockets can have different configurations. (It would be strange if it was global, as that would undoubtedly cause problems.) – Wingblade Dec 26 '17 at 10:28
  • 1
    'Enabled on a per-socket basis' does not exclude inheritance, and the answer you cite is about per-socket versus system-wide, not per-socket versus inheritance. In any case what other people have said on SO is irrelevant. The only thing that matters is the primary sources, i.e. the normative documentation. It is my experience that *every* socket option is inherited from the listening socket by the accepted socket, except where deliberately interfered with, as in the SO_RCVTIMEO option with Java sockets. My question 'what happened when you tried it' remains. – user207421 Dec 26 '17 at 11:07
  • @EJP I did not mean to imply that it excludes inheritance, I will edit my question to avoid further confusion. Also, you should post your "every socket option is inherited unless deliberately interfered" as an answer. Thank you for your help! – Wingblade Dec 26 '17 at 11:14
  • Besides: accept() **is** a cloning operation, so *inheritance* is the natural outcome. – wildplasser Dec 26 '17 at 11:25
  • Exactly so, and that's exactly why you should expect TCP_NODELAY to be inherited, if it can be set on the listening socket at all. – user207421 Dec 28 '17 at 08:37

1 Answers1

2

Yes, TCP_NODELAY on a listening socket is inherited by accepted sockets. Tested on Linux 4.18.0.

T Percival
  • 8,526
  • 3
  • 43
  • 43