客户端套接字上的setSoTimeout不会影响套接字 [英] setSoTimeout on a client socket doesn't affect the socket

查看:141
本文介绍了客户端套接字上的setSoTimeout不会影响套接字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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?

推荐答案

过去我在Windows中处理SO_TIMEOUT时遇到过几个问题。我相信设置这个假设来设置可能依赖于操作系统且与注册表设置冲突的底层套接字实现等。

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.

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

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().

这篇关于客户端套接字上的setSoTimeout不会影响套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