title: Netty send message to Server without for loop
body: <p>I was using the following code to send messages from the Client to the Server:</p>
  
  <p>Client class:</p>
  
  <pre><code>public class Client {
  
    String host = "localhost";
    int port = 14930;
    private final ClientHandler clientHandler = new ClientHandler();
  
    public Client(String host, int port) {
      this.host = host;
      this.port = port;
    }
  
    public void run() throws Exception {
  
      try {
        workerGroup = new NioEventLoopGroup();
        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 {
            ChannelPipeline pipeline = ch.pipeline();
            pipeline.addLast(new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
            pipeline.addLast(new StringDecoder());
            pipeline.addLast(new StringEncoder());
            pipeline.addLast(clientHandler);
          }
        }
        );
  
        ChannelFuture f = b.connect(host, port).sync();
  
        f.channel().closeFuture().sync();
      }
      finally {
        workerGroup.shutdownGracefully();
      }
    }
    public void writeMessage(String msg) {
      clientHandler.sendMessage(msg);
    }
  }
  </code></pre>
  
  <p>Handler class:</p>
  
  <pre><code>public class ClientHandler extends SimpleChannelInboundHandler&lt;String&gt; {
    ChannelHandlerContext ctx;
  
    public void sendMessage(String msgToSend) {
      ctx.writeAndFlush(Unpooled.copiedBuffer(msgToSend, CharsetUtil.UTF_8));
    }
  
    @Override
      public void channelActive(ChannelHandlerContext ctx) throws Exception {
      this.ctx = ctx;
    }
  
    @Override
      public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
      cause.printStackTrace();
      ctx.close();
    }
  
    @Override
      protected void channelRead0(ChannelHandlerContext arg0, String msg) throws Exception {
    }
  
    void channelInactive(ChannelHandlerContext ctx) throws Exception {
    }
  }
  </code></pre>
  
  <p>I am creating the Client like this:</p>
  
  <pre><code>Client client;
  client = new Client(ipAddress, serverPort);
  client.run();
  client.writeMessage("random_text");
  </code></pre>
  
  <p>The Server:</p>
  
  <pre><code>public final class ChatServer {
  
    public ChatServer(int PORT) throws Exception {
  
      bossGroup = new NioEventLoopGroup();
      workerGroup = new NioEventLoopGroup();
  
      try {
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup)
          .channel(NioServerSocketChannel.class)
            .handler(new LoggingHandler(LogLevel.INFO))
              .childHandler(new ChatServerInitializer());
  
        b.bind(PORT).sync().channel().closeFuture().sync();
      } 
      finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
      }
    }
  }
  </code></pre>
  
  <p>ServerHandler:</p>
  
  <pre><code>public class ChatServerHandler extends SimpleChannelInboundHandler&lt;String&gt; {
  
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
      Channel incoming = ctx.channel();
      channels.add(ctx.channel());
    }
  
    public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
      Channel incoming = ctx.channel();
      channels.remove(ctx.channel());
    } 
  
    @Override
      public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
      cause.printStackTrace();
      ctx.close();
    }
  
    @Override
      protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
      println(msg);
    }
  }
  </code></pre>
  
  <p>Method to send Message from Server to Client:</p>
  
  <pre><code>public void sendMessage(String message) {
    if (message != "" &amp;&amp; message != null) {
      for (Channel c : channels) {
        c.writeAndFlush(message + "\r\n");
      }
    }
  }
  </code></pre>
  
  <p>The problem is that the Server does not get any Messages. I tried some debug steps, the Server adds the Client to its channels, and channelActive gets executed successfully on the Client side. </p>