比如timeOut为1秒,BeginConnect是异步连接
var client = new TcpClient();
var result = client.BeginConnect("remotehost", this.Port, null, null);
var success = result.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(1));
if (!success)
{
throw new Exception("Failed to connect.");
}
// we have connected
client.EndConnect(result);
版权声明:本文为nini3816原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。