TCP 连接尝试超时

发布于 2024-12-13 22:29:59 字数 413 浏览 0 评论 0原文

在 C# 中,我尝试测试与这样的端口的连接:

Socket socket = new Socket(AddressFamily.InterNetwork,
                    SocketType.Stream,
                    ProtocolType.Tcp);

socket.Connect(telco.Address, telco.Port);

当端口可用时,它工作得很好。然而,当端口不可用时(例如 TCP/81 上的 google.com),需要很长时间(约 60 秒)才会超时。这是一个状态页面,所以我想相对快速地失败。

我尝试将 socket.SendTimeout 和 socket.RecieveTimeout 设置为 5000ms(5 秒),但这似乎没有效果。

In C# I'm trying to test connectivity to a port like this:

Socket socket = new Socket(AddressFamily.InterNetwork,
                    SocketType.Stream,
                    ProtocolType.Tcp);

socket.Connect(telco.Address, telco.Port);

It works great when the port is available. However when the port is not available (e.g. google.com on TCP/81) it takes a long time (~60 seconds) for it to timeout. This is for a status page so I want to fail relatively quickly.

I tried to set socket.SendTimeout and socket.RecieveTimeout to 5000ms (5 seconds) but that appears to have no effect.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

菊凝晚露 2024-12-20 22:29:59

为了保持简单性,我最终使用了与此类似的代码:

Socket socket = new Socket(AddressFamily.InterNetwork,
                    SocketType.Stream,
                    ProtocolType.Tcp);

IAsyncResult result = socket.BeginConnect(telco.Address, telco.Port, null, null);

bool connectionSuccess = result.AsyncWaitHandle.WaitOne(5000, true);

I ended up going with code similar to this to maintain simplicity:

Socket socket = new Socket(AddressFamily.InterNetwork,
                    SocketType.Stream,
                    ProtocolType.Tcp);

IAsyncResult result = socket.BeginConnect(telco.Address, telco.Port, null, null);

bool connectionSuccess = result.AsyncWaitHandle.WaitOne(5000, true);
貪欢 2024-12-20 22:29:59

认为您正在寻找 socket.ReceiveTimeout();

或使用 beginConnect() 并使用同步工具(例如 AutoResetEvent)来捕获超时。

请参阅这篇文章:如何配置套接字连接超时

think you're looking for socket.ReceiveTimeout();

or use beginConnect() and use a sync tool such AutoResetEvent to catch the time out.

see this post: How to configure socket connect timeout

眼眸里的那抹悲凉 2024-12-20 22:29:59

要精细控制超时,请执行以下操作:

  • 使用上面的代码启动一个线程
  • 在线程被触发并尝试套接字连接后, ,执行您需要的操作(即您有一点时间的事情,至少 Application.DoEvents( )
  • Thread.Sleep() 以获得所需的超时间隔,或者使用其他方法来检查连接
  • 是否从主线程打开。成功连接后您将设置的状态变量。
  • 如果是,请使用该连接。如果没有,请在等待的套接字上使用 Socket.Close() 来中断连接尝试。

是的,您会在尝试连接的线程中收到异常。 并假设一切正常。

您可以放心地阅读它 rel="nofollow">http://msdn.microsoft.com/en-us/library/wahsac9k(v=VS.80).aspx Socket.Close()

To fine control your timeout, do this:

  • fire up one thread with the code above
  • after thread is fired and socket connection is attempted, do what you need (i.e. something you have a little time for, at least Application.DoEvents()
  • Thread.Sleep() for desired timeout interval. Or use some other method to make the time pass
  • check if the connection is open from your main thread. You can do this by observing some state variable that you'll set after successful connection is made.
  • if so, use the connection. If not, use Socket.Close() on your waiting socket to break the connection attempt.

Yes, you'll get the exception in the thread that tries to connect, but you can safely gobble it and assume that everything is OK.

http://msdn.microsoft.com/en-us/library/wahsac9k(v=VS.80).aspx Socket.Close()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文