1. public class DelimiterClientHandler extends SimpleChannelInboundHandler<ByteBuf> {
    2. private AtomicInteger counter = new AtomicInteger(0);
    3. /*** 客户端读取到网络数据后的处理*/
    4. protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
    5. System.out.println("client Accept["+msg.toString(CharsetUtil.UTF_8)
    6. +"] and the counter is:"+counter.incrementAndGet());
    7. }
    8. /*** 客户端被通知channel活跃后,做事*/
    9. @Override
    10. public void channelActive(ChannelHandlerContext ctx) throws Exception {
    11. ByteBuf msg = null;
    12. String request = "Mark,Lison,King,James,Deer"
    13. + DelimiterEchoServer.DELIMITER_SYMBOL;
    14. for(int i=0;i<10;i++){
    15. msg = Unpooled.buffer(request.length());
    16. msg.writeBytes(request.getBytes());
    17. ctx.writeAndFlush(msg);
    18. }
    19. }
    20. /*** 发生异常后的处理*/
    21. @Override
    22. public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
    23. cause.printStackTrace();
    24. ctx.close();
    25. }
    26. }