title: Netty setting and handling connection
body: <p>I'm not sure if I'm setting right bootstrap for my needs.</p>
  
  <p>As far I have this(i.e. for 2 clients):</p>
  
  <p><strong>Server display only this:</strong></p>
  
  <p>REGISTER</p>
  
  <p>Complete</p>
  
  <p>Added 1 player</p>
  
  <p>[Server] - /127.0.0.1:51850 has joined!</p>
  
  <p>Players online: 1</p>
  
  <p>REGISTER</p>
  
  <p>Complete</p>
  
  <p>Added 2 player</p>
  
  <p>[Server] - /127.0.0.1:51869 has joined!</p>
  
  <p>Players online: 2</p>
  
  <p><strong>When it should display also "Alo..." from each client</strong></p>
  
  <p>Client display data from Server <em>only if</em> next client connect(cards are setting in other function and build correctly). On first(if is only 1 client)/last client display nothing. <strong>I want to display on each client immediately cards when client is connected isntead of waiting for next client.</strong></p>
  
  <p>Setting Server:</p>
  
  <pre><code>EventLoopGroup bossGroup = new NioEventLoopGroup();
          EventLoopGroup workerGroup = new NioEventLoopGroup();
  
          try {
              ServerBootstrap bootstrap = new ServerBootstrap();
                      bootstrap.group(bossGroup,workerGroup)
                      .channel(NioServerSocketChannel.class)
                      .childHandler(new ChatServerInitializer())
                      .option(ChannelOption.SO_BACKLOG, 128)
                      .childOption(ChannelOption.SO_KEEPALIVE, true)
                      .childOption(ChannelOption.TCP_NODELAY, true);
  
                      ChannelFuture f = bootstrap.bind(port).sync();
                       f.channel().closeFuture().sync();
              // bootstrap.bind(port).sync().channel().closeFuture().sync();
          }
          finally { 
              bossGroup.shutdownGracefully();
              workerGroup.shutdownGracefully();
          }
      }
  }
  </code></pre>
  
  <p>ChatServerInitializer: </p>
  
  <pre><code>protected void initChannel(SocketChannel arg0) throws Exception {
          ChannelPipeline pipeline = arg0.pipeline();
  
          pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
          pipeline.addLast("decoder", new StringDecoder());
          pipeline.addLast("encoder", new StringEncoder());
  
          pipeline.addLast("handler", new ChatServerHandler());
      }
  }
  </code></pre>
  
  <p>ChatServerHandler:</p>
  
  <pre><code>static final ChannelGroup channels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
  
  private String cards = "";
  
      @Override
      public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
              super.channelRegistered(ctx);
                  System.out.println("REGISTER");
          }
      @Override
      public void channelActive(final ChannelHandlerContext ctx) throws Exception{
          Channel incoming = ctx.channel();
  
          final ChannelFuture f = incoming.writeAndFlush(cards);
          f.addListener(new ChannelFutureListener() {
              public void operationComplete(ChannelFuture future) {
                 System.out.println("Complete");
              }
          });
  
          for(Channel channel : channels) {
              channel.writeAndFlush("[Server] - " + incoming.remoteAddress() + " has joined!\n");
  
          }
  
          channels.add(ctx.channel());
          showMessage( "[Server] - " + incoming.remoteAddress().toString() + " has joined!" );
          showMessage( "Players online: " + channels.size() );
          System.out.println("Added " + channels.size() + " player");
      }
  
      public void channelInactive(ChannelHandlerContext ctx) throws Exception {
          Channel incoming = ctx.channel();
          for(Channel channel : channels) {
              channel.writeAndFlush("[Server] - " + incoming.remoteAddress() + " has left!\n");
          }
          showMessage( "[Server] - " + incoming.remoteAddress().toString() + " has left!" );
          channels.remove(ctx.channel());
          showMessage( "Players online: " + channels.size() );
  
      }
      @Override
      public void channelRead0(ChannelHandlerContext arg0, String arg1) throws Exception {
          System.out.println(arg1);
      }
  
      }
  </code></pre>
  
  <p>Setting Client:</p>
  
  <pre><code>EventLoopGroup group = new NioEventLoopGroup();
  
          try{
              Bootstrap bootstrap = new Bootstrap()
                      .group(group)
                      .channel(NioSocketChannel.class)
                      .option(ChannelOption.SO_KEEPALIVE, true)
                      .option(ChannelOption.TCP_NODELAY, true)
                      .handler(new ChatClientInitializer());
          //  bootstrap.connect(host,port).sync().channel();
              ChannelFuture f = bootstrap.connect(host, port).sync();
              f.channel().closeFuture().sync();
          }
          finally {
              group.shutdownGracefully();
          }
  </code></pre>
  
  <p>ChatClientInitializer:</p>
  
  <pre><code>@Override
      protected void initChannel(SocketChannel arg0) throws Exception {
  
          ChannelPipeline pipeline = arg0.pipeline();
  
          pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
          pipeline.addLast("decoder", new StringDecoder());
          pipeline.addLast("encoder", new StringEncoder());
  
          pipeline.addLast("handler", new ChatClientHandler());
      }
  </code></pre>
  
  <p>ChatClientHandler:</p>
  
  <pre><code>public void channelActive(ChannelHandlerContext ctx){
          final ChannelFuture f = ctx.writeAndFlush("Alo...");
          f.addListener(new ChannelFutureListener() {
              public void operationComplete(ChannelFuture future) {
                 System.out.println("Completed..");
              }
          });
      }
  
      public void channelRead0(ChannelHandlerContext arg0, String arg1) throws Exception{
              System.out.println(arg1);
              showMessage(arg1);
      }
  </code></pre>
  
  <p>What I finnaly want is:
  Connect 8 Clients to Server. Each Client should get from the Server, the same 5 cards(deck) and for each 2 diffrent cards(hand). Like in texas holdem. And then Server should listen if Clients bet/check/fold and interact with each respond.
  Is that bootstrap setting good? Should I do all this code in Client/Server Handler?</p>