std.socket - problems closing socket

simendsjo simendsjo at gmail.com
Mon Oct 3 04:57:56 PDT 2011


On 03.10.2011 11:36, Regan Heath wrote:
> For a "graceful" close you're supposed to ensure there is no data
> pending.  To do that you:
>
> shutdown(SD_SEND);  // send only, not recv
> <enter a loop reading all data remaining on the socket>
> close();
>
> The loop should read until recv returns 0.  If recv returns -1 and the
> socket is blocking it should error/exit.  If recv returns -1 and the
> socket is non-blocking it should check for [WSA]EWOULDBLOCK (and
> select/sleep + loop) or error/exit.
>
> The reason to do this is to flush all the data from the socket buffers
> on the remote and local ends, otherwise a close can cause remote
> buffered data to cause a "connection broken" error on the remote end,
> and/or (I am guessing a little here) may cause the socket to close while
> negotiating a graceful close, and/or remain in a TIME_WAIT state due to
> buffered data or data "in flight".
>
> ... are you setting any close options/timeouts i.e. LINGER?

Thanks.

recv returns -1 for many requests. The errors are only WSAECONNABORTED 
and WSAECONNRESET as described here: 
http://msdn.microsoft.com/en-us/library/ms740668.aspx

I'm doing socket.shutdown(SocketShutdown.SEND) now after sending all my 
data and reading until I receive 0 or -1. (doesn't really matter as 
sending the FastCGI EndRequest makes the server shut it down as it 
doesn't handle multiplexing)

I have tried with linger too, but it doesn't help:
socket.setOption(SocketOptionLevel.SOCKET, SocketOption.LINGER, 
std.socket.linger(1, 30));

Could this be caused by some bad settings on the webserver?

PS: Seems my computer can handle about 16000 TIME_WAIT before it starts 
"hanging".


More information about the Digitalmars-d-learn mailing list