TCP 粘包和拆包基本介绍

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

java netty udp 如何粘包_netty

假设客户端分别发送了两个数据包 D1 和 D2 给服务端,由于服务端一次读取到字节数是不确定的,故可能存在以 下四种情况

  1. 服务端分两次读取到了两个独立的数据包,分别是 D1 和 D2,没有粘包和拆包
  2. 服务端一次接受到了两个数据包,D1 和 D2 粘合在一起,称之为 TCP 粘包
  3. 服务端分两次读取到了数据包,第一次读取到了完整的 D1 包和 D2 包的部分内容,第二次读取到了 D2 包的剩余内容,这称之为 TCP 拆包
  4. 服务端分两次读取到了数据包,第一次读取到了 D1 包的部分内容 D1_1,第二次读取到了 D1 包的剩余部 分内容 D1_2 和完整的 D2 包

TCP 粘包和拆包现象实例

server
public class Server {
    public static void main(String[] args) {
        EventLoopGroup bossGroup=new NioEventLoopGroup();
        EventLoopGroup workerGroup=new NioEventLoopGroup();

        try {
            ServerBootstrap serverBootstrap=new ServerBootstrap();

            serverBootstrap.group(bossGroup,workerGroup)
                .channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_BACKLOG,128)
                .childOption(ChannelOption.SO_KEEPALIVE,true)
                .childHandler(new ServerInitializer());
            ChannelFuture channelFuture = serverBootstrap.bind(new InetSocketAddress(50000)).sync();

            channelFuture.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }
}



serverInitializer
public class ServerInitializer extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel sc) throws Exception {
        ChannelPipeline pipeline = sc.pipeline();
        pipeline.addLast(new ServerHandler());
    }
}



serverHandler
public class ServerHandler extends SimpleChannelInboundHandler<ByteBuf> {

    private int count=0;

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
        System.out.println("服务器接受到的数据:"+msg.toString(CharsetUtil.UTF_8));
        System.out.println("服务器接受到的信息量:"+(++this.count));

        //回送数据,返回一个UUID
        String id= UUID.randomUUID().toString();
        ctx.writeAndFlush(Unpooled.copiedBuffer(id+"\n",CharsetUtil.UTF_8));
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}



client
public class Client {
    public static void main(String[] args) {
        EventLoopGroup group=new NioEventLoopGroup();

        try {
            Bootstrap bootstrap=new Bootstrap();

            bootstrap.group(group)
                .channel(NioSocketChannel.class)
                .handler(new ClientInitializer());

            ChannelFuture channelFuture = bootstrap.connect(new InetSocketAddress("127.0.0.1", 50000)).sync();
            channelFuture.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            group.shutdownGracefully();
        }
    }
}



clientInitializer
public class ClientInitializer extends ChannelInitializer<SocketChannel> {
    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();

        pipeline.addLast(new ClientHandler());
    }
}



clientHandler
public class ClientHandler extends SimpleChannelInboundHandler<ByteBuf> {

    private int count=0;

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        //客户端发送10条数据
        for(int i=0;i<10;++i){
            ctx.writeAndFlush(Unpooled.copiedBuffer(("hello server "+i+"\t"), CharsetUtil.UTF_8));
        }
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
        System.out.println("客户端接受到服务器的信息:"+msg.toString(CharsetUtil.UTF_8));
        System.out.println("客户端接收到的信息量:"+(++this.count));
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}



运行

 服务器

java netty udp 如何粘包_java netty udp 如何粘包_02

 客户端

java netty udp 如何粘包_网络_03




TCP 粘包和拆包解决方案

  1. 使用自定义协议 + 编解码器 来解决
  2. 关键就是要解决 服务器端每次读取数据长度的问题, 这个问题解决,就不会出现服务器多读或少读数据的问 题,从而避免的 TCP 粘包、拆包


messageProtocol
public class MessageProtocol {
    private int len;
    private byte[] content;

    public MessageProtocol() {
    }



