当我通过Java语言编写TCP通信时,发生了socketTimeout异常,但打印的异常没有消息:java.net.SocketTimeoutException我使用这个:日志错误(e(;另一个现象是连接时间和读取时间都中断了,变得很长
我尝试关闭后端连接并读取后端超时,这会打印以下异常:java.net.SocketTimeoutException:读取超时/java.net.ConnectException:连接被拒绝:连接...他们会有一些信息
Socket socket = null;
String ip = null;
int port = 0;
OutputStream out = null;
InputStream in = null;
ByteArrayOutputStream baos = null;
String response = null;
int connTimeout = 3000;
int readTimeout = 5000;
try {
socket = new Socket();
ip = locationConfig.getHost();
port = locationConfig.getPort();
socket.setSendBufferSize(64 * 1024);
socket.setReceiveBufferSize(64 * 1024);
socket.setTcpNoDelay(true);
socket.setSoTimeout(readTimeout);
socket.setKeepAlive(true);
socket.setSoLinger(true, 0);
socket.setReuseAddress(false);
socket.connect(new InetSocketAddress(ip, port), connTimeout);
out = socket.getOutputStream();
out.write("DETECT1".getBytes());
out.flush();
socket.shutdownOutput();
in = socket.getInputStream();
int length = 0;
byte[] buffer = new byte[Constants.READ_BUFFER_MAX_SIZE];
baos = new ByteArrayOutputStream();
while ((length = in.read(buffer)) != -1) {
baos.write(buffer, 0, length);
baos.flush();
}
response = new String(baos.toByteArray());
} catch (Exception e) {
log.error(e)
} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
log.error(e);
}
}
}
我想知道是什么原因造成的
这个问题已经解决了。问题是服务器端内存溢出。当内存已满时,Java 虚拟机可能无法将网络数据移交给上层代码,从而导致上层代码设置的超时失败。和其他奇怪的东西。