How to Abort a Socket in C#

I've managed to simulate this situation:

To do a normal graceful disconnection:

you do:

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

However, to do an Abort, you do:

socket.Shutdown(SocketShutdown.Send);
socket.Close();

I think the difference is that the client will not receive any ACK packets and thinks the computer reset or something.


The underlying details, including when it is actually appropriate to forcibly abort a connection are described in great detail here (https://stackoverflow.com/a/13088864/1280848).

The C# way of doing this is:

socket.LingerState = new LingerOption(true, 0);
socket.Close();

That is, use SO_LINGER with TIME_WAIT = 0

Tags:

C#

.Net

Sockets