1

I looked at documentation about this but shouldn't a socket linger be declared before the close so the program doesn't immediately abort before it knows what kind of shutdown to expect? Shouldn't linger be used first to explain to the socket close the shutdown should be one way or another? I'm looking at previously used code and the microsoft documentation but it just seemed odd to me.

https://msdn.microsoft.com/en-us/library/windows/desktop/ms738547(v=vs.85).aspx

From this code, couldn't the socket immediately shutdown from the sockClose and the linger would be ignored? Code:

  self->socket = sockAccept(srvSocket)
  sockClose(srvSocket);
  sockDelay(self->socket, 1);
  sockBuffer(self->socket, pileSize + sizeof(messages));
  sockLinger(self->socket);
Bewlar
  • 81
  • 11
  • Any code you want us to consider should be presented in the question itself. In the code you did present, the argument to `sockClose()` is different from the argument to `sockLinger()`, so I'm inclined to think they are different sockets. From the names alone, I might speculate that `srvSocket` is a listening socket, and `self->socket` is a connected one. There's no need for a listening socket to linger. – John Bollinger Aug 10 '17 at 15:50
  • Interesting reading on [SO_LINGER](https://blog.netherlabs.nl/articles/2009/01/18/the-ultimate-so_linger-page-or-why-is-my-tcp-not-reliable) – TonyB Aug 10 '17 at 21:36

1 Answers1

0

Yes, you must set linger options prior to closing a socket. Only during the close process the system kernel will look at the linger options and this changes the way how the socket is actually closed.

Please also refer to this answer for more details about lingering.

Mecki
  • 125,244
  • 33
  • 244
  • 253