我正在使用创建一个小型服务器java.nio
,但是在尝试对其进行压力测试时,我不断收到有关在服务器端重置连接的消息,或者更具体地说:
apr_socket_recv: An established connection was aborted by the software in your host machine
我试图将其缩小到最简单的循环,但仍然没有运气。我可以在一百左右的连接后得到错误,或者可能只是在 1 或 2 之后。
这是服务器循环:
byte[] response = ("HTTP/1.1 200 OK\r\n"
+ "Server: TestServer\r\n"
+ "Content-Type: text/html\r\n"
+ "\r\n"
+ "<html><b>Hello</b></html>").getBytes();
SocketChannel newChannel = null;
while (active) {
try {
//get a new connection and delegate it.
System.out.print("Waiting for connection..");
newChannel = serverSocketChannel.accept();
System.out.println("ok");
newChannel.configureBlocking(true);
newChannel.write(ByteBuffer.wrap(response));
}
catch (IOException e) {
e.printStackTrace();
}
finally {
try {
newChannel.close();
} catch (IOException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
我试过检查写入是否没有写入所有请求的字节,但它似乎确实如此。有趣的是,每次调用System.gc()
都会newChannel.close()
使问题消失(但作为回报,它非常慢)。所以要么我没有释放我应该释放的所有资源,要么应用程序只需要暂停..
我正在失去我所有最好的岁月。哦,顺便说一句..如果我忽略写入频道并在我接受连接后关闭,问题仍然没有消失。