netty解决粘包问题,利用ReplayingDecoder
问题描述
利用netty的ReplayingDecoder解决tcp的粘包和拆包问题
问题出现的环境背景及自己尝试过哪些方法
当客户端发送10条数据给服务器端时,服务器端只接收到了一条总的消息
相关代码
// 请把代码文本粘贴到下方(请勿用图片代替代码)
PersonProtocol.java
public class PersonProtocol {
private int length;
private byte[] content;
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public byte[] getContent() {
return content;
}
public void setContent(byte[] content) {
this.content = content;
}
}
PersonDecoder.java
public class PersonDecoder extends ReplayingDecoder<Void> {
@Override
protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {
int length = byteBuf.readInt();
byte[] content = new byte[length];
byteBuf.readBytes(content);
PersonProtocol personProtocol = new PersonProtocol();
personProtocol.setLength(length);
personProtocol.setContent(content);
list.add(personProtocol);
}
}
PersonEncoder.java
public class PersonEncoder extends MessageToByteEncoder<PersonProtocol> {
@Override
protected void encode(ChannelHandlerContext ctx, PersonProtocol msg, ByteBuf out) throws Exception {
int length = msg.getLength();
byte[] content = msg.getContent();
out.writeInt(length);
out.writeBytes(content);
}
}
Server.java
public class Server {
public static void main(String[] args) throws InterruptedException {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try{
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(bossGroup,workerGroup).channel(NioServerSocketChannel.class).childHandler(new ServerInitlization());
ChannelFuture channelFuture = serverBootstrap.bind(8899).sync();
channelFuture.channel().closeFuture().sync();
}finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}
ServerInitlization.java
public class ServerInitlization extends ChannelInitializer<Channel> {
@Override
protected void initChannel(Channel channel) throws Exception {
ChannelPipeline pipeline = channel.pipeline();
pipeline.addLast(new PersonDecoder());
pipeline.addLast(new PersonEncoder());
pipeline.addLast(new ServerHandler());
}
}
ServerHandler.java
public class ServerHandler extends SimpleChannelInboundHandler<PersonProtocol> {
private int count = 0;
@Override
protected void channelRead0(ChannelHandlerContext ctx, PersonProtocol msg) throws Exception {
String content = new String(msg.getContent(), Charset.forName("utf-8"));
System.out.println("Server - 接收到的消息为:" + content);
System.out.println("Server - 接收到的消息体长度为:" + content.length());
System.out.println("Server - 接收到的消息数量为:" + (++count));
String result = new String("Server接收到的消息为:"+content);
PersonProtocol personProtocol = new PersonProtocol();
personProtocol.setLength(result.length());
personProtocol.setContent(result.getBytes(Charset.forName("utf-8")));
ctx.writeAndFlush(personProtocol);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
}
}
Client.java
public class Client {
public static void main(String[] args) throws InterruptedException {
EventLoopGroup worker = new NioEventLoopGroup();
try {
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(worker).channel(NioSocketChannel.class).handler(new ClientInitialization());
ChannelFuture channelFuture = bootstrap.connect("127.0.0.1", 8899).sync();
channelFuture.channel().closeFuture().sync();
}finally {
worker.shutdownGracefully();
}
}
}
ClientInitialization.java
public class ClientInitialization extends ChannelInitializer<Channel> {
@Override
protected void initChannel(Channel ch) throws Exception {
ChannelPipeline channelPipeline = ch.pipeline();
channelPipeline.addLast(new PersonDecoder());
channelPipeline.addLast(new PersonEncoder());
channelPipeline.addLast(new ClientHandler());
}
}
ClientHandler.java
public class ClientHandler extends SimpleChannelInboundHandler<PersonProtocol> {
private int count = 0;
@Override
protected void channelRead0(ChannelHandlerContext ctx, PersonProtocol msg) throws Exception {
System.out.println("Client - 接收消息:" + msg.getContent());
System.out.println("Client - 接收消息长度:" + msg.getLength());
System.out.println(++count);
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
for(int i = 0 ; i < 10 ; i++){
String body = "来自客户端" + UUID.randomUUID().toString() + ":" + "发送的消息:" + i;
PersonProtocol personProtocol = new PersonProtocol();
personProtocol.setLength(body.length());
personProtocol.setContent(body.getBytes());
ctx.writeAndFlush(personProtocol);
System.out.println("123");
}
}
}
你期待的结果是什么?实际看到的错误信息又是什么?
结果:
服务器端:
Server - 接收到的消息为:来自客户端caa4be43-1957-4d72-8866-518e6dba56
Server - 接收到的消息体长度为:39
Server - 接收到的消息数量为:1
客户端:
123
123
123
123
123
123
123
123
123
123
Client - 接收消息:[B@7cb85a04
Client - 接收消息长度:53
1
期望的结果是服务器端能够收到来自客户端发来的10条消息。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论