[C#]設定連線Time out
TcpClient MyTcpClient = new TcpClient();
IAsyncResult MyResult = MyTcpClient.BeginConnect(YourTargetIP, YourTargetPort, null, null);
MyResult.AsyncWaitHandle.WaitOne(3000, true);//只等三秒
if (!MyResult.IsCompleted)
{
MyTcpClient.Close();
//作如果沒連上線的事
}
else if (MyTcpClient.Connected == true)
{
//作連上線的事
}
MyTcpClient.Close();
IAsyncResult MyResult = MyTcpClient.BeginConnect(YourTargetIP, YourTargetPort, null, null);
MyResult.AsyncWaitHandle.WaitOne(3000, true);//只等三秒
if (!MyResult.IsCompleted)
{
MyTcpClient.Close();
//作如果沒連上線的事
}
else if (MyTcpClient.Connected == true)
{
//作連上線的事
}
MyTcpClient.Close();
留言
張貼留言