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

标签 java nio apachebench

我正在使用 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/

相关文章:

java - Spring Boot 无法成功将数据 POST 到数据库 (ORA-00942)

performance - 每次迭代更改Apache Bench使用的POST数据

ruby-on-rails - 大型机器上的 Rails 应用程序仅获得 60 个请求/秒的基准测试结果

java - 每个 SelectionKey(或 SelectionKeyImpl)是否都有一个可供我使用的唯一标识符 (id)?

nginx - 无法让 ab 测试与 gzip 一起使用

java - 使用Keycloak进行客户注册

java - 每个 bean 的 HandlerMethodArgumentResolver 的替代方案

java - array.length 的运行时间是多少?

java - PushbackReader 等效项

java - NIO 中事件注册的顺序重要吗?