RE: Does Socket.Shutdown or Socket.Close Block and when?

From: Chris Tanger (chrisNnOoSsPpAaMmtanger_at_technocisive.com)
Date: 10/15/04


Date: Fri, 15 Oct 2004 08:13:03 -0700


   I was hoping for a more definitive answer, but no problem, I was just
wondering if anyone knew for certain. Also, it is a bummer that Shutdown and
Close cannot be called asynchronously. Here is why I wish to call them
asynchronously without using an asynchronous delegate: I have a server that
may have 1000 TCP connections coming into it. The server will expect to
receive a heartbeat message every 5 seconds on each socket (to make sure the
connection is still up to each client). If the server does not receive a
heartbeat within say 10 seconds then the server will proceed to shutdown the
socket. I wish to give the socket about 10 additional seconds to complete
sending any data and to send a disconnect notification message if possible
with the reason for the disconnect(heartbeat timeout). Let us assume that
the network path that carries all of these client-server connections goes
down (a black hole down, so no nack packets are sent like when a firewall is
blocking). The TCP connections will likely not throw an exception for
20-120+ seconds. The server will notice that it has not received a heartbeat
on each connection within the specified amount of time (10 seconds since the
last heartbeat). The server will send a disconnect imminent message with the
reason (heartbeat timeout) to each connection and then call shutdown on each
connection. I want the server to attempt to send any remaining data in the
socket send buffers for 10 seconds after shutdown is called. If it takes
longer than 10 seconds I want the socket to perform a hard close and throw
away any data in the send buffer. If all connections go down at the same
time calling shutdown on a separate thread for each connection would require
1000 threads. As you can imagine this is not a scalable solution. The
threadpool cannot handle it, and I cannot reasonable start 1000 threads
simultaneously for the task either.

One solution that comes to mind is creating my own shutdown function that
simply waits for 10 seconds after it is called using a System.Threading.Timer
and then performs a hard close on the socket. The unfortunate thing about
that particular solution is that the socket will not be closed immediately if
the send buffer is empty or becomes empty before the elapsed timeout.

So this is why I need to determine how these things work.

I am going to write a test program that accepts incoming socket connections
with at 8K receive buffer. The test program will never perform a read on the
socket. There will be a corresponding test program that makes a connection
to the listening program. It will have a 8K send buffer. The sending
program will send 12k of data after connecting. This will cause the listing
program's receive buffer to be completely filled to its 8K limit at which
point the sending programs socket will get blocked leaving its 8K send buffer
half full with 4K of data. Then I will call shutdown on the send programs
socket and see what happens. I will also play around with the various linger
options to determine how the Socket class behaves.

I was posting to this group in the hopes that someone already knew the
answer, but this program should enlighten me.

One other question comes to mind, what if I set the Socket.Blocking = false?
 Will this result in Shutdown or close no longer blocking?

Thanks,

-Chris



Relevant Pages

  • Re: Context of error messages with respect to BSD sockets
    ... under what circumstances will send or recv block? ... A 'connection mode' socket usually has an in-kernel send and ... receive buffer associated with it. ... this signifies that the connection has closed ...
    (comp.unix.programmer)
  • Re: closing ASyncSocket
    ... I reread the help for the ShutDown() call. ... not close the socket, and resources attached to the socket will not be freed ... What if I want to Closethe connection to return resources. ... receiving side after I received the last data. ...
    (microsoft.public.vc.mfc)
  • RE: Does Socket.Shutdown or Socket.Close Block and when?
    ... keeps trying to send the data in the send buffers of the socket until TCP ... in the send buffer has been sent or the timeout has elapsed. ... > connection is still up to each client). ... > heartbeat within say 10 seconds then the server will proceed to shutdown the ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Socket BeginSend and disconnections
    ... What determines the length of time between this remote user yanking his ... It does depend somewhat on how the connection is broken and where. ... In theory, you should be able to queue as many sends as you need, especially if you've set the socket buffer itself to 0 ). ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Code run from IDLE but not via double-clicking on its *.py
    ... I'd definitely wait for the client connection to come in, ... If the remote side shuts down writing, the socket ... you should send the shutdown ...
    (comp.lang.python)

Loading