tags:

views:

1115

answers:

5

I'm trying to abort a socket connection such that the client at the other end will get a "WSAECONNABORTED (10053) Software caused connection abort." error message when it polls the connection.

Close() and Shutdown() will disconnect gracefully. I don't want a graceful disconnection. I want to do an Abort so that the client senses something really went wrong.

Thanks,

EDIT: I'm coding a Server and I want to abort sockets to connecting clients

A: 

What if you kill the process, and/or reboot the machine, and/or unplug the ethernet cable, without calling Close() and/or Shutdown()?

ChrisW
are you joking? clearly he wants the *program* to send ECONNABORTED when an error occurs. requiring that he physically crash the network link/process completely defeats the purpose.
muusbolla
I meant, kill the *other* process at the *other* end of the connection. He wants *this* program to *receive* ECONNABORTED from its local TCP stack, which (I think) happens if the connection is abended (e.g. if the remote peer disappears).
ChrisW
I agree with Chris. You cannot have the Server sending ECONNABORTED. Only when the client will see missed Acks for the data it sends will it receive ECONNABORTED on the next Send call.
Aditya Sehgal
A: 

try calling EndSend() or EndReceive() depending on the situation immediately followed by a Dispose()

Konstantinos
A: 

I don't think that it is possible to get the behavior you want with the .Net Socket implementation other than having the client send intermittent keep-alives. I've tried all sorts of things to get the behavior you described and ended up implementing keep-alive messages. If the sever calls Close() the client socket will get a connection aborted error the next time it attempts to send data.

SpaceghostAli
+1  A: 

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.

TCP is a bi-directional protocol i.e. it requires closing of connection from both sides. So, when you call Shutdown with "SocketShutdown.Both", it disables both Sending and receiving on the socket. However, when you do "SocketShutdown.Send", the Server closes its side of the TCP connection. In this scenario, when the client tries to send messages to server will receive ECONNABORTED. Take a look at http://www.chilkatsoft.com/p/p_299.asp to understand it better.
Aditya Sehgal
A: 

you can try to code all secondary sockets in another thread, and kill it when you want to crash connections.

Jorge Mota