客户端套接字上的 setSoTimeout 不会影响套接字

发布于 2024-08-02 08:27:14 字数 175 浏览 3 评论 0原文

我有一个包含三个线程的 Java 应用程序,每个线程打开一个套接字并连接到不同端口上的服务器。 建立与服务器的连接后,我在每个套接字上设置了 so_timeout 。 之后线程会阻塞等待 read()。 只有一个线程在 20 秒后超时(这是我设置的超时)。 另外两个忽略超时。 TCP 层是否有可能一次只处理一个超时? 还有其他解释吗?

I have a Java application with three threads that open, each, a socket and connect to a server on different ports.
I set so_timeout on each of these sockets after the connection to the server is established.
After that the threads block waiting on read().
Only one of the threads times out after 20 seconds (which is the timeout I set).
The other two ignore the timeout.
Is it possible that the TCP layer handles only one timeout at a time?
Is there any other explanation?

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

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

发布评论

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

评论(2

晚雾 2024-08-09 08:27:14

文档说:

必须在进入阻止操作之前启用该选项才能生效。

也许您应该在建立与服务器的连接之前设置它,至少在调用套接字上的 read() 之前。
但没有代码很难说...

The documentation says:

The option must be enabled prior to entering the blocking operation to have effect.

maybe you should set it before the connection to the server is established, at least before calling read() on the socket.
But hard to say without the code...

扛刀软妹 2024-08-09 08:27:14

过去在 Windows 中处理 SO_TIMEOUT 时遇到了几个问题。我相信设置它是“应该”设置底层套接字实现,该实现可能依赖于操作系统并与注册表设置等冲突。

我的建议是不要使用 SO_TIMEOUT 来强制超时抛出异常。使用非阻塞 I/O 或在 read() 之前检查是否有可用字节()。

I've had several problems in the past dealing with SO_TIMEOUT in windows. I believe setting this is "supposed" to set the underlying socket implementation that could be OS dependent and conflicting with registry settings and such.

My advice is to not use SO_TIMEOUT to force a thrown exception on a timeout. Use either non-blocking I/O or check that you have bytes available() before you read().

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