Netty粘包,拆包问题以及自定义协议解决方案
问题概述
由于Tcp是面向连接的,面向流的,提供高可靠性的服务。收发两端都要有一一成对的socket。因此,发送端为了将多个发送给接收端的包更有效的发送给对方,使用了Nagle算法,将多次间隔较小且数据量较小的数据,合并成了一个大的数据块,然后进行封包发送给对方。这样虽然能提升效率,但是接收端很难分辨出完整的数据包了。
Tcp半包拆包演示
第一行数据没有发生粘包和拆包现象
第二行接收端一次性接收到了两个独立的数据包,称之为粘包
第三行第一次读取到了完整的D1包和D2部分内容,第二次读取到了D2剩余内容,称之为拆包
第四行第一次读取到了完整的D1的部分内容,第二次读取到了D1剩余内容和D2完整内容,称之为拆包
Tcp粘包半包问题解决
可以使用自定义协议以及编解码器来解决,关键就是在于接收端每次读取数据长度的问题,能够确认读取长度就不会产生粘包拆包现象了
小案例:模拟一个客户端循环发送五次数据包给服务器端
自定义协议类
public class MessagePoJo {
//每次读取长度
private int length;
//内容
private byte [] concat;
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public byte[] getConcat() {
return concat;
}
public void setConcat(byte[] concat) {
this.concat = concat;
}
}
编解码器
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ReplayingDecoder;
import java.util.List;
//解码
public class MyMessageDecode extends ReplayingDecoder<Void> {
@Override
protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {
int length = byteBuf.readInt();
byte[] b = new byte[length];
byteBuf.readBytes(b);
MessagePoJo messagePoJo = new MessagePoJo();
messagePoJo.setLength(length);
messagePoJo.setConcat(b);
list.add(messagePoJo);
}
}
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
//编码
public class MyMessageEncode extends MessageToByteEncoder<MessagePoJo> {
@Override
protected void encode(ChannelHandlerContext channelHandlerContext, MessagePoJo messagePoJo, ByteBuf byteBuf) throws Exception {
byteBuf.writeInt(messagePoJo.getLength());
byteBuf.writeBytes(messagePoJo.getConcat());
}
}
服务器端代码
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
/**
* 自定义协议解决粘包半包问题
* */
public class MyServer {
public static void main(String[] args) {
EventLoopGroup bossLoopGroup = new NioEventLoopGroup(1);
EventLoopGroup workLoopGroup = new NioEventLoopGroup();
try{
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(bossLoopGroup,workLoopGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
ChannelPipeline pipeline = socketChannel.pipeline();
pipeline.addLast(new MyMessageDecode());
pipeline.addLast(new MyMessageEncode());
pipeline.addLast(new MyServerHandler());
}
});
ChannelFuture future = serverBootstrap.bind(8686).sync();
future.channel().closeFuture().sync();
}catch (Exception e){
e.printStackTrace();
}finally {
bossLoopGroup.shutdownGracefully();
workLoopGroup.shutdownGracefully();
}
}
}
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.CharsetUtil;
public class MyServerHandler extends SimpleChannelInboundHandler<MessagePoJo> {
@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, MessagePoJo messagePoJo) throws Exception {
int length = messagePoJo.getLength();
byte[] concat = messagePoJo.getConcat();
String s = new String(concat, CharsetUtil.UTF_8);
System.out.println("长度"+length+"内容"+s);
}
}
客户端代码
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
public class MyClient {
public static void main(String[] args) {
EventLoopGroup group = new NioEventLoopGroup();
try{
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(group).channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
ChannelPipeline pipeline = socketChannel.pipeline();
pipeline.addLast(new MyMessageEncode());
pipeline.addLast(new MyMessageDecode());
pipeline.addLast(new MyClientHandler());
}
});
ChannelFuture future = bootstrap.connect("127.0.0.1", 8686).sync();
future.channel().closeFuture().sync();
}catch (Exception e){
e.printStackTrace();
}finally {
group.shutdownGracefully();
}
}
}
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.CharsetUtil;
import java.nio.charset.Charset;
public class MyClientHandler extends SimpleChannelInboundHandler<MessagePoJo> {
@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, MessagePoJo messagePoJo) throws Exception {
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
for (int i = 0; i < 5; i++) {
MessagePoJo messagePoJo = new MessagePoJo();
byte[] bytes = ("你好啊" + i + "次").getBytes(CharsetUtil.UTF_8);
messagePoJo.setLength(bytes.length);
messagePoJo.setConcat(bytes);
ctx.writeAndFlush(messagePoJo);
}
}
}
结果可以看出没有发生拆包粘包现象