【问题标题】:SocketTimeoutException on Solaris 9 not being thrown未抛出 Solaris 9 上的 SocketTimeoutException
【发布时间】:2010-12-04 08:41:17
【问题描述】:

我的连接应该每两分钟接收一次心跳。

但是偶尔会发生一些事情并且网络会变坏。出于某种原因,Solaris 认为连接仍然存在,本地没有引发 IOException,但是远程引发了一个。

我已在套接字上设置了超时,当从 Windows XP 测试环境中调试时,我们可以确认在两分十秒后,套接字按预期超时。

为了引发异常,是否有任何我没有背诵的魔法圣歌?

public void run() {
    this.connect();
    while (true) {
        try {
            InputStreamReader reader = new InputStreamReader(this.socket.getInputStream());
            OutputStreamWriter writer = new OutputStreamWriter(this.socket.getOutputStream(), "ISO-8859-1");
            for (int errorCount = 0; errorCount < 15;) {
                if (!this.readResponse(reader, writer)) {
                    errorCount++;
                }
            }
            this.logger.error("read incomplete message for 15 times, reset the connection");
        } catch (Exception e) {
            this.logger.error("read response error", e);
        } finally {
            this.reconnect();
        }
    }
}

在哪里

private boolean readResponse(InputStreamReader reader, OutputStreamWriter writer) throws IOException {
    int length = 0;
    for (int i = 0; i < 2; i++) {
        int ch = reader.read();
        if (ch < 0) {
            this.logger.error("read response buffer length error");
            return false;
        }
        length = length * 256 + ch;
    }
return true;
}

是 readResponse 方法的开始。

该方法在 int ch = reader.read(); 上阻塞在 readResponse 中,正如我所料,但永远不会引发 SocketTimeoutException。

有什么想法吗?

该代码适用于所有情况,但超时除外。

感谢您的帮助。

【问题讨论】:

  • 您遗漏了代码的相关部分:您在何处、何时以及如何设置套接字超时?

标签: java debugging sockets solaris


【解决方案1】:

我想知道你是否可以做点什么:

if (reader.ready()) { int ch = reader.read(); } 

在调用该方法之间可能会有短暂的睡眠超时。

【讨论】:

    【解决方案2】:

    看看http://bugs.sun.com/view_bug.do?bug_id=6596323,看看它是否能解决您的问题

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-14
      • 2016-08-09
      • 2010-09-14
      • 2012-05-16
      • 1970-01-01
      • 2021-11-19
      • 2022-11-09
      相关资源
      最近更新 更多