Java 如何使异步tcp服务器继续侦听通道而不是关闭通道

Java 如何使异步tcp服务器继续侦听通道而不是关闭通道,java,networking,asynchronous,tcp,Java,Networking,Asynchronous,Tcp,我正在尝试制作一个能够处理1000多个客户端连接的服务器。这是一个半学术,半业余的项目,所以我有点想做我自己的解决方案,但我面临一个问题:当我开始监听连接,有人连接时,TCP连接在5秒钟后被java关闭。我知道这是我5秒钟睡眠的结果,但如果我把它去掉,它会立即恢复 以下是我的服务器代码: 最终int服务器_端口=9000; 最终字符串服务器IP=10.0.0.201; AsynchronousChannelGroup组=null; 试一试{ group=AsynchronousChannelGr

我正在尝试制作一个能够处理1000多个客户端连接的服务器。这是一个半学术,半业余的项目,所以我有点想做我自己的解决方案,但我面临一个问题:当我开始监听连接,有人连接时,TCP连接在5秒钟后被java关闭。我知道这是我5秒钟睡眠的结果,但如果我把它去掉,它会立即恢复

以下是我的服务器代码:

最终int服务器_端口=9000; 最终字符串服务器IP=10.0.0.201; AsynchronousChannelGroup组=null; 试一试{ group=AsynchronousChannelGroup.WithThreadPool线程池; }捕获IOE1异常{ //TODO自动生成的捕捉块 e1.printStackTrace; } //创建绑定到默认组的异步服务器套接字通道。 tryAsynchronousServerSocketChannel asynchronousServerSocketChannel=asynchronousServerSocketChannel .opengroup{ 如果异步服务器socketchannel.isOpen{ //绑定到本地地址 asynchronousServerSocketChannel.bindnew InetSocketAddressSERVER_IP、SERVER_端口、, 服务器\插座\通道\积压工作; //显示等待消息 System.out.println正在等待ip上的连接:端口+服务器\ ip+:+服务器\端口; 虽然真实{//不好? Future asynchronousSocketChannelFuture=asynchronousServerSocketChannel 接受 tryAsynchronousSocketChannel asynchronousSocketChannel=asynchronousSocketChannelFuture.get{ 最终SocketAddress remoteAddress=asynchronousSocketChannel.getRemoteAddress; System.out.PrintLn来自:+remoteAddress的传入连接; final ByteBuffer incomingBuffer=ByteBuffer.allocateDirect1024; //接收数据的时间。 异步socketchannel.readincomingBuffer,incomingBuffer, 新的CompletionHandler{ public void已完成整数结果,ByteBuffer缓冲区{ } public void失败可丢弃exc,ByteBuffer缓冲区{ 如果exc instanceof AsynchronousCloseException{ //有人关闭了连接 //当我们在听的时候。 我们监听了插座,但有人把它关了。; } } }; 试一试{ 线程5000; }捕获异常e{ System.out.printlne.toString; } }catch IOException | InterruptedException | ExecutionException ex{ System.err.printlnex; } } }否则{ System.out.PrintLn无法打开异步服务器套接字通道!; } }捕捉异常{ System.err.printlnex; } } 运行此代码并连接netcat nc 10.0.0.201 9000时,如果删除睡眠,5秒后立即从java/服务器端重置连接

我怎样才能阻止它回来,让它继续聆听?
我是否采取了正确的方法来解决我的初始目标?

这是一个符合我要求的工作示例:

    final int SERVER_PORT = 9000;
    final String SERVER_IP = "10.0.0.201";

    AsynchronousChannelGroup group = null;
    try {
        group = AsynchronousChannelGroup.withThreadPool(threadPool);
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    // Create asynchronous server-socket channel bound to the default group.
    AsynchronousServerSocketChannel asynchronousServerSocketChannel = AsynchronousServerSocketChannel.open(group);
    if ( asynchronousServerSocketChannel.isOpen() ) {
        // Bind to local address
        asynchronousServerSocketChannel.bind(new InetSocketAddress(SERVER_IP, SERVER_PORT),
                SERVER_SOCKET_CHANNEL_BACKLOG);
        // Display a waiting message
        System.out.println("Waiting for connections on ip:port " + SERVER_IP + ":" + SERVER_PORT);
        while (true) { // Not good?
            Future<AsynchronousSocketChannel> asynchronousSocketChannelFuture = asynchronousServerSocketChannel
                    .accept();
            final AsynchronousSocketChannel asynchronousSocketChannel = asynchronousSocketChannelFuture.get();

            final SocketAddress remoteAddress = asynchronousSocketChannel.getRemoteAddress();

            System.out.println("Incoming connection from: " + remoteAddress);
            final ByteBuffer incomingBuffer = ByteBuffer.allocateDirect(1024);

            // Time to receive data.
            asynchronousSocketChannel.read(incomingBuffer, incomingBuffer,
                    new CompletionHandler<Integer, ByteBuffer>() {

                        public void completed( Integer result, ByteBuffer buffer ) {
                         // Why flip it?
                            buffer.flip();
                            String msgReceived = Charset.defaultCharset().decode(buffer).toString();
                            System.out.print("Got stuff from the network: " + msgReceived);

                            // Empty the buffer, and listen for new
                            // messages.
                            incomingBuffer.clear();
                            asynchronousSocketChannel.read(incomingBuffer, incomingBuffer, this);
                        }

                        public void failed( Throwable exc, ByteBuffer buffer ) {
                            if ( exc instanceof AsynchronousCloseException ) {
                                // Someone closed the connection
                                // while we where listening on it.
                                System.out.println("We listened on the socket, but someone closed it.");
                            }
                        }
                    });
        }
    }
}