title: How to detect a new incoming connection with Netty 4.0.x?
body: <p>How can I detect a new incoming connection with Netty 4.0.x?
  Here is a code of a simple server:</p>
  
  <pre><code>public static void main(String[] args) {
      EventLoopGroup bossGroup = new NioEventLoopGroup();
      EventLoopGroup workerGroup = new NioEventLoopGroup();
      try {
          ServerBootstrap b = new ServerBootstrap();
          b.group(bossGroup, workerGroup)
           .channel(NioServerSocketChannel.class)
           .childHandler(new ChannelInitializer&lt;SocketChannel&gt;() {
               @Override
               public void initChannel(SocketChannel ch) throws Exception {
                   System.out.println("Init channel");
  
                   ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {
  
                      @Override
                      public void channelRegistered(ChannelHandlerContext ctx) {
                          System.out.println("Channel registered");
                      }
  
                      @Override
                      public void channelActive(final ChannelHandlerContext ctx) {
                          System.out.println("Channel active");
                      }
  
                      @Override
                      public void channelRead(ChannelHandlerContext ctx, Object msg) {
                          System.out.println("Channel read");
                          ctx.writeAndFlush((ByteBuf) msg);
                      }
  
                      @Override
                      public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
                          System.out.println("Channel exception caught");
                          cause.printStackTrace();
                          ctx.close();
                      }
  
                   });
               }
           })
           .option(ChannelOption.SO_BACKLOG, 128)
           .childOption(ChannelOption.SO_KEEPALIVE, true);
  
          ChannelFuture f;
          try {
              f = b.bind("127.0.0.1", 8080).sync();
              f.channel().closeFuture().sync();
          } catch (InterruptedException e) {
              e.printStackTrace();
          }
      } finally {
          workerGroup.shutdownGracefully();
          bossGroup.shutdownGracefully();
      }
  }
  </code></pre>
  
  <p>The problem is that I can not detect a moment when a connection is established. All <em>System.out.printlns</em> are called only after the client sent some information to the server. And this means that the server can not send any data to the client before the client sent some data to the server.</p>
  
  <p><em>What I expect:</em></p>
  
  <ul>
  <li>server started</li>
  <li>client started</li>
  <li>client connected</li>
  <li> {{ Init channel }}</li>
  <li> {{ Channel registered }}</li>
  <li> {{ Channel active }}</li>
  <li> // Possibly send some data from server to client</li>
  <li>Client send some data</li>
  <li> {{ Channel read }}</li>
  </ul>
  
  <p><em>What I get:</em></p>
  
  <ul>
  <li>server started</li>
  <li>client started</li>
  <li>client connected</li>
  <li>// Unable to send any data to client</li>
  <li>Client send some data</li>
  <li> {{ Init channel }}</li>
  <li> {{ Channel registered }}</li>
  <li> {{ Channel active }}</li>
  <li> {{ Channel read }}</li>
  </ul>
  
  <p>Are there any ways to achieve such behavior?</p>