netty粘包半包自定义填充长度解码

package com.io.netty.netty.netty12;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.Random;

@Slf4j
public class HelloWorldClient {


    //填充,每次填充10个字节,不足10字节填满到10字节
    public static byte[] fill0Bytes(char c, int len) {
        byte[] result = new byte[10];
        Arrays.fill(result, (byte) '_');
        byte charByte = (byte) c;
        for (int i = 0; i < len; i++) {
            result[i] = charByte;
        }
        return result;
    }

    static void start() {
        NioEventLoopGroup worker = new NioEventLoopGroup();
        try {
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.channel(NioSocketChannel.class);
            bootstrap.group(worker);
            bootstrap.handler(new ChannelInitializer<SocketChannel>() {
                @Override
                protected void initChannel(SocketChannel ch) {
                    ch.pipeline().addLast(new LoggingHandler(LogLevel.DEBUG));
                    ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {
                        @Override
                        public void channelActive(ChannelHandlerContext ctx) throws Exception {
                            ByteBuf buf=ctx.alloc().buffer();
                            char c = '0';
                            Random random = new Random();
                            for (int i = 0; i < 10; i++) {
                                byte[] bytes = fill0Bytes(c, random.nextInt(10) + 1);
                                c++;
                                buf.writeBytes(bytes);
                            }
                            ctx.writeAndFlush(buf);
                        }
                    });
                }
            });
            ChannelFuture localhost = bootstrap.connect("localhost", 9091).sync();
            localhost.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            log.debug("server error", e);
        } finally {
            worker.shutdownGracefully();
        }


    }

    public static void main(String[] args) {
            start();
    }
}

  

 

package com.io.netty.netty.netty12;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.Random;

@Slf4j
public class HelloWorldClient {


    //填充,每次填充10个字节,不足10字节填满到10字节
    public static byte[] fill0Bytes(char c, int len) {
        byte[] result = new byte[10];
        Arrays.fill(result, (byte) '_');
        byte charByte = (byte) c;
        for (int i = 0; i < len; i++) {
            result[i] = charByte;
        }
        return result;
    }

    static void start() {
        NioEventLoopGroup worker = new NioEventLoopGroup();
        try {
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.channel(NioSocketChannel.class);
            bootstrap.group(worker);
            bootstrap.handler(new ChannelInitializer<SocketChannel>() {
                @Override
                protected void initChannel(SocketChannel ch) {
                    ch.pipeline().addLast(new LoggingHandler(LogLevel.DEBUG));
                    ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {
                        @Override
                        public void channelActive(ChannelHandlerContext ctx) throws Exception {
                            ByteBuf buf=ctx.alloc().buffer();
                            char c = '0';
                            Random random = new Random();
                            for (int i = 0; i < 10; i++) {
                                byte[] bytes = fill0Bytes(c, random.nextInt(10) + 1);
                                c++;
                                buf.writeBytes(bytes);
                            }
                            ctx.writeAndFlush(buf);
                        }
                    });
                }
            });
            ChannelFuture localhost = bootstrap.connect("localhost", 9091).sync();
            localhost.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            log.debug("server error", e);
        } finally {
            worker.shutdownGracefully();
        }


    }

    public static void main(String[] args) {
            start();
    }
}

  

posted @ 2025-02-10 14:43  余生请多指教ANT  阅读(14)  评论(0)    收藏  举报