title: Communication in Netty Nio java
body: <p>I want to create a communication system with two clients and a server in Netty nio. More specifically, firstly, I want when two clients are connected with the server to send a message from the server and after that to be able to exchnage data between the two clients. I am using the <a href="https://github.com/eugenp/tutorials/tree/master/libraries/src/main/java/com/baeldung/netty" rel="noreferrer">code provided from this example</a>. My modifications in the code can be found here: <a href="https://github.com/kristosh/netty-nio-Client-Server" rel="noreferrer">link</a></p>
  
  <p>It seems that the channelRead in the serverHandler works when the first client is connceted so it always return 1 but when a second client is connected does not change to 2. How can I check properly from the server when both clients are connected to the server? How can I read this value dynamically from my main function of the Client? Then which is the best way to let both clients communicate?</p>
  
  <p><strong>EDIT1:</strong> Apparently it seems that the client service is running and close directly so every time that I am running a new NettyClient is connected but the connection is closed after that. So the counter is always chnages from zero to one. As I was advised in the below comments I tested it using telnet in the same port and the counter seems to increasing normally, however, with the NettyClient service no.</p>
  
  <p><strong>EDIT2:</strong> It seems that the issue I got was from <code>future.addListener(ChannelFutureListener.CLOSE);</code> which was in <code>channelRead</code> in the <code>ProcessingHandler class</code>. When I commented it that out it seems that the code works. However, am not sure what are the consequences of commented that out. Moreover, I want from my main function of the client to check when the return message is specific two. How, could I create a method that waits for a specific message from server and meanwhile it blocks the main functionality. </p>
  
  <pre><code> static EventLoopGroup workerGroup = new NioEventLoopGroup();
   static Promise&lt;Object&gt; promise = workerGroup.next().newPromise(); 
   public static void callClient() throws Exception {
      String host = "localhost";
      int port = 8080;
      try {
          Bootstrap b = new Bootstrap();
          b.group(workerGroup);
          b.channel(NioSocketChannel.class);
          b.option(ChannelOption.SO_KEEPALIVE, true);
          b.handler(new ChannelInitializer&lt;SocketChannel&gt;() {
              @Override
              public void initChannel(SocketChannel ch) throws Exception {
                  ch.pipeline().addLast(new RequestDataEncoder(), new ResponseDataDecoder(), new ClientHandler(promise));
              }
          });
          ChannelFuture f = b.connect(host, port).sync();
      } finally {
          //workerGroup.shutdownGracefully();
      }
  }
  </code></pre>
  
  <p>I want inside the main function to call the method and return the result and when it is 2 to continue with the main functionality. However, I cannot call callClient inside the while since it will run multiple times the same client. </p>
  
  <pre><code>   callBack();
      while (true) {
          Object msg = promise.get();
          System.out.println("Case1: the connected clients is not two");
          int ret = Integer.parseInt(msg.toString());
          if (ret == 2){
              break;
          }
      }
     System.out.println("Case2: the connected clients is two");
     // proceed with the main functionality
  </code></pre>
  
  <p>How can I update the promise variable for the first client. When I run two clients, for the first client I always received the message :</p>
  
  <blockquote>
    <p>Case1: the connected clients is not two</p>
  </blockquote>
  
  <p>seems that the promise is not updated normally, while for the second client I always received the:</p>
  
  <blockquote>
    <p>Case2: the connected clients is two</p>
  </blockquote>