c#/.Net Socket.Shutdown

5

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?

c#
.net
sockets
asked on Stack Overflow Feb 24, 2009 by user70567 • edited Feb 24, 2009 by Mykroft

2 Answers

3

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...

answered on Stack Overflow Feb 24, 2009 by ShuggyCoUk
2

For reusing the socket use:

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

For force closing use:

socket.Shutdown(SocketShutdown.Both);
socket.Close();
answered on Stack Overflow Aug 4, 2009 by (unknown user)

User contributions licensed under CC BY-SA 3.0