c# tcpclient 连接超时的设置

 

比如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);

 

 

你可能感兴趣的:(c# tcpclient 连接超时的设置)