Netty进阶——粘包与半包(代码示例)
创始人
2024-03-02 07:42:20

目录

    • 一、消息粘包和消息半包的概述
      • 1.1、消息粘包
      • 1.2、消息半包
    • 二、粘包现象代码示例
      • 2.1、粘包现象服务端示例代码
      • 2.2、粘包现象客户端示例代码
      • 2.3、分别启动服务端,客户端,查看服务端结果输出
    • 三、半包现象代码示例
      • 3.1、半包现象服务端示例代码
      • 3.2、半包现象客户端示例代码
      • 3.3、分别启动服务端,客户端,查看服务端结果输出

一、消息粘包和消息半包的概述

1.1、消息粘包

  • 当缓冲区足够大,由于网络不稳定种种原因,可能会有多条消息从通道读入缓冲区,此时如果无法分清数据包之间的界限,就会导致粘包问题。

1.2、消息半包

  • 若消息没有接收完,缓冲区就被填满了,会导致从缓冲区取出的消息不完整,即半包的现象。

二、粘包现象代码示例

2.1、粘包现象服务端示例代码

  • 服务端代码

    package com.example.nettytest.netty.day5;import io.netty.bootstrap.ServerBootstrap;
    import io.netty.channel.ChannelFuture;
    import io.netty.channel.ChannelHandlerContext;
    import io.netty.channel.ChannelInboundHandlerAdapter;
    import io.netty.channel.ChannelInitializer;
    import io.netty.channel.nio.NioEventLoopGroup;
    import io.netty.channel.socket.SocketChannel;
    import io.netty.channel.socket.nio.NioServerSocketChannel;
    import io.netty.handler.logging.LogLevel;
    import io.netty.handler.logging.LoggingHandler;
    import lombok.extern.slf4j.Slf4j;/*** @description:   Netty粘包现象演示服务端*                 消息粘包:当缓冲区足够大,由于网络不稳定种种原因,可能会有多条消息从通道读入缓冲区,*                         此时如果无法分清数据包之间的界限,就会导致粘包问题;* @author: xz*/
    @Slf4j
    public class NettyServerTest {public static void main(String[] args) {new NettyServerTest().start();}void start() {NioEventLoopGroup boss = new NioEventLoopGroup(1);NioEventLoopGroup worker = new NioEventLoopGroup();try {ServerBootstrap serverBootstrap = new ServerBootstrap().channel(NioServerSocketChannel.class).group(boss, worker).childHandler(new ChannelInitializer() {@Overrideprotected void initChannel(SocketChannel ch) throws Exception {ch.pipeline().addLast(new LoggingHandler(LogLevel.DEBUG));ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {//会在连接channel建立成功后,触发active事件@Overridepublic void channelActive(ChannelHandlerContext ctx) throws Exception {log.debug("connected>>>>>>>>>>>>>>>> {}", ctx.channel());super.channelActive(ctx);}@Overridepublic void channelInactive(ChannelHandlerContext ctx) throws Exception {log.debug("disconnect>>>>>>>>>>>>>>>> {}", ctx.channel());super.channelInactive(ctx);}});}});ChannelFuture channelFuture = serverBootstrap.bind(8080);log.debug("{}>>>>>>>>>>>>>>>> binding...", channelFuture.channel());channelFuture.sync();log.debug("{}>>>>>>>>>>>>>>>> bound...", channelFuture.channel());channelFuture.channel().closeFuture().sync();} catch (InterruptedException e) {log.error("server error", e);} finally {boss.shutdownGracefully();worker.shutdownGracefully();log.debug(">>>>>>>>>>>>>>>>stoped");}}
    }
    

2.2、粘包现象客户端示例代码

  • 客户端代码示例

    package com.example.nettytest.netty.day5;import io.netty.bootstrap.Bootstrap;
    import io.netty.buffer.ByteBuf;
    import io.netty.channel.ChannelFuture;
    import io.netty.channel.ChannelHandlerContext;
    import io.netty.channel.ChannelInboundHandlerAdapter;
    import io.netty.channel.ChannelInitializer;
    import io.netty.channel.nio.NioEventLoopGroup;
    import io.netty.channel.socket.SocketChannel;
    import io.netty.channel.socket.nio.NioSocketChannel;
    import lombok.extern.slf4j.Slf4j;
    /*** @description: Netty粘包现象演示客户端* @author: xz*/
    @Slf4j
    public class NettyClientTest {public static void main(String[] args) {new NettyClientTest().start();}void start() {NioEventLoopGroup worker = new NioEventLoopGroup();try {Bootstrap bootstrap = new Bootstrap().channel(NioSocketChannel.class).group(worker).handler(new ChannelInitializer() {@Overrideprotected void initChannel(SocketChannel ch) throws Exception {log.debug("connetted》》》》》》》》》》》》》》》");ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {//会在连接channel建立成功后,触发active事件@Overridepublic void channelActive(ChannelHandlerContext ctx) throws Exception {log.debug("遍历 sending 每次发送16个字节》》》》》》》》》》》》》》》");for (int i = 0; i < 10; i++) {//设置缓冲区大小16个字节ByteBuf buffer = ctx.alloc().buffer(16);buffer.writeBytes(new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16});ctx.writeAndFlush(buffer);}}});}});ChannelFuture channelFuture = bootstrap.connect("127.0.0.1", 8080).sync();channelFuture.channel().closeFuture().sync();} catch (InterruptedException e) {log.error("client error", e);} finally {worker.shutdownGracefully();}}
    }
    

2.3、分别启动服务端,客户端,查看服务端结果输出

  • 先启动服务端
    在这里插入图片描述
  • 再启动客户端
    在这里插入图片描述
  • 再次查看服务端
    注:可下图输出结果可知:服务器端的某次输出,可以看到一次就接收了 160 个字节,而非分 10 次接收。在这里插入图片描述

三、半包现象代码示例

3.1、半包现象服务端示例代码

  • 为现象明显,服务端添加一下接收缓冲区,其它代码不变

    serverBootstrap.option(ChannelOption.SO_RCVBUF, 10);
    
  • 服务端完整示例代码

    package com.example.nettytest.netty.day5;import io.netty.bootstrap.ServerBootstrap;
    import io.netty.channel.*;
    import io.netty.channel.nio.NioEventLoopGroup;
    import io.netty.channel.socket.SocketChannel;
    import io.netty.channel.socket.nio.NioServerSocketChannel;
    import io.netty.handler.logging.LogLevel;
    import io.netty.handler.logging.LoggingHandler;
    import lombok.extern.slf4j.Slf4j;
    /*** @description:   Netty半包现象演示服务端*                 消息半包:若消息没有接收完,缓冲区就被填满了,会导致从缓冲区取出的消息不完整,即半包的现象。* @author: xz*/
    @Slf4j
    public class NettyServerTest1 {public static void main(String[] args) {new NettyServerTest().start();}void start() {NioEventLoopGroup boss = new NioEventLoopGroup(1);NioEventLoopGroup worker = new NioEventLoopGroup();try {ServerBootstrap serverBootstrap = new ServerBootstrap().channel(NioServerSocketChannel.class)//服务端添加接收缓冲区,大小设置10.option(ChannelOption.SO_RCVBUF, 10).group(boss, worker).childHandler(new ChannelInitializer() {@Overrideprotected void initChannel(SocketChannel ch) throws Exception {ch.pipeline().addLast(new LoggingHandler(LogLevel.DEBUG));ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {@Overridepublic void channelActive(ChannelHandlerContext ctx) throws Exception {log.debug("connected================== {}", ctx.channel());super.channelActive(ctx);}@Overridepublic void channelInactive(ChannelHandlerContext ctx) throws Exception {log.debug("disconnect================== {}", ctx.channel());super.channelInactive(ctx);}});}});ChannelFuture channelFuture = serverBootstrap.bind(8080);log.debug("{} binding====================", channelFuture.channel());channelFuture.sync();log.debug("{} bound====================", channelFuture.channel());channelFuture.channel().closeFuture().sync();} catch (InterruptedException e) {log.error("server error", e);} finally {boss.shutdownGracefully();worker.shutdownGracefully();log.debug("stoped======================");}}
    }
    

3.2、半包现象客户端示例代码

  • 客户端代码示例无变化,完整代码如下

    package com.example.nettytest.netty.day5;import io.netty.bootstrap.Bootstrap;
    import io.netty.buffer.ByteBuf;
    import io.netty.channel.ChannelFuture;
    import io.netty.channel.ChannelHandlerContext;
    import io.netty.channel.ChannelInboundHandlerAdapter;
    import io.netty.channel.ChannelInitializer;
    import io.netty.channel.nio.NioEventLoopGroup;
    import io.netty.channel.socket.SocketChannel;
    import io.netty.channel.socket.nio.NioSocketChannel;
    import lombok.extern.slf4j.Slf4j;
    /*** @description: Netty半包现象演示客户端* @author: xz*/
    @Slf4j
    public class NettyClientTest1 {public static void main(String[] args) {new NettyClientTest1().start();}void start() {NioEventLoopGroup worker = new NioEventLoopGroup();try {Bootstrap bootstrap = new Bootstrap().channel(NioSocketChannel.class).group(worker).handler(new ChannelInitializer() {@Overrideprotected void initChannel(SocketChannel ch) throws Exception {log.debug("connetted----------------");ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {//会在连接channel建立成功后,触发active事件@Overridepublic void channelActive(ChannelHandlerContext ctx) throws Exception {log.debug("sending----------------");for (int i = 0; i < 10; i++) {ByteBuf buffer = ctx.alloc().buffer(16);buffer.writeBytes(new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16});ctx.writeAndFlush(buffer);}}});}});ChannelFuture channelFuture = bootstrap.connect("127.0.0.1", 8080).sync();channelFuture.channel().closeFuture().sync();} catch (InterruptedException e) {log.error("client error", e);} finally {worker.shutdownGracefully();}}
    }
    

3.3、分别启动服务端,客户端,查看服务端结果输出

  • 先启动服务端
    在这里插入图片描述
  • 再启动客户端
    在这里插入图片描述
  • 再次查看服务端
    注:可下图输出结果可知:服务器端的某次输出,可以看到接收的消息被分为两节,第一次 16 字节,第二次 32 字节,第三次 64 字节,第四次 48 字节
    在这里插入图片描述
    在这里插入图片描述

相关内容

热门资讯

北京的名胜古迹 北京最著名的景... 北京从元代开始,逐渐走上帝国首都的道路,先是成为大辽朝五大首都之一的南京城,随着金灭辽,金代从海陵王...
埃菲尔铁塔在哪 中国仿建埃菲尔... 2019年4月26日,广西南宁市,街头惊现一座巨型山寨版埃菲尔铁塔,高约20米,白色塔身,造型逼真,...
苗族的传统节日 贵州苗族节日有... 【岜沙苗族芦笙节】岜沙,苗语叫“分送”,距从江县城7.5公里,是世界上最崇拜树木并以树为神的枪手部落...
长白山自助游攻略 吉林长白山游... 昨天介绍了西坡的景点详细请看链接:一个人的旅行,据说能看到长白山天池全凭运气,您的运气如何?今日介绍...
猫咪吃了塑料袋怎么办 猫咪误食... 你知道吗?塑料袋放久了会长猫哦!要说猫咪对塑料袋的喜爱程度完完全全可以媲美纸箱家里只要一有塑料袋的响...
应用未安装解决办法 平板应用未... ---IT小技术,每天Get一个小技能!一、前言描述苹果IPad2居然不能安装怎么办?与此IPad不...
脚上的穴位图 脚面经络图对应的... 人体穴位作用图解大全更清晰直观的标注了各个人体穴位的作用,包括头部穴位图、胸部穴位图、背部穴位图、胳...
世界上最漂亮的人 世界上最漂亮... 此前在某网上,选出了全球265万颜值姣好的女性。从这些数量庞大的女性群体中,人们投票选出了心目中最美...
demo什么意思 demo版本... 618快到了,各位的小金库大概也在准备开闸放水了吧。没有小金库的,也该向老婆撒娇卖萌服个软了,一切只...
埃菲尔铁塔在哪 中国仿建埃菲尔... 2019年4月26日,广西南宁市,街头惊现一座巨型山寨版埃菲尔铁塔,高约20米,白色塔身,造型逼真,...
苗族的传统节日 贵州苗族节日有... 【岜沙苗族芦笙节】岜沙,苗语叫“分送”,距从江县城7.5公里,是世界上最崇拜树木并以树为神的枪手部落...
北京的名胜古迹 北京最著名的景... 北京从元代开始,逐渐走上帝国首都的道路,先是成为大辽朝五大首都之一的南京城,随着金灭辽,金代从海陵王...
长白山自助游攻略 吉林长白山游... 昨天介绍了西坡的景点详细请看链接:一个人的旅行,据说能看到长白山天池全凭运气,您的运气如何?今日介绍...