tags:

views:

2046

answers:

2

I recognize this type of question has a long history, but the way I am using this must be the correct '.net way' and yet it does not seem to work.

I have a trivial synchronous IP server daemon that does a simple AcceptSocket, do some stuff, socket.send, socket.shutdown, socket.close. My client is another trivial C# app that does URLDownloadToFile.

What happens is that part of the time URLDownloadToFilefails fails with (0x800C0008) .. thinks its download resource failed.

My server side end sequence is:

socket.Shutdown(Both);
socket.Close();

If I change this to

socket.Disconnect();
socket.Close();

(I open the above with sockopt Linger true, timeout 5 secs)

this works great.

Am I missing something on the Shutdown method.. it sounds like the 'magic bullet' MS wants you to use for gracefully doing an exit that will ultimately send any remaining send data.

Grossly, (and this cannot be right) it appears like the close.. kills any async processing that might be in progress from shutdown().

Any ideas?

+1  A: 

Based on Socket.Disconnect

If you need to call Disconnect without first calling Shutdown, you can set the DontLinger Socket option to false and specify a nonzero time-out interval to ensure that data queued for outgoing transmission is sent. Disconnect then blocks until the data is sent or until the specified time-out expires. If you set DontLinger to false and specify a zero time-out interval, Close releases the connection and automatically discards outgoing queued data.

Suggests the Shutdown is at best unnecessary...

ShuggyCoUk
+2  A: 

For reusing the socket use:

socket.Shutdown(SocketShutdown.Both);
socket.Disconnect(true);

For force closing use:

socket.Shutdown(SocketShutdown.Both);
socket.Close();
reusing the socket doesn't work. Try connecting again, it will throw an InvalidOperationException.
Hermann