I am trying to write a client/server type of application using TCP. A client has the client program installed which listens to port (say 11000) at all times. My server sends a quick small string of text to one of the clients (I could be doing a for loop and could be hundreds of clients). Now all of this is working, except here's the thing, if I send commands too quickly, the application crashes, saying 'cannot use disposed object Socket' in this method (I am using this code: http://msdn.microsoft.com/en-us/library/bew39x2a.aspx):
private static void ConnectCallback(IAsyncResult ar)
{
// Retrieve the socket from the state object.
Socket client = (Socket)ar.AsyncState;
// Complete the connection.
client.EndConnect(ar);
// Signal that the connection has been made.
connectDone.Set();
}
Can someone help. Is the port not opened by then? I do call this at the end of every command:
client.Shutdown(SocketShutdown.Both);
client.Disconnect(false);
client.Close();