I am learning netcode and multithreading in Monodevelop using C # with GTK #. I have never done this before, and now I need to do everything at once.
I used a chat tutorial that has no error handling, and I found an error that occurs in the client every time I disconnect from the server. The code that sits in the thread listening for messages looks like this, surrounded by try / catch statements:
try { while (Connected) { if (!srReceiver.EndOfStream && Connected) { string temp = srReceiver.ReadLine();
After which the function ends and the thread ends.
The code that ends the connection looks like this and works in the main thread:
private void CloseConnection(string Reason) {
And the above try / catch statements will catch this error:
System.IO.IOException: unable to read data from transport connection: blocking operation was interrupted by calling WSACancelBlockingCall. ---> System.Net.Sockets.SocketException: A lock operation was aborted by a call to WSACancelBlockingCall
in System.Net.Sockets.Socket.Receive (byte buffer [], Int32 offset, Int32 size, SocketFlags socketFlags)
in System.Net.Sockets.NetworkStream.Read (byte [] buffer, Int32 offset, Int32 size)
--- End of internal exception stack trace ---
in System.Net.Sockets.NetworkStream.Read (byte [] buffer, Int32 offset, Int32 size)
in System.IO.StreamReader.ReadBuffer ()
in System.IO.StreamReader.get_EndOfStream ()
in ChatClientGTK.MainWindow.ReceiveMessages () in g: \ Android \ Tutes \ ChatClientRemake \ ChatClientGTK \ MainWindow.cs: line 157
Now, as far as I can tell, when sRReciever.Close () occurs in the main thread, srReciever.ReadLine () is still trying to execute in the listen thread, where the problem lies, but even when I comment srReciever.Close (), I still I get an error.
As far as I can tell, there are no side effects caused by just catching a mistake and moving on, but in fact it is not. Does this mistake need to be fixed, and if so, does anyone have any ideas?