    public MessageProtocol(byte[] content, int len) {
        this.content=content;
        this.len=len;
    }

    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;
    }
}



messageEncoder
public class MessageEncoder extends MessageToByteEncoder<MessageProtocol> {
    @Override
    protected void encode(ChannelHandlerContext ctx, MessageProtocol msg, ByteBuf out) throws Exception {
        System.out.println("MessageEncoder encode 方法被调用\n");
        out.writeInt(msg.getLen());
        out.writeBytes(msg.getContent());
    }
}



messageDecoder
public class MessageDecoder extends ReplayingDecoder<Void> {
    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
        System.out.println("MessageDecoder decode 被调用");
        //将得到的二进制字节码->MessageProtocol数据包(对象)
        int len=in.readInt();

        byte[] content=new byte[len];
        in.readBytes(content);
        out.add(new MessageProtocol(content,len));
    }
}



server
public class Server {
    public static void main(String[] args) {
        EventLoopGroup bossGroup=new NioEventLoopGroup();
        EventLoopGroup workerGroup=new NioEventLoopGroup();

        try {
            ServerBootstrap serverBootstrap=new ServerBootstrap();

            serverBootstrap.group(bossGroup,workerGroup)
                .channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_BACKLOG,128)
                .childOption(ChannelOption.SO_KEEPALIVE,true)
                .childHandler(new ServerInitializer());
            ChannelFuture channelFuture = serverBootstrap.bind(new InetSocketAddress(50000)).sync();

            channelFuture.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }
}



serverInitializer
public class ServerInitializer extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel sc) throws Exception {
        ChannelPipeline pipeline = sc.pipeline();

        pipeline.addLast(new MessageEncoder());
        pipeline.addLast(new MessageDecoder());
        pipeline.addLast(new ServerHandler());
    }
}



serverHandler
public class ServerHandler extends SimpleChannelInboundHandler<MessageProtocol> {

    private int count=0;

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, MessageProtocol msg) throws Exception {
        System.out.println("服务器接受到的数据:"+new String(msg.getContent(),CharsetUtil.UTF_8)
                           +"\t数据长度"+msg.getLen());

        System.out.println("服务器接受到的信息量:"+(++this.count));

        //回送数据,返回一个UUID
        String id= UUID.randomUUID().toString();
        ctx.writeAndFlush(new MessageProtocol(id.getBytes(),id.getBytes().length));
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}



client
public class Client {
    public static void main(String[] args) {
        EventLoopGroup group=new NioEventLoopGroup();

        try {
            Bootstrap bootstrap=new Bootstrap();

            bootstrap.group(group)
                .channel(NioSocketChannel.class)
                .handler(new ClientInitializer());

            ChannelFuture channelFuture = bootstrap.connect(new InetSocketAddress("127.0.0.1", 50000)).sync();
            channelFuture.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            group.shutdownGracefully();
        }
    }
}



clientInitializer
public class ClientInitializer extends ChannelInitializer<SocketChannel> {
    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();

        pipeline.addLast(new MessageEncoder());
        pipeline.addLast(new MessageDecoder());
        pipeline.addLast(new ClientHandler());
    }
}



clientHandler
public class ClientHandler extends SimpleChannelInboundHandler<MessageProtocol> {

    private int count=0;

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        //客户端发送10条数据

        for(int i=0;i<5;++i){
            byte[] content = ("hello server " + i + "\t").getBytes(CharsetUtil.UTF_8);
            MessageProtocol messageProtocol = new MessageProtocol(content,content.length);
            System.out.println(messageProtocol.getLen()+" "+new String(messageProtocol.getContent(),CharsetUtil.UTF_8));
            ctx.writeAndFlush(messageProtocol);
        }
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, MessageProtocol msg) throws Exception {
        System.out.println("客户端接受到服务器的信息:"+new String(msg.getContent(),CharsetUtil.UTF_8)
                           +"\t数据长度"+msg.getLen());
        System.out.println("客户端接收到的信息量:"+(++this.count));
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}