Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting Error thrown when TCP connect lost
    text
    copied!<p>I have a block of code that "listens" on a TCP port and just sends an string back no matter what is sent. The issue is the client side is just testing to see if the port is active then disconnecting. At which point I get an error thrown. Cannot access a disposed object Object name: 'System.Net.Socket.NetworkSystem'</p> <p>I think the issue is that this code is on a thread and when the connection closes the while loop references a disposed object... how should I prevent the error from firing when the client closes the connection?</p> <pre><code> //Cretae listener to accept client connections TcpClient tcpClient = (TcpClient)client; NetworkStream clientStream = tcpClient.GetStream(); byte[] rcvBuffer = new byte[BUFSIZE]; // Receive buffer int bytesRcvd; // Received byte count while (ServiceRunning) // Run forever, accepting and servicing connections { try { // Receive until client closes connection, indicated by 0 return value int totalBytesEchoed = 0; //I THINK THIS IS WHERE THE PROBLEM IS .. THE CLIENTSTREAM.READ??? while (((bytesRcvd = clientStream.Read(rcvBuffer, 0, rcvBuffer.Length)) &gt; 0) &amp;&amp; (ServiceRunning)) { clientStream.Write(responseBytes, 0, responseBytes.Length); WriteEventToWindowsLog("GSSResponderService", "Received "+System.Text.Encoding.UTF8.GetString(rcvBuffer), System.Diagnostics.EventLogEntryType.Information); totalBytesEchoed += bytesRcvd; } WriteEventToWindowsLog("GSSResponderService", "Responded to " + totalBytesEchoed.ToString() + " bytes.", System.Diagnostics.EventLogEntryType.Information); // Close the stream and socket. We are done with this client! clientStream.Close(); tcpClient.Close(); } catch (Exception e) { //THIS IS GETTING TRIGGERED WHEN A CONNECTION IS LOST WriteEventToWindowsLog("GSSResponderService", "Error:" + e.Message, System.Diagnostics.EventLogEntryType.Error); clientStream.Close(); tcpClient.Close(); break; } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload