2012-04-11 21 views
0

TCPポート上で「リッスン」し、何を送信しても文字列を返すだけのコードブロックがあります。問題は、クライアント側がポートがアクティブであるかどうかを確認して切断することです。どの時点で私はエラーがスローされます。私は、問題は、このコードはスレッド上で、接続が閉じたときにwhileループが配置されたオブジェクトを参照することだと思う「System.Net.Socket.NetworkSystem」TCP接続が紛失したときにエラーが発生する

...: が配置されたオブジェクト オブジェクト名にアクセスできませんクライアントが接続を閉じたときにエラーが発生しないようにするにはどうすればよいですか? NetworkStreamが閉じられたときに下層のソケットが閉じられ、説明ObjectDisposedExceptionれるとき

//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)) > 0) && (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; 
      } 
     } 
    } 

答えて

0

は、MSDNによれば、NetworkStreamクラスのRead方法は、にIOExceptionをスローまたはネットワークからの読み取り障害があります。同じ例外がメソッドでスローされます。

したがって、これらの2つの例外タイプを捕捉し、例外ハンドラで適切な処置をとることが十分です。

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)) > 0) && (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; 
     } 
    } 
} 
関連する問題