开发者社区> 问答> 正文

java.net.sockettimeoutexception: read timed out的问题

"

client有如下伪代码:

<code class=""java"">try{ Socket ss = new Socket(); ss.connect(new InetSocketAddress(127.0.0.1, TCPServer.LISTEN_PORT), 4000); ss.setSoTimeout(5000);

//dosomthing
InputStream is = ss.getInputStrean();
log.info("read start time:" + System.currentTimeMillis())
int r = is.read(....);
log.info("read start time:" + System.currentTimeMillis())

} catch(Exception e){ log.info("throw exception time:" + System.currentTimeMillis()); }

上面这样的逻辑,ss.setSoTimeout(5000);把读超时设置为5000ms,可经过测试read的时间也不过才619ms 就抛出了如题的异常?

"

展开
收起
因为相信,所以看见。 2020-05-27 10:03:04 2613 0
1 条回答
写回答
取消 提交回答
  • 阿里,我所有的向往

    这是服务端是同步通知,时间过长,把服务端修改成异步的即可

    ######

    这个timeout,并不是触发的你setSoTimeOut(5000)的异常。我的理解是soTimeOut是整个Socket建立连接的最长超时时间,如果你之前connect 没有TimeOut,Read 没有TimeOut,Write 也没有TimeOut,但是你的socket仍然没有得到关闭,当时间超过SoTimeOut的时候,就会抛出异常。

    2020-05-27 17:34:54
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载