1. package net.xdclass.echo;
    2. import io.netty.buffer.ByteBuf;
    3. import io.netty.buffer.Unpooled;
    4. import io.netty.channel.ChannelHandlerContext;
    5. import io.netty.channel.SimpleChannelInboundHandler;
    6. import io.netty.util.CharsetUtil;
    7. public class EchoClientHandler extends SimpleChannelInboundHandler<ByteBuf> {
    8. protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
    9. System.out.println("Client received: " + msg.toString(CharsetUtil.UTF_8));
    10. }
    11. @Override
    12. public void channelActive(ChannelHandlerContext ctx) throws Exception {
    13. System.out.println("Active");
    14. ctx.writeAndFlush(Unpooled.copiedBuffer("小滴课堂 xdclass.net",CharsetUtil.UTF_8));
    15. }
    16. @Override
    17. public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
    18. System.out.println("EchoClientHandler channelReadComplete");
    19. }
    20. @Override
    21. public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
    22. cause.printStackTrace();
    23. ctx.close();
    24. }
    25. }