why chrome finish a keep-alive connection after there is no data exchange after a while

24 views Asked by At

I'm using java's nio to test the keep-alive connection, just wondering if I don't close the connection at the server side, what will happen, I thought the connection will keep util server side close the connection,but i was wrong, after a while for no data exchange, next chrome request will finish the connection and start a new connection, why?

below figure was captured on wireshark wireshark capture

below figure was captured on wireshark (not too long between two request) wireshark capture

here's my code

public static void nioStart() throws IOException {
        ServerSocketChannel socketChannel = ServerSocketChannel.open();
        socketChannel.bind(new InetSocketAddress(8000));
        socketChannel.configureBlocking(Boolean.FALSE);

        Selector selector = Selector.open();
        socketChannel.register(selector, SelectionKey.OP_ACCEPT);

        while (true) {
            selector.select();
            Set<SelectionKey> selectedKeys = selector.selectedKeys();
            for (SelectionKey key : selectedKeys) {
                if (key.isAcceptable()) {
                    ServerSocketChannel serverChannel = (ServerSocketChannel) key.channel();
                    SocketChannel clientChannel = serverChannel.accept();
                    clientChannel.configureBlocking(Boolean.FALSE);
                    clientChannel.register(selector, SelectionKey.OP_READ);
                } else if (key.isReadable()) {
                    SocketChannel clientChannel = (SocketChannel) key.channel();
                    System.out.println("hashCode = " + clientChannel.hashCode() + ", remoteAddress = " + clientChannel.getRemoteAddress());
                    ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
                    int read;
                    while ((read = clientChannel.read(byteBuffer)) > 0) {
                        byteBuffer.flip();
                        byte[] bytes = new byte[byteBuffer.remaining()];
                        byteBuffer.get(bytes);
                        byteBuffer.clear();
                    }
                    // When SocketChannel.read() returns -1, it typically indicates that the remote end of the channel has been closed gracefully. This is the usual behavior when reading from a SocketChannel in non-blocking mode
                    if (read == -1) {
                        System.out.println("close client connection");
                        clientChannel.close();
                        continue;
                    }
                    String response = "hello, client!";
                    byteBuffer.put(("HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nConnection: keep-alive\r\nContent-Length: " + response.getBytes().length + "\r\n\r\n" + response).getBytes());
                    byteBuffer.flip();
                    clientChannel.write(byteBuffer);
                }
            }
            selectedKeys.clear();
        }

    }

if anyone know the reason why, please answer, thanks a lot

I thought server side will make the close firt

0

There are 0 answers