title: Netty - Channel instantly closed
body: <p>I've started working with netty and (obviously) want to send messages between clients and server. Since I am in early stage, I have problems with the simple stuff, in this case it's sending a message. This is how I create my server and my client:</p>
  
  <p>Client:</p>
  
  <pre><code>public void run() throws Exception
  {
      EventLoopGroup group = new NioEventLoopGroup();
      try
      {
          Bootstrap b = new Bootstrap();
          b.group(group)
                  .channel(NioSocketChannel.class)
                  .handler(new SecureChatClientInitializer());
          b.option(ChannelOption.SO_KEEPALIVE, true);
  
          // Start the connection attempt.
          ChannelFuture future = b.connect(new InetSocketAddress(host, port));
          Channel ch = future.awaitUninterruptibly().channel();
          ch.writeAndFlush("hi\r\n");
  
          // Wait until all messages are flushed before closing the channel.
          if (lastWriteFuture != null)
          {
              lastWriteFuture.sync();
          }
      } finally
      {
          // The connection is closed automatically on shutdown.
          group.shutdownGracefully();
      }
  }
  </code></pre>
  
  <p>Server:</p>
  
  <pre><code>public void run() throws InterruptedException
  {
      EventLoopGroup bossGroup = new NioEventLoopGroup();
      EventLoopGroup workerGroup = new NioEventLoopGroup();
      try
      {
          ServerBootstrap b = new ServerBootstrap();
          b.group(bossGroup, workerGroup)
                  .channel(NioServerSocketChannel.class)
                  .childHandler(new SecureChatServerInitializer(sessionManager));
          b.option(ChannelOption.SO_KEEPALIVE, true);
          b.bind(port).sync().channel().closeFuture().sync();
  
      } finally
      {
          bossGroup.shutdownGracefully();
          workerGroup.shutdownGracefully();
      }
  }
  </code></pre>
  
  <p>Client Initializer:</p>
  
  <pre><code>public class SecureChatClientInitializer extends ChannelInitializer&lt;SocketChannel&gt;
  {
  
      @Override
      public void initChannel(SocketChannel ch) throws Exception
      {
             ChannelPipeline pipeline = ch.pipeline();
  
          SSLEngine engine =
                  SecureChatSslContextFactory.getClientContext().createSSLEngine();
          engine.setUseClientMode(true);
          pipeline.addLast("ssl", new SslHandler(engine));
          pipeline.addLast("framer", new DelimiterBasedFrameDecoder(
                  8192, Delimiters.lineDelimiter()));
          pipeline.addLast("decoder", new StringDecoder());
          pipeline.addLast("encoder", new StringEncoder());
          pipeline.addLast("handler", new SecureChatClientHandler());
      }
  }
  </code></pre>
  
  <p>Server Initializer:</p>
  
  <pre><code>public class SecureChatServerInitializer extends ChannelInitializer&lt;SocketChannel&gt;
  {
  
      ...
  
      @Override
      public void initChannel(SocketChannel ch) throws Exception
      {
          ChannelPipeline pipeline = ch.pipeline();
  
          SSLEngine engine =
                  SecureChatSslContextFactory.getServerContext().createSSLEngine();
          engine.setUseClientMode(false);
          pipeline.addLast("ssl", new SslHandler(engine));
          pipeline.addLast("framer", new DelimiterBasedFrameDecoder(
                  8192, Delimiters.lineDelimiter()));
          pipeline.addLast("decoder", new StringDecoder());
          pipeline.addLast("encoder", new StringEncoder());
          pipeline.addLast("handler", new SecureChatServerHandler(sessionManager));
      }
  }
  </code></pre>
  
  <p>As you may have guessed from seeing the source code: Yes, parts of it are from the SecureChatExample. I edited parts of it and don't understand why it isn't working anymore. When executing the client, I only get one line of error message:</p>
  
  <pre><code>java.nio.channels.ClosedChannelException
  </code></pre>