How to reuse socket in .NET?
Posted
by Hermann
on Stack Overflow
See other posts from Stack Overflow
or by Hermann
Published on 2010-04-23T13:41:27Z
Indexed on
2010/04/23
13:43 UTC
Read the original article
Hit count: 514
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?
© Stack Overflow or respective owner