2012-02-21 55 views
0

我正在開發使用C#套接字編程的TCP客戶端/服務器應用程序。有時,我遇到了一個非常奇怪的問題,因爲服務器(Windows服務)在端口(8089)上運行,但它沒有監聽任何客戶端請求,並且當我使用端口掃描器測試端口時,它告訴我端口不是響應!這是我的服務器代碼:TCP套接字沒有響應

首先,

私人無效MainThread(){字節 []字節=新字節[1024];

 IPEndPoint localEndPoint = new IPEndPoint(0, this.port); 

     Socket listener = new Socket(AddressFamily.InterNetwork, 
      SocketType.Stream, ProtocolType.Tcp); 

     try { 
      listener.Bind(localEndPoint); 
      listener.Listen(100); 

      while (active) { 
       mainDone.Reset(); 

       listener.BeginAccept(new AsyncCallback(AcceptCallback),listener); 

       while (active) 
        if (mainDone.WaitOne(100, true)) 
         break; 
      } 
      listener.Close(); 

     } catch (Exception e) { 
      if (OnError != null) 
       OnError(this, e.ToString()); 
      LogManager.LogError(e, "TCPSimpleServer MainThread"); 
     } 

然後,

private void AcceptCallback(IAsyncResult ar) { 
     Socket handler = null; 
     try 
     { 
      mainDone.Set(); 

      Socket listener = (Socket)ar.AsyncState; 
      handler = listener.EndAccept(ar); 

      if (OnConnect != null) 
       OnConnect(this, handler); 

      StateObject state = new StateObject(); 

      state.workSocket = handler; 
      state.endPoint = (IPEndPoint)handler.RemoteEndPoint; 
      stateObjectDictionary.Add(state, state.workSocket); 
      handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, 
       new AsyncCallback(ReadCallback), state); 
     } 
     catch (ObjectDisposedException) 
     { 
      // Connection closed by client 
      if (OnDisconnect != null) 
       OnDisconnect(this, (IPEndPoint)handler.RemoteEndPoint); 
      return; 
     } 
     catch (Exception ex) 
     { 
      LogManager.LogError(ex, "TCPSimpleServer AcceptCallback"); 
      return; 
     } 

和最後

private void ReadCallback(IAsyncResult ar) { 
     try 
     { 
      String content = String.Empty; 
      StateObject state = (StateObject)ar.AsyncState; 
      Socket handler = state.workSocket; 

      int bytesRead = 0; 
      try 
      { 
       bytesRead = handler.EndReceive(ar); 
      } 
      catch (Exception ex) 
      { 
       // Connection closed by client 
       if (OnDisconnect != null) 
        OnDisconnect(this, state.endPoint); 
       handler.Close(); 
       return; 
      } 

      if (bytesRead > 0) 
      { 
       string data = Encoding.Default.GetString(state.buffer, 0, bytesRead); 

       if (OnDataAvailable != null) 
        OnDataAvailable(this, handler, data); 
       try 
       { 
        handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, 
         new AsyncCallback(ReadCallback), state); 
       } 
       catch (Exception e) 
       { 
        if (OnError != null) 
         OnError(this, e.Message); 
        LogManager.LogError(e, "TCPSimpleServer ReadCallback"); 
        handler.Close(); 
       } 
      } 
      else 
      { 
       // Connection closed by peer 
       if (OnDisconnect != null) 
        OnDisconnect(this, state.endPoint); 
      } 
     } 
     catch (Exception ex) 
     { 
      LogManager.LogError(ex, "TCPSimpleServer ReadCallback"); 
     } 
    } 

我認爲這個問題是在最後一個方法ReadCallback()如果一點改進發生在EndReceive()方法插槽(處理器)從不釋放端口。請幫忙嗎?

回答

0

莫非,一個客戶端可以阻止在服務器:

while (active) 
    if (mainDone.WaitOne(100, true)) 
     break;