2012-04-11 71 views
0

我有一塊代碼在TCP端口上「偵聽」,無論發送什麼內容,只是發回一個字符串。問題是客戶端只是測試端口是否處於活動狀態,然後斷開連接。在哪一點,我得到一個錯誤拋出。 無法訪問已釋放的對象 對象名稱:「System.Net.Socket.NetworkSystem」當TCP連接丟失時拋出錯誤

我認爲這個問題是此代碼是在一個線程,當連接關閉while循環引用一個釋放的對象......當客戶端關閉連接時,應該如何防止錯誤發生?

//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異常當底層套接字被關閉並且的ObjectDisposedException當的NetworkStream被關閉或有故障從網絡讀取。 寫法方法拋出同樣的例外。

因此,它足以抓住這兩個異常類型,並在異常處理程序中採取適當的操作。

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; 
     } 
    } 
} 
相關問題