如何设置TcpClient的超时时间?

如何设置TcpClient的超时时间?,第1张

如何设置TcpClient的超时时间?

您将需要使用async

BeginConnect
方法
TcpClient
而不是尝试同步连接,这是构造函数所做的。像这样:

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 connectedclient.EndConnect(result);


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5462554.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-12
下一篇 2022-12-12

发表评论

登录后才能评论

评论列表(0条)

保存