2012-09-25 28 views
1

我在.NET C#中有多线程套接字应用程序的问题。听取套接字 - 连接套接字在没有调试中断的情况下EndAccept

随机一段时间后,接受连接而不执行回调函数,并且不会开始接受。

监听器和回调函数的代码如下。

public void StartListening() 
    { 
     // Data buffer for incoming data. 
     byte[] bytes = new Byte[1024]; 

     // Establish the local endpoint for the socket. 
     // The DNS name of the computer 
     // running the listener is "host.contoso.com". 
     IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, listenPort);//Listen on all available network interfaces 

     // Create a TCP/IP socket. 
     Socket listener = new Socket(AddressFamily.InterNetwork, 
      SocketType.Stream, ProtocolType.Tcp); 

     // Bind the socket to the local endpoint and listen for incoming connections. 
     bool signal = true; 
     try 
     { 
      listener.Bind(localEndPoint);  
      listener.Listen(listenPort); 

      while (startlisten) 
      { 
       // Set the event to nonsignaled state. 
       allDone.Reset(); 
       if (signal) 
       { 
        // Start an asynchronous socket to listen for connections. 
        string s = string.Format("Server Socket: Waiting for a connection at Port:{0}", listenPort); 
        DisplayMsg(s); 
        listener.BeginAccept(
         new AsyncCallback(AcceptCallback), 
         listener); 
       } 
       // Wait until a connection is made before continuing. 
       signal = allDone.WaitOne(100); 
      } 
      listener.Close(); 
     } 
     catch (Exception e) 
     { 
      if (e is System.Net.Sockets.SocketException) 
      { 
       SocketException socex = (e as SocketException); 

       throw new Exception(socex.Message); 
      } 
      else 
      { 
       throw new Exception(e.Message); 
      } 
     } 
    } 

    public void AcceptCallback(IAsyncResult ar) 
    { 
     // Signal the main thread to continue. 

     try 
     { 
      if (startlisten) 
      { 
       // Get the socket that handles the client request. 
       Socket listener = (Socket)ar.AsyncState; 

       TCPClient tcp = frm.GetClientObj(); 
       tcp.socketForServer = listener.EndAccept(ar); 

       DisplayErrorMsg("Connection Request From: " + tcp.socketForServer.RemoteEndPoint.ToString()); 

       tcp.Connected = true; 
       tcp.ReceiveData(); 

       // Create the state object. 
       //    StateObject state = new StateObject(); 
       //    state.workSocket = handler; 
       //    handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, 
       //     new AsyncCallback(ReadCallback), state); 
      } 
     } 
     catch (Exception e) 
     { 
      if (e is System.Net.Sockets.SocketException) 
      { 
       SocketException socex = (e as SocketException); 

       DisplayErrorMsg(socex.Message); 
      } 
      else 
      { 
       DisplayErrorMsg(e.Message); 
      } 
     } 
     allDone.Set(); 

    } 
+0

为什么不捕获SocketException而不是使用'is''''as'。或者只是'抛出异常(e.Message)'来隐藏你的异常,因为如果块最终做同样的事情...... –

+0

是你的tcp.RecieveData()阻塞或异步? – Anri

+0

tcp.ReceiveData是不阻塞的异步。 BeginAccept是否设想接受一个连接或多个连接?我猜的问题是BeginAccept不断接受连接,但在一些随机连接之后AcceptCallback没有被调用。 –

回答

0

这种情况发生,如果你关闭EndAccept

之前监听套接字

“取消挂起的调用BeginAccept方法,关闭套接字。当被调用Close方法,同时异步操作正在进行中,提供给BeginAccept方法的回调函数将被调用,随后对EndAccept方法的调用将抛出一个ObjectDisposedException异常,以指示该操作已被取消。

http://msdn.microsoft.com/en-us/library/5bb431f9.aspx

0

我找到了答案,这是关系到allDone事件,如若如果循环内部信号。

if (signal) 
{ 
    allDone.Reset(); 
    // Start an asynchronous socket to listen for connections. 
    string s = string.Format("Server Socket: Waiting for a connection at Port:{0}", listenPort); 
    DisplayMsg(s); 
    listener.BeginAccept(new AsyncCallback(AcceptCallback), listener); 
} 

// Wait until a connection is made before continuing. 
signal = allDone.WaitOne(100); 
相关问题