当我通过Java语言编写TCP通信时,发生socketTimeout异常,但是打印的异常没有消息:java.net.SocketTimeoutException 我用这个:log.error(e); 另一个现象是连接时间和读取时间都中断了,并且变得很长
我尝试关闭后端连接并读取后端超时,该超时会显示以下异常:java.net.SocketTimeoutException:读取超时/ java.net.ConnectException:连接被拒绝:connect ...他们将有一些消息
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);
}
}
}
我想知道是什么原因造成的
答案 0 :(得分:0)
此问题已解决。问题是服务器端内存溢出,可能感觉Java虚拟机在内存已满时无法将网络数据移交给高端代码,从而导致由高端代码设置的超时失败。以及其他奇怪的地方。