| import io.netty.buffer.ByteBuf;  | 
| import io.netty.buffer.ByteBufUtil;  | 
| import io.netty.buffer.Unpooled;  | 
| import io.netty.channel.ChannelHandlerContext;  | 
| import io.netty.channel.SimpleChannelInboundHandler;  | 
| import io.netty.util.CharsetUtil;  | 
|   | 
| import java.nio.charset.Charset;  | 
|   | 
| /**  | 
|  * Created by a on 2017/7/27.  | 
|  */  | 
| public class EchoClientHandler  extends SimpleChannelInboundHandler<ByteBuf> {  | 
|   | 
|     @Override  | 
|     public void channelActive(ChannelHandlerContext ctx) throws Exception {  | 
|         System.out.println("client channelActive..");  | 
|         ctx.writeAndFlush(Unpooled.copiedBuffer("Netty rocks!", CharsetUtil.UTF_8)); // 必须有flush  | 
|   | 
|         // 必须存在flush  | 
|         // ctx.write(Unpooled.copiedBuffer("Netty rocks!", CharsetUtil.UTF_8));  | 
|         // ctx.flush();  | 
|     }  | 
|   | 
|     @Override  | 
|     protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {  | 
|         System.out.println("client channelRead..");  | 
|         ByteBuf buf = msg.readBytes(msg.readableBytes());  | 
|         System.out.println("Client received:" + ByteBufUtil.hexDump(buf) + "; The value is:" + buf.toString(Charset.forName("utf-8")));  | 
|         //ctx.channel().close().sync();// client关闭channel连接  | 
|     }  | 
|   | 
|     @Override  | 
|     public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {  | 
|         cause.printStackTrace();  | 
|         ctx.close();  | 
|     }  | 
|   | 
|   | 
| }  |