1.TCP是面向连接、面向流的,提供高可靠性服务,收发两端(客户端和服务端)都要有一 一成对的socket,因此,发送端为了将多个发送给接收端的包,更有效的发送给对方,使用了优化方法(Nagle算法),将多次间隔较小且数据量小的数据,合并成一个大的数据块,然后进行封包。这样做虽然提高了效率,但是接收端就难于分辨出完整的数据包了,因为面向流的通信是无消息保护边界的
2.由于TCP无消息保护边界,需要在接收端处理消息边界问题,也就是我们说的粘包、拆包问题

问题演示
接收方可能将数据分为多次接收,不能保证消息的准确
解决
1.使用自定义协议 + 编解码器来解决
2.关键就是要解决 服务器端每次读取数据长度的问题,这个问题解决,就不会出现服务器多读或少读数据的问题,从而避免TCP 粘包、拆包
自定义协议包
//协议包public class MessageProtocol {private int len;private byte[] content;public int getLen() {return len;}public void setLen(int len) {this.len = len;}public byte[] getContent() {return content;}public void setContent(byte[] content) {this.content = content;}}
ClientInitializer
public class MyClientInitializer extends ChannelInitializer<SocketChannel> {@Overrideprotected void initChannel(SocketChannel ch) throws Exception {ChannelPipeline pipeline = ch.pipeline();pipeline.addLast(new MyMessageEncoder());pipeline.addLast(new MyClientHandler());}}
Encoder
public class MyMessageEncoder extends MessageToByteEncoder<MessageProtocol> {@Overrideprotected void encode(ChannelHandlerContext ctx, MessageProtocol msg, ByteBuf out) throws Exception {System.out.println("MyMessageEncoder--encode");out.writeInt(msg.getLen());out.writeBytes(msg.getContent());}}
ClientHandler
public class MyClientHandler extends SimpleChannelInboundHandler<MessageProtocol> {private int count;@Overrideprotected void channelRead0(ChannelHandlerContext ctx, MessageProtocol msg) throws Exception {}@Overridepublic void channelActive(ChannelHandlerContext ctx) throws Exception {//发送10条数据for (int i = 0; i < 5; i++) {String msg = "zzzzzzz";byte[] bytes = msg.getBytes(StandardCharsets.UTF_8);int length = msg.getBytes(StandardCharsets.UTF_8).length;//创建协议包MessageProtocol messageProtocol = new MessageProtocol();messageProtocol.setLen(length);messageProtocol.setContent(bytes);ctx.writeAndFlush(messageProtocol);}}}
Client
public class MyClient {public static void main(String[] args) throws Exception {EventLoopGroup group = new NioEventLoopGroup();try {Bootstrap bootstrap = new Bootstrap();bootstrap.group(group).channel(NioSocketChannel.class).handler(new MyClientInitializer()); //自定义一个初始化类ChannelFuture channelFuture = bootstrap.connect("localhost", 7000).sync();channelFuture.channel().closeFuture().sync();} finally {group.shutdownGracefully();}}}
ServerInitializer
public class MyServerInitializer extends ChannelInitializer<SocketChannel> {@Overrideprotected void initChannel(SocketChannel ch) throws Exception {ch.pipeline().addLast(new MyMessageDecoder()).addLast(new MyServerHandler());}}
Decoder
public class MyMessageDecoder extends ReplayingDecoder<Void> {@Overrideprotected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {System.out.println("decode");//将得到的二进制字节码转化为messageProtocolint len = in.readInt();byte[] content = new byte[len];in.readBytes(content);//封装成messageProtocol对象,传递给下一个handler进行处理MessageProtocol messageProtocol = new MessageProtocol();messageProtocol.setLen(len);messageProtocol.setContent(content);out.add(messageProtocol);}}
ServerHandler
public class MyServerHandler extends SimpleChannelInboundHandler<MessageProtocol> {private int count;@Overrideprotected void channelRead0(ChannelHandlerContext ctx, MessageProtocol msg) throws Exception {int len = msg.getLen();byte[] content = msg.getContent();String s = new String(content, StandardCharsets.UTF_8);System.out.println(s);System.out.println("++++++++++++++++++");System.out.println(++count);}}
Server
public class MyServer {public static void main(String[] args) throws Exception{EventLoopGroup bossGroup = new NioEventLoopGroup(1);EventLoopGroup workerGroup = new NioEventLoopGroup();try {ServerBootstrap serverBootstrap = new ServerBootstrap();serverBootstrap.group(bossGroup,workerGroup).channel(NioServerSocketChannel.class).childHandler(new MyServerInitializer()); //自定义一个初始化类ChannelFuture channelFuture = serverBootstrap.bind(7000).sync();channelFuture.channel().closeFuture().sync();}finally {bossGroup.shutdownGracefully();workerGroup.shutdownGracefully();}}}

发送5次就分5次接收,不会存在粘包,拆包问题
