content: package com.lgp.netty.netty_ch1;
  
  import io.netty.buffer.ByteBuf;
  import io.netty.buffer.Unpooled;
  import io.netty.channel.ChannelHandler.Sharable;
  import io.netty.channel.ChannelHandlerContext;
  import io.netty.channel.SimpleChannelInboundHandler;
  import io.netty.util.CharsetUtil;
  
  
  @Sharable
  public class EchoClientHandler extends SimpleChannelInboundHandler<ByteBuf> {
      @Override
      public void channelActive(ChannelHandlerContext ctx) {
          //当被通知 Channel是活跃的时候，发送一条消息，此方法连接建立成功只会调用一次
          ctx.writeAndFlush(Unpooled.copiedBuffer("hello world!", CharsetUtil.UTF_8));
      }
  
      @Override
      //在发生异常时，记录错误并关闭Channel
      public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
          cause.printStackTrace();
          ctx.close();
      }
  
      /**
       * 记录接收到的消息
       */
  
      @Override
      protected void channelRead0(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf) throws Exception {
          System.out.println(System.currentTimeMillis() + " netty_ch1 Client received: " + byteBuf.toString(CharsetUtil.UTF_8));
      }
  }
  