2012-02-21 51 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;