Netty之TCP粘包和拆包

基本介绍

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

由于TCP无消息保护边界, 需要在接收端处理消息边界问题,也就是我们所说的粘包、拆包问题, 看一张图:

粘包和拆包

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

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

现象案例

通过代码展示TCP粘包拆包现象。

客户端

image-20201201105853314

public class MyClient {













    public static void main(String[] args) throws InterruptedException {



        NioEventLoopGroup 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();

        }









    }



}



image-20201201105932772

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



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

image-20201201110318105

public class MyClientHandler extends SimpleChannelInboundHandler<ByteBuf> {












    private int count;






    @Override


    protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {

        byte[] buffer = new byte[msg.readableBytes()];

        msg.readBytes(buffer);



        //将buffer转成字符串

        String message = new String(buffer, Charset.forName("utf-8"));





        System.out.println("客户端接受到数据" + message);
        System.out.println("客户端接受到消息数量=" + (++this.count));




    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        //客户端发送10条数据hello,server编号
        for (int i = 0; i < 10; i++) {
            ByteBuf buffer = Unpooled.copiedBuffer(" hello,server " + i, Charset.forName("utf-8"));
            ctx.writeAndFlush(buffer);
        }
    }


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

服务器

image-20201201110426850

public class MyServer {













    public static void main(String[] args) throws InterruptedException {



        NioEventLoopGroup boosGroup = new NioEventLoopGroup(1);

        NioEventLoopGroup workerGroup = new NioEventLoopGroup(8);





        try {

            ServerBootstrap bootstrap = new ServerBootstrap();

            bootstrap.group(boosGroup, workerGroup)

                    .channel(NioServerSocketChannel.class)

                    .childHandler(new MyServerInitializer());





            ChannelFuture channelFuture = bootstrap.bind(7000).sync();






            channelFuture.channel().closeFuture().sync();







        } finally {



            boosGroup.shutdownGracefully();

            workerGroup.shutdownGracefully();

        }







    }



}



image-20201201110502127

public class MyServerInitializer extends ChannelInitializer<SocketChannel> {













    @Override



    protected void initChannel(SocketChannel ch) throws Exception {

        ChannelPipeline pipeline = ch.pipeline();





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

image-20201201110638962

public class MyServerHandler extends SimpleChannelInboundHandler<ByteBuf> {












    private int count;






    @Override


    protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {

        byte[] buffer = new byte[msg.readableBytes()];

        msg.readBytes(buffer);



        //将buffer转成字符串

        String message = new String(buffer, Charset.forName("utf-8"));





        System.out.println("服务器接受到数据" + message);
        System.out.println("服务器接受到消息数量=" + (++this.count));




        //服务器回送随机数据给客户端
        ByteBuf responseByteBuf = Unpooled.copiedBuffer(UUID.randomUUID().toString()+" ", Charset.forName("utf-8"));
        ctx.writeAndFlush(responseByteBuf);

    }






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

}

测试

启动一个服务器,和多个客户端。

image-20201201111717130

image-20201201111750800

image-20201201111849979

这里这个案例并没有出现拆包现象。出现的话应该会出现乱码。不过应该可以理解粘包和拆包现象了。

解决方案

  1. 使用自定义协议 + 编解码器 来解决

  2. 关键就是要解决 服务器端每次读取数据长度的问题, 这个问题解决,就不会出现服务器多读或少读数据的问题,从而避免的TCP 粘包、拆包 。

看一个具体的实例

  1. 要求客户端发送 5 个 Message 对象, 客户端每次发送一个 Message 对象
  2. 服务器端每次接收一个Message, 分5次进行解码, 每读取到 一个Message , 会回复一个Message 对象 给客户端。

image-20201201123141884

协议包

image-20201201123349480

//协议包
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;
    }


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


    public MessageProtocol() {
    }

}

客户端

image-20201201125139562

public class MyClient {













    public static void main(String[] args) throws InterruptedException {



        NioEventLoopGroup 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();

        }









    }



}



image-20201201125223531

public class MyClientInitializer extends ChannelInitializer<SocketChannel> {
    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
        //编码
        pipeline.addLast(new MyMessageEncoder());
        //解码
        pipeline.addLast(new MyMessageDecoder());
        pipeline.addLast(new MyClientHandler());
    }
}

image-20201201125558204

public class MyClientHandler extends SimpleChannelInboundHandler<MessageProtocol> {












    private int count;






    @Override


