C# TcpClient的Connect超时处理(Timeout)

 

IAsyncResult ar = tcpClient.BeginConnect(ip,port,null,null);
bool success = ar.AsyncWaitHandle.WaitOne(1000);
if (!success)
throw new Exception("超时时间已到,未连接到指定服务器");

 

重点就是使用BeginConnect方法,及IAsyncResult.AsyncWaitHandle.WaitOne方法 

 

具体怎么定义的就不写了。

 



 

posted @ 2016-02-16 10:41  逍遥子k  阅读(4432)  评论(0编辑  收藏  举报