2014-05-03 37 views
0

爲什麼我在tcpClient = this.ss.AcceptTcpClient上掛起程序?爲什麼我在AcceptTcpClient上掛起程序?


public virtual void Run() 
{ 
    if (this.tcpListener == null) 
     return; 

    TcpClient tcpClient = (TcpClient)null; 

    while (!this.m_Stop) 
    { 
     try 
     { 
      tcpClient = this.tcpListener.AcceptTcpClient(); 
      ThreadPool.QueueUserWorkItem(new WaitCallback(this.handler.Handle), (object)tcpClient); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.ToString()); 
     } 
    } 
} 

這裏ssTcpListener

回答

1

我使用Google搜索TcpListener.AcceptTcpClient Method。這是我發現的。

AcceptTcpClient是一種阻塞方法,它返回您可以用來發送和接收數據的TcpClient。如果您想避免阻塞,請使用掛起方法確定 如果連接請求在傳入連接隊列中可用 。

看來,該方法等待,直到有可用的連接請求。所以要麼使用Pending方法調用,要麼在後臺線程(而不是UI線程)上執行此操作。

+0

好的謝謝你的信息。 – user3592352

+0

但我怎麼能用它給任何例子先謝謝 – user3592352

+0

試試這個:http://msdn.microsoft.com/en-us/library/vstudio/system.net.sockets.tcplistener.pending%28v=vs.100 %29 –

相關問題