    protected void channelRead0(ChannelHandlerContext ctx, MessageProtocol msg) throws Exception {
        int len = msg.getLen();
        byte[] content = msg.getContent();
        System.out.println("客户端接收到消息如下");
        System.out.println("长度=" + len);
        System.out.println("内容=" + new String(content, Charset.forName("utf-8")));
        System.out.println("客户端接收消息数量=" + (++this.count));
    }






    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        //客户端发送10条数据 今天天气冷,吃火锅 编号
        for (int i = 0; i < 5; i++) {
            String msg = "今天天气冷,吃火锅";
            byte[] content = msg.getBytes(Charset.forName("utf-8"));
            int length = content.length;


            //创建协议包对象
            MessageProtocol messageProtocol = new MessageProtocol();
            messageProtocol.setLen(length);
            messageProtocol.setContent(content);
            ctx.writeAndFlush(messageProtocol);
        }
    }


    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        System.out.println("异常信息:" + cause.getMessage());
        ctx.close();
    }
}

编码器

image-20201201130216017

public class MyMessageEncoder extends MessageToByteEncoder<MessageProtocol> {












    @Override



    protected void encode(ChannelHandlerContext ctx, MessageProtocol msg, ByteBuf out) throws Exception {
        System.out.println("MyMessageEncoder encode 方法被调用");
        out.writeInt(msg.getLen());
        out.writeBytes(msg.getContent());


    }

}

解码器

image-20201201131114093

public class MyMessageDecoder extends ReplayingDecoder<Void> {












    @Override



    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
        System.out.println("MyMessageDecoder decode 被调用");
        //需要将得到的二进制字节码--->MessageProtocol数据包(对象)
        int length = in.readInt();


        byte[] content = new byte[length];
        in.readBytes(content);



        //封装成MessageProtocol对象,放入out,传递下一个handler业务处理
        MessageProtocol messageProtocol = new MessageProtocol();
        messageProtocol.setLen(length);
        messageProtocol.setContent(content);






        out.add(messageProtocol);
    }
}

服务器

image-20201201131151937

public class MyServer {













    public static void main(String[] args) throws InterruptedException {



        NioEventLoopGroup boosGroup = new NioEventLoopGroup(1);

        NioEventLoopGroup workerGroup = new NioEventLoopGroup(8);





        try {

            ServerBootstrap bootstrap = new ServerBootstrap();

            bootstrap.group(boosGroup, workerGroup)

                    .channel(NioServerSocketChannel.class)

                    .childHandler(new MyServerInitializer());





            ChannelFuture channelFuture = bootstrap.bind(7000).sync();






            channelFuture.channel().closeFuture().sync();







        } finally {



            boosGroup.shutdownGracefully();

            workerGroup.shutdownGracefully();

        }







    }



}



image-20201201131233205

public class MyServerInitializer extends ChannelInitializer<SocketChannel> {













    @Override



    protected void initChannel(SocketChannel ch) throws Exception {

        ChannelPipeline pipeline = ch.pipeline();

        //解码
        pipeline.addLast(new MyMessageDecoder());
        //编码
        pipeline.addLast(new MyMessageEncoder());
        pipeline.addLast(new MyServerHandler());
    }
}

image-20201201131352967

public class MyServerHandler extends SimpleChannelInboundHandler<MessageProtocol> {












    private int count;




    @Override
    protected void channelRead0(ChannelHandlerContext ctx, MessageProtocol msg) throws Exception {
        //接收到数据,并处理
        int len = msg.getLen();
        byte[] content = msg.getContent();


        System.out.println("服务器接收到消息如下");
        System.out.println("长度=" + len);
        System.out.println("内容=" + new String(content, Charset.forName("utf-8")));





        System.out.println("服务器接收到消息包数量=" + (++this.count));






        //回复消息
        String responseContent = UUID.randomUUID().toString();//字符串信息
        int length = responseContent.getBytes("utf-8").length;//字节数组长度
        byte[] responseContentBytes = responseContent.getBytes("utf-8");//字节数组信息






        //构建一个协议包
        MessageProtocol messageProtocol = new MessageProtocol();
        messageProtocol.setLen(length);
        messageProtocol.setContent(responseContentBytes);


        ctx.writeAndFlush(messageProtocol);
    }

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

测试

启动服务器,和客户端。

image-20201201131659221

image-20201201132057307

成功解决~~

© 版权声明
THE END
喜欢就支持一下吧
点赞0

Warning: mysqli_query(): (HY000/3): Error writing file '/tmp/MYdc3xtG' (Errcode: 28 - No space left on device) in /www/wwwroot/583.cn/wp-includes/class-wpdb.php on line 2345
admin的头像-五八三
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

图形验证码
取消
昵称代码图片