C# 给TcpClient的Connect方法设置超时时间
2021-06-23 23:07
标签:end connect overflow client span var cti 方法 wait .Net 4.5的简便写法 代码出处: https://stackoverflow.com/questions/17118632/how-to-set-the-timeout-for-a-tcpclient C# 给TcpClient的Connect方法设置超时时间 标签:end connect overflow client span var cti 方法 wait 原文地址:https://www.cnblogs.com/xyz0835/p/10179665.htmlvar 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);
var client = new TcpClient();
if (!client.ConnectAsync("remotehost", remotePort).Wait(1000))
{
// connection failure
}
文章标题:C# 给TcpClient的Connect方法设置超时时间
文章链接:http://soscw.com/index.php/essay/97904.html