Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>According to MSDN, <a href="http://msdn.microsoft.com/en-us/library/system.net.sockets.networkstream.read.aspx" rel="nofollow">Read</a> method of <a href="http://msdn.microsoft.com/en-us/library/system.net.sockets.networkstream.aspx" rel="nofollow">NetworkStream</a> class throws <em>IOException</em> when the underlying Socket is closed and <em>ObjectDisposedException</em> when the NetworkStream is closed or there is a failure reading from the network. The same exceptions are thrown by <em>Write</em> method.</p> <p>Therefore it shoud be enough to catch these 2 exception types and take appropriate action in exception handlers.</p> <pre><code> 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; try { 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; } } catch(IOException) { //HERE GOES CODE TO HANDLE CLIENT DISCONNECTION } catch(ObjectDisposedException) { //HERE GOES CODE TO HANDLE CLIENT DISCONNECTION } 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) { 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