【发布时间】:2012-06-13 01:12:53
【问题描述】:
尝试实现连接服务器的超时参数,但我运气不佳。这是我的代码:
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 的超时属性似乎没有任何区别。
【问题讨论】:
-
聚合的
InnerExceptions是什么? -
@FrédéricHamidi:
"No connection could be made because the target machine actively refused it 127.0.0.1:8080" -
超时小于1023秒是连接成功,还是在所有情况下总是被拒绝?
-
@FrédéricHamidi:如果超时时间小于 1023 秒,则不会引发错误。但是,连接也不成功,因为服务器没有运行。
标签: c# android android-asynctask tcpclient waithandle