tags:

views:

575

answers:

2

I am trying to reconnect to a socket that I have disconnected from but it won't allow it for some reason even though I called the Disconnect method with the argument "reuseSocket" set to true.

_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_socket.Connect(ipAddress, port);
//...receive data
_socket.Disconnect(true); //reuseSocket = true
//...wait
_socket.Connect(ipAddress, port); //throws an InvalidOperationException:

Once the socket has been disconnected, you can only reconnect again asynchronously, and only to a different EndPoint. BeginConnect must be called on a thread that won't exit until the operation has been completed.

What am I doing wrong?

+2  A: 

After reading the MSDN documentation for Socket.Disconnect I noticed something that might be causing your issue.

If you need to call Disconnect without first calling Shutdown, you can set the DontLingerSocket 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.

Try setting the DontLingerSocket option and specify a 0 timeout or use Shutdown before you call disconnect.

James
Funny thing is there is no such property or method called 'DontLingerSocket'. The closest thing to it is a property called 'LingerState'. You can instantiate a 'LingerOption' though and set that to it, but it doesn't make any difference if you try to reconnect after calling 'Disconnect'. And you can't reuse a socket if you call 'Close' because that will dispose of the socket.
Hermann
A: 

From MSDN:

// Release the socket.
client.Shutdown(SocketShutdown.Both);

client.Disconnect(true);
if (client.Connected) 
    Console.WriteLine("We're still connnected");
else 
    Console.WriteLine("We're disconnected");

If you are using a connection-oriented protocol, you can use this method to close the socket. This method ends the connection and sets the Connected property to false. However, if reuseSocket is true, you can reuse the socket.

To ensure that all data is sent and received before the socket is closed, you should call Shutdown before calling the Disconnect method.

Socket.Shutdown method waits until all the data in the buffer has been sent or received. However, if we only set linger options, the Socket will shutdown after certain timeout interval.

KMan
Tried that, doesn't work.
Hermann
@Hermann: I think you will have to wait for some seconds(timeout) for the socket to be "reuseable", you can try putting your socket into a thread - for retries... just thinking; read more about the `graceful shudown`, http://msdn.microsoft.com/en-us/library/ms738547(VS.85).aspx
KMan