1. @ChannelHandler.Sharable
    2. public class FixedLengthServerHandler extends ChannelInboundHandlerAdapter {
    3. private AtomicInteger counter = new AtomicInteger(0);
    4. /*** 服务端读取到网络数据后的处理*/
    5. @Override
    6. public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    7. ByteBuf in = (ByteBuf) msg;
    8. String request = in.toString(CharsetUtil.UTF_8);
    9. System.out.println("Server Accept["+request
    10. +"] and the counter is:"+counter.incrementAndGet());
    11. ctx.writeAndFlush(Unpooled.copiedBuffer(
    12. FixedLengthEchoServer.RESPONSE.getBytes()));
    13. }
    14. /*** 发生异常后的处理*/
    15. @Override
    16. public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
    17. cause.printStackTrace();
    18. ctx.close();
    19. }
    20. }