Solaris 9 上的 SocketTimeoutException 不会被抛出

发布于 2024-08-06 19:11:10 字数 1560 浏览 5 评论 0原文

我有一个连接,应该每两分钟收到一次心跳。

但偶尔会发生一些事情,网络会变差。由于某种原因,Solaris 认为连接仍然存在,本地不会引发 IOException,但远程会引发 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 永远不会被引发。

有什么想法吗?

该代码在除超时之外的所有情况下都有效。

感谢您的任何帮助。

I have a connection that should recieve a heartbeat every two minutes.

However occasionally something happens and the network goes bad. For some reason Solaris believes that the connection is still alive, No IOException is raised locally, however one is raised remotely.

I have set a timeout on the socket, and when debugged from a Windows XP test environment we can confirm that after two minutes and ten seconds the socket times out as expected.

Is there any magical chant that I am not reciting that I need to in order for the exception to be raised?

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();
        }
    }
}

Where

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;
}

is the beginning of the readResponse method.

The method is blocking on int ch = reader.read(); in readResponse, as I would expect but the SocketTimeoutException is never being raised.

Any Ideas?

The code works in all cases except timeouts.

Thanks for any help.

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

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

发布评论

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

评论(2

若言繁花未落 2024-08-13 19:11:10

我想知道您是否可以执行以下操作:

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

并且可能在该方法的调用之间设置短暂的超时睡眠。

I'm wondering if you could do something with:

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

And maybe a short timeout to sleep between calls of that method.

瑾兮 2024-08-13 19:11:10

看看 https://bugs.java.com/bugdatabase/view_bug?bug_id= 6596323 看看它是否能解决您的问题

Take a look at https://bugs.java.com/bugdatabase/view_bug?bug_id=6596323 and see if it addresses your problem

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