【发布时间】: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