Tcp的粘包拆包
1.TCP是面向连接的,面向流的,提供高可靠性服务,收发两端,都要有一一成对的socket,因此,发送端为了将多个发给接收端的包,更有效的发给对方,使用了优化方法(Nagle算法),将多次间隔较小且数据量小的数据,合成一个大的数据块,然后进行封包,这样做虽然提高了效率,但是接收端就难于分辨出完整的数据包了,因为面向流的通信是无消息保护边界的
2.由于TCP无消息保护边界,需要在接收端处理消息边界问题,也就是我们所说的粘包,拆包问题.
TCP粘包,拆包图解
假设客户端分别发送了两个数据包D1 D2给服务端,由于服务端一次读取到的字节数是不确定的,故可能存在以下四种情况
1)服务端分两次读取到了两个独立的数据包,分别是D1 D2 没有粘包和拆包
2)服务端一次接受了两个数据包,D1 D2粘合在一起 成为TCP粘包
3)服务端分两次读取带了数据包,第一次读取到了完整的D1包和D2包的部分内容,第二次读取到了D2的剩余内容,这称之为TCP拆包
4)服务端分两次读取到了数据包,第一次读取到了D1包的部分内容D1_1,第二次读取到了D1包的剩余部分内容D1_2和完整的D2包.
TCP粘包和拆包的解决方案
1.使用自定义协议+编解码器来解决.
2.关键就是要解决服务器端每次读取数据长度的问题,这个问题解决,就不会初选服务器多读或者少读数据的问题,从而避免的TCP粘包,拆包.
自定义Tcp数据包:
有两个属性,一个长度,一个内容
package com.jym.protocoltcp;
/**
* @program: NettyPro
* @description:
* @author: jym
* @create: 2020/02/12
*/
public class MessageProtocol {
private int len;
private byte[] bytes;
public int getLen() {
return len;
}
public void setLen(int len) {
this.len = len;
}
public byte[] getBytes() {
return bytes;
}
public void setBytes(byte[] bytes) {
this.bytes = bytes;
}
}
自定义的编解码器:
编码器:
package com.jym.protocoltcp;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
/**
* @program: NettyPro
* @description:
* @author: jym
* @create: 2020/02/12
*/
public class MessageEncoder extends MessageToByteEncoder<MessageProtocol> {
private int count;
@Override
protected void encode(ChannelHandlerContext channelHandlerContext, MessageProtocol messageProtocol, ByteBuf byteBuf) throws Exception {
System.out.println("MessageEncoder encode方法被调用"+(++this.count)+"次");
byteBuf.writeInt(messageProtocol.getLen());
byteBuf.writeBytes(messageProtocol.getBytes());
}
}
解码器:
package com.jym.protocoltcp;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ReplayingDecoder;
import java.util.List;
/**
* @program: NettyPro
* @description:
* @author: jym
* @create: 2020/02/12
*/
public class MessageDecoder extends ReplayingDecoder<Void> {
private int count;
@Override
protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {
System.out.println("MessageDecoder 方法被调用"+(++this.count));
// 将二进制字节码 -> messageProtocol
int length = byteBuf.readInt();
byte[] content = new byte[length];
byteBuf.readBytes(content);
// 分装成messageProtocol 给下一个handler去处理
MessageProtocol messageProtocol = new MessageProtocol();
messageProtocol.setLen(length);
messageProtocol.setBytes(content);
list.add(messageProtocol);
}
}
服务端:
package com.jym.protocoltcp;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
/**
* @program: NettyPro
* @description:
* @author: jym
* @create: 2020/02/11
*/
public class TcpServer {
public static void main(String[] args) {
EventLoopGroup boosGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(boosGroup,workerGroup).channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG,128)
.childOption(ChannelOption.SO_KEEPALIVE,true)
.childHandler(new TcpSeverInitializer());
ChannelFuture sync = serverBootstrap.bind(7000).sync();
sync.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
boosGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}
服务端Initializer与handler
package com.jym.protocoltcp;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
/**
* @program: NettyPro
* @description:
* @author: jym
* @create: 2020/02/12
*/
public class TcpSeverInitializer extends ChannelInitializer<SocketChannel> {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
ChannelPipeline pipeline = socketChannel.pipeline();
pipeline.addLast(new MessageDecoder());
pipeline.addLast(new MessageEncoder());
pipeline.addLast(new TcpSeverHandler());
}
}
package com.jym.protocoltcp;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import java.nio.charset.Charset;
import java.util.UUID;
/**
* @program: NettyPro
* @description:
* @author: jym
* @create: 2020/02/12
*/
public class TcpSeverHandler extends SimpleChannelInboundHandler<MessageProtocol> {
private int count;
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
ctx.close();
}
@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, MessageProtocol messageProtocol) throws Exception {
int len = messageProtocol.getLen();
byte[] bytes = messageProtocol.getBytes();
System.out.println("服务端接收信息如下");
System.out.println("长度=" + len);
System.out.println("内容=" + new String(bytes, Charset.forName("utf-8")));
System.out.println("服务器接受消息包的数量" +(++this.count));
// 回复消息
String s = UUID.randomUUID().toString();
int length = s.getBytes("utf-8").length;
MessageProtocol resp = new MessageProtocol();
resp.setLen(length);
resp.setBytes(s.getBytes("utf-8"));
channelHandlerContext.writeAndFlush(resp);
}
}
客户端:
package com.jym.protocoltcp;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
/**
* @program: NettyPro
* @description:
* @author: jym
* @create: 2020/02/11
*/
public class TcpClient {
public static void main(String[] args) {
NioEventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(group).channel(NioSocketChannel.class)
.handler(new TcpClientInitializer());
ChannelFuture sync = bootstrap.connect("127.0.0.1", 7000).sync();
sync.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
group.shutdownGracefully();
}
}
}
服务端Initializer与handler
package com.jym.protocoltcp;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
/**
* @program: NettyPro
* @description:
* @author: jym
* @create: 2020/02/12
*/
public class TcpClientInitializer extends ChannelInitializer<SocketChannel> {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
ChannelPipeline pipeline = socketChannel.pipeline();
pipeline.addLast(new MessageEncoder());
pipeline.addLast(new MessageDecoder());
pipeline.addLast(new TcpClientHandler());
}
}
package com.jym.protocoltcp;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import java.nio.charset.Charset;
/**
* @program: NettyPro
* @description:
* @author: jym
* @create: 2020/02/12
*/
public class TcpClientHandler extends SimpleChannelInboundHandler<MessageProtocol> {
private int count;
/**
* 使用客户端发送10条数据,保温杯里泡枸杞
*/
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
for(int i = 0; i < 10; i++){
String message = "保温杯里泡枸杞";
byte[] bytes = message.getBytes(Charset.forName("utf-8"));
int length = message.getBytes(Charset.forName("utf-8")).length;
// 创建协议包
MessageProtocol messageProtocol = new MessageProtocol();
messageProtocol.setLen(length);
messageProtocol.setBytes(bytes);
ctx.writeAndFlush(messageProtocol);
}
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
ctx.close();
}
@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, MessageProtocol messageProtocol) throws Exception {
System.out.println("从服务端接受到消息:");
System.out.println("接收到的长度为:"+ messageProtocol.getLen());
System.out.println("接收到的内容为:"+ new String(messageProtocol.getBytes(),Charset.forName("utf-8")));
}
}
主要是通过自定义数据包,让handler每次处理一个数据包,解决读取数据长度