gpt4 book ai didi

java - apr_socket_recv : An established connection was aborted by the software in your host machine

转载 作者:行者123 更新时间:2023-11-29 09:22:56 24 4
gpt4 key购买 nike

我正在使用 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);
}
}

}

我已经尝试检查写入是否没有写入所有请求的字节,但它似乎确实如此。有趣的是,在每次 newChannel.close() 之后调用 System.gc() 会使问题消失(但作为返回,速度非常慢)。所以要么我没有释放我应该释放的所有资源,要么应用程序只需要暂停一下..

我正在为此失去所有最好的年华。哦,顺便说一句......如果我忽略写入 channel 并在我接受连接后关闭,问题仍然不会消失。

最佳答案

好吧,我发现了,所以我不妨分享一下。

我的应用需要暂停。它只是速度太快了,在客户端写入所有请求数据之前就关闭了连接。解决方法是继续阅读,直到收到整个 HTTP 请求。 D'oh.. 吸取教训。

关于java - apr_socket_recv : An established connection was aborted by the software in your host machine,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5009895/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com