title: How to make Asynchronous tcp server continue to listen on channel and not close it
body: <p>I am trying to make a server that is able to handle 1000+ connections from clients. This is part academic, part hobby project, so I kinda want to make my own solution, but I am facing a problem: When I start to listen for connections, and someone connects, the TCP connection is getting closed by java after 5 seconds. I know that this is from my 5 second sleep, but if I remove it, it returns immediately.</p>
  
  <p>Here is my server code (stripped down):</p>
  
  <pre class="lang-java prettyprint-override"><code>    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.
      try(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&lt;AsynchronousSocketChannel&gt; asynchronousSocketChannelFuture = asynchronousServerSocketChannel
                          .accept();
                  try(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&lt;Integer, ByteBuffer&gt;() {
  
                                  public void completed( Integer result, ByteBuffer buffer ) {
  
                                  }
  
                                  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.");
                                      }
                                  }
                              });
  
                      try {
                          Thread.sleep(5000);
                      } catch (Exception e) {
                          System.out.println(e.toString());
                      }
                  } catch (IOException | InterruptedException | ExecutionException ex) {
                      System.err.println(ex);
                  }
              }
          } else {
              System.out.println("The asynchronous server-socket channel cannot be opened!");
          }
      } catch (IOException ex) {
          System.err.println(ex);
      }
  }
  </code></pre>
  
  <p>When running this code, and connecting with netcat "nc 10.0.0.201 9000" the connection is reset from the java/server side after 5 seconds (immediately if removing the sleep). </p>
  
  <p>How can I stop it from returning, and make it continue to listen?
  Am I taking the right approch on solving my initial goal?</p>