【发布时间】:2016-04-03 16:58:45
【问题描述】:
您好,我正在尝试使用 IP 和端口向服务器发送二进制消息:
192.168.2.101:10001
套接字每 50 条消息重新打开一次。
如果我正常运行应用程序,我会在第 5 行收到 java.net.ConnectException,即使我可以 ping 和远程登录服务器。
如果我调试应用程序,我会在不同的行 (11) 收到 java.net.SocketException,有时第一条消息似乎也没有任何错误。
private void sendMessage(String message, int relaisId, long timestamp) {
try {
if (connCount > 50) {
s = new Socket(ip, port); //RUN NORMALLY: java.net.ConnectException: Connection refused: connect
connCount=0;
}
outputStream = s.getOutputStream();
outputStream.write(message.getBytes());
outputStream.write(new byte[]{0});//DEBUG: java.net.SocketException: Connection reset by peer: socket write error
outputStream.flush();
connCount++;
} catch (UnknownHostException ex) {
logger.error("Host not found: " + ip + ":" + port, ex);
connCount=51;
retryMessage(message, relaisId, timestamp);// basically sleep 3s then call sendMessage
} catch (IOException ex) {
logger.error("Error at Relais No. " + relaisId + ": " + ip + ":" + port, ex);
connCount=51;
retryMessage(message, relaisId, timestamp); // basically sleep 3s then call sendMessage
} finally {
try {
if (connCount > 50 && s != null) {
s.close();
}
} catch (IOException ex) {
logger.error("IOException", ex);
}
}
}
非常感谢任何帮助或分析工具:)
【问题讨论】:
标签: java sockets connection outputstream tcp-ip