尝试实现连接到服务器的超时参数,但我运气不佳。这是我的代码:
client = new TcpClient();
Task task = Task.Factory.FromAsync(client.BeginConnect, client.EndConnect, host, port, null);
bool taskCompleted = connectTask.Wait(timeoutInMS);
if (taskCompleted)
{
// Do something with the establishment of a successful connection
}
else
{
Console.WriteLine("Timeout!");
}
不幸的是,如果 timeoutInMS 大于 1022,则会在此行引发 AggregateException:
bool taskCompleted = connectTask.Wait(timeoutInMS);
调整 TcpClient 的超时属性似乎没有任何区别。