简体   繁体   English

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

[英]setSoTimeout on a client socket doesn't affect the socket

I have a Java application with three threads that open, each, a socket and connect to a server on different ports. 我有一个Java应用程序,它带有三个打开的​​线程,每个线程一个套接字,并连接到不同端口上的服务器。 I set so_timeout on each of these sockets after the connection to the server is established. 建立与服务器的连接后,我在每个套接字上都设置了so_timeout。 After that the threads block waiting on read(). 之后,线程阻塞,等待read()。 Only one of the threads times out after 20 seconds (which is the timeout I set). 20秒(我设置的超时)后,只有一个线程超时。 The other two ignore the timeout. 其他两个忽略超时。 Is it possible that the TCP layer handles only one timeout at a time? TCP层一次只能处理一个超时吗? Is there any other explanation? 还有其他解释吗?

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. 也许您应该在建立与服务器的连接之前 ,至少在调用套接字上的read()之前进行设置。
But hard to say without the code... 但是没有代码很难说...

I've had several problems in the past dealing with SO_TIMEOUT in windows. 过去在Windows中处理SO_TIMEOUT时遇到了几个问题。 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. 我的建议是不要使用SO_TIMEOUT强制在超时时引发抛出的异常。 Use either non-blocking I/O or check that you have bytes available() before you read(). 使用非阻塞I / O或在读取()之前检查字节是否可用()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM