Solaris 9 上的 SocketTimeoutException 不会被抛出
我有一个连接,应该每两分钟收到一次心跳。
但偶尔会发生一些事情,网络会变差。由于某种原因,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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想知道您是否可以执行以下操作:
并且可能在该方法的调用之间设置短暂的超时睡眠。
I'm wondering if you could do something with:
And maybe a short timeout to sleep between calls of that method.
看看 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