I am trying to code a very simple (seems like it to me) extension of the MSDN example here: https://msdn.microsoft.com/en-us/library/bew39x2a(v=vs.110).aspx
The client would run in a loop. In the case below, I am reinitializing the client every time.
while (true)
{
Socket client = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
client.BeginConnect(remoteEP,
new AsyncCallback(ConnectCallback), client);
connectDone.WaitOne();
string myCommand = "";
Console.WriteLine("Enter command:");
myCommand = Console.ReadLine();
if (myCommand == "quit") break;
// Send test data to the remote device.
Send(client, myCommand + "<EOF>");
sendDone.WaitOne();
// Receive the response from the remote device.
Receive(client);
receiveDone.WaitOne();
// Write the response to the console.
Console.WriteLine("Response received : {0}", response);
// Release the socket.
client.Shutdown(SocketShutdown.Both);
client.Close();
}
It doesn't work consistently, frequently I get the error "Cannot access a disposed object", but can't figure out what is triggering it. I'm a beginner at socket programming and multithreading.