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();
}
}
本文来自博客园,作者:余生请多指教ANT,转载请注明原文链接:https://www.cnblogs.com/wangbiaohistory/p/18707857

浙公网安备 33010602011771号