java.net.ConnectException/java.net.ConnectException,具体取决于正常运行或调试模式
您好,我正在尝试向具有IP和端口的服务器发送二进制消息: 192.168.2.101:10001 套接字每50条消息重新打开一次 如果我正常运行应用程序,我会在第5行得到一个java.net.ConnectException,即使我可以ping并telnet服务器 如果我调试应用程序,我会在另一行(11)得到一个java.net.SocketException,有时第一条消息似乎没有任何错误java.net.ConnectException/java.net.ConnectException,具体取决于正常运行或调试模式,java,sockets,connection,outputstream,tcp-ip,Java,Sockets,Connection,Outputstream,Tcp Ip,您好,我正在尝试向具有IP和端口的服务器发送二进制消息: 192.168.2.101:10001 套接字每50条消息重新打开一次 如果我正常运行应用程序,我会在第5行得到一个java.net.ConnectException,即使我可以ping并telnet服务器 如果我调试应用程序,我会在另一行(11)得到一个java.net.SocketException,有时第一条消息似乎没有任何错误 private void sendMessage(String message, int relaisI
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);
}
}
}
非常感谢任何帮助或分析工具:)我找到了解决方案。在我的程序中,不同的线程通过给定的IP和端口向外部硬件组件发送消息。 发生此错误的原因是,使用相同的IP地址和端口两次意外启动线程,导致使用相同的套接字时发生计时冲突 更奇怪的是,在更换服务器机器时,这个错误开始永久性地发生,在它只是偶尔出现之前,我们认为这是网络通信中的噪音 希望这对以后的任何人都有帮助:)