- 主页 > 生活百科 > >
详解什么是 TCP 粘包和拆包现象并演示 Netty 是如何解决的( 四 )
- 服务器端 NettyServerHandler 改动如下
@Overrideprotected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) {byte[] buffer = new byte[msg.readableBytes()];msg.readBytes(buffer);//将buffer转为字符串String message = new String(buffer, StandardCharsets.UTF_8);System.out.println("服务器收到的数据=" + message);System.out.println("服务器收到的数据次数=" + (++this.count));//服务器回送数据给客户端 回送一个随机idString replyData = https://www.isolves.com/it/wl/zs/2022-04-24/UUID.randomUUID().toString();ByteBuf buffer1 = Unpooled.copiedBuffer(replyData.concat(NettyServer.DELIMITER), StandardCharsets.UTF_8);ctx.writeAndFlush(buffer1);System.out.println("服务器回复数据=" + replyData);} - 客户端 NettyClient 改动如下
public static void main(String[] args) throws Exception {// 初始化客户端事件组EventLoopGroup group = new NioEventLoopGroup();try {Bootstrap b = new Bootstrap();b.group(group)// 初始化通道.channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true).handler(new ChannelInitializer<SocketChannel>() {// 初始化通道处理器@Overridepublic void initChannel(SocketChannel ch) {ChannelPipeline p = ch.pipeline();// 使用分隔符"$_"的半包解码器ByteBuf byteBuf = Unpooled.copiedBuffer(DELIMITER.getBytes());ch.pipeline().addLast(new DelimiterBasedFrameDecoder(1024, byteBuf));p.addLast(new NettyClientHandler());}});b.connect(HOST, PORT).addListener(future -> {log.info(String.format("连接服务器端:%s:%s 成功!", HOST, PORT));}).await();} catch (Exception e) {log.error("启动客户端出现异常", e);}} - 客户端 NettyClientHandler 中接收数据的部分不变 , 发送数据的地方改动如下
// 重写 channelActive, 当客户端启动的时候 自动发送数据给服务端@Overridepublic void channelActive(ChannelHandlerContext ctx) throws Exception {// tcp 粘包现象// 使用客户端分10次发送 , 每次数据量少// tcp 会等客户端多次生产后 , 一次性进行发送for (int i = 0; i < 10; i++) {String data = https://www.isolves.com/it/wl/zs/2022-04-24/"ckjava" + i;log.info(String.format("客户端发送消息:[%s]", data));ByteBuf buffer = Unpooled.copiedBuffer(data.concat(NettyClient.DELIMITER), StandardCharsets.UTF_8);ctx.writeAndFlush(buffer);}} - 服务器端输出如下
18:14:33.627 [nioEventLoopGroup-2-1] INFOcom.ckjava.test.server.NettyServer - 服务器端启动成功 , 开放端口:8080服务器收到的数据=ckjava0服务器收到的数据次数=1服务器回复数据=c6129b89-c869-4e06-97ca-55518c55aff7服务器收到的数据=ckjava1服务器收到的数据次数=2服务器回复数据=bc3426cb-072f-4cb9-9f69-d2797863c9e4服务器收到的数据=ckjava2服务器收到的数据次数=3服务器回复数据=43790702-1978-462b-a865-15c0ff2803af服务器收到的数据=ckjava3服务器收到的数据次数=4服务器回复数据=4eb3e4e6-0c6a-4cef-a639-d6c40ebc27d2服务器收到的数据=ckjava4服务器收到的数据次数=5服务器回复数据=6a9f02f9-9e0d-4eae-a380-605c3ba410d2服务器收到的数据=ckjava5服务器收到的数据次数=6服务器回复数据=7ab9e20e-a86b-4f68-8673-5bc024643274服务器收到的数据=ckjava6服务器收到的数据次数=7服务器回复数据=3b6b68cf-c066-4e32-8b5a-961c995fdd6d服务器收到的数据=ckjava7服务器收到的数据次数=8服务器回复数据=cf2a5c51-96d9-4309-8f05-1c09abbe04f2服务器收到的数据=ckjava8服务器收到的数据次数=9服务器回复数据=4d586684-be55-4c10-8071-a88dad5f0684服务器收到的数据=ckjava9服务器收到的数据次数=10服务器回复数据=22fd511e-e65a-4f10-9426-f14b4524d4d0 - 客户端输出如下
18:14:50.056 [nioEventLoopGroup-2-1] INFOcom.ckjava.test.client.NettyClient - 连接服务器端:127.0.0.1:8080 成功!18:14:50.058 [nioEventLoopGroup-2-1] INFOc.c.test.client.NettyClientHandler - 客户端发送消息:[ckjava0]18:14:50.075 [nioEventLoopGroup-2-1] INFOc.c.test.client.NettyClientHandler - 客户端发送消息:[ckjava1]18:14:50.076 [nioEventLoopGroup-2-1] INFOc.c.test.client.NettyClientHandler - 客户端发送消息:[ckjava2]18:14:50.076 [nioEventLoopGroup-2-1] INFOc.c.test.client.NettyClientHandler - 客户端发送消息:[ckjava3]18:14:50.076 [nioEventLoopGroup-2-1] INFOc.c.test.client.NettyClientHandler - 客户端发送消息:[ckjava4]18:14:50.076 [nioEventLoopGroup-2-1] INFOc.c.test.client.NettyClientHandler - 客户端发送消息:[ckjava5]18:14:50.076 [nioEventLoopGroup-2-1] INFOc.c.test.client.NettyClientHandler - 客户端发送消息:[ckjava6]18:14:50.077 [nioEventLoopGroup-2-1] INFOc.c.test.client.NettyClientHandler - 客户端发送消息:[ckjava7]18:14:50.077 [nioEventLoopGroup-2-1] INFOc.c.test.client.NettyClientHandler - 客户端发送消息:[ckjava8]18:14:50.077 [nioEventLoopGroup-2-1] INFOc.c.test.client.NettyClientHandler - 客户端发送消息:[ckjava9]18:14:50.104 [nioEventLoopGroup-2-1] INFOc.c.test.client.NettyClientHandler - 客户端接收到消息:[c6129b89-c869-4e06-97ca-55518c55aff7]18:14:50.105 [nioEventLoopGroup-2-1] INFOc.c.test.client.NettyClientHandler - 客户端接收到消息的次数:118:14:50.105 [nioEventLoopGroup-2-1] INFOc.c.test.client.NettyClientHandler - ---------------------------------------------------18:14:50.105 [nioEventLoopGroup-2-1] INFOc.c.test.client.NettyClientHandler - 客户端接收到消息:[bc3426cb-072f-4cb9-9f69-d2797863c9e4]18:14:50.105 [nioEventLoopGroup-2-1] INFOc.c.test.client.NettyClientHandler - 客户端接收到消息的次数:218:14:50.105 [nioEventLoopGroup-2-1] INFOc.c.test.client.NettyClientHandler - ---------------------------------------------------18:14:50.106 [nioEventLoopGroup-2-1] INFOc.c.test.client.NettyClientHandler - 客户端接收到消息:[43790702-1978-462b-a865-15c0ff2803af]18:14:50.106 [nioEventLoopGroup-2-1] INFOc.c.test.client.NettyClientHandler - 客户端接收到消息的次数:318:14:50.106 [nioEventLoopGroup-2-1] INFOc.c.test.client.NettyClientHandler - ---------------------------------------------------18:14:50.106 [nioEventLoopGroup-2-1] INFOc.c.test.client.NettyClientHandler - 客户端接收到消息:[4eb3e4e6-0c6a-4cef-a639-d6c40ebc27d2]18:14:50.106 [nioEventLoopGroup-2-1] INFOc.c.test.client.NettyClientHandler - 客户端接收到消息的次数:418:14:50.106 [nioEventLoopGroup-2-1] INFOc.c.test.client.NettyClientHandler - ---------------------------------------------------18:14:50.106 [nioEventLoopGroup-2-1] INFOc.c.test.client.NettyClientHandler - 客户端接收到消息:[6a9f02f9-9e0d-4eae-a380-605c3ba410d2]18:14:50.106 [nioEventLoopGroup-2-1] INFOc.c.test.client.NettyClientHandler - 客户端接收到消息的次数:518:14:50.106 [nioEventLoopGroup-2-1] INFOc.c.test.client.NettyClientHandler - ---------------------------------------------------18:14:50.106 [nioEventLoopGroup-2-1] INFOc.c.test.client.NettyClientHandler - 客户端接收到消息:[7ab9e20e-a86b-4f68-8673-5bc024643274]18:14:50.106 [nioEventLoopGroup-2-1] INFOc.c.test.client.NettyClientHandler - 客户端接收到消息的次数:618:14:50.106 [nioEventLoopGroup-2-1] INFOc.c.test.client.NettyClientHandler - ---------------------------------------------------18:14:50.107 [nioEventLoopGroup-2-1] INFOc.c.test.client.NettyClientHandler - 客户端接收到消息:[3b6b68cf-c066-4e32-8b5a-961c995fdd6d]18:14:50.107 [nioEventLoopGroup-2-1] INFOc.c.test.client.NettyClientHandler - 客户端接收到消息的次数:718:14:50.107 [nioEventLoopGroup-2-1] INFOc.c.test.client.NettyClientHandler - ---------------------------------------------------18:14:50.107 [nioEventLoopGroup-2-1] INFOc.c.test.client.NettyClientHandler - 客户端接收到消息:[cf2a5c51-96d9-4309-8f05-1c09abbe04f2]18:14:50.107 [nioEventLoopGroup-2-1] INFOc.c.test.client.NettyClientHandler - 客户端接收到消息的次数:818:14:50.107 [nioEventLoopGroup-2-1] INFOc.c.test.client.NettyClientHandler - ---------------------------------------------------18:14:50.107 [nioEventLoopGroup-2-1] INFOc.c.test.client.NettyClientHandler - 客户端接收到消息:[4d586684-be55-4c10-8071-a88dad5f0684]18:14:50.107 [nioEventLoopGroup-2-1] INFOc.c.test.client.NettyClientHandler - 客户端接收到消息的次数:918:14:50.107 [nioEventLoopGroup-2-1] INFOc.c.test.client.NettyClientHandler - ---------------------------------------------------18:14:50.107 [nioEventLoopGroup-2-1] INFOc.c.test.client.NettyClientHandler - 客户端接收到消息:[22fd511e-e65a-4f10-9426-f14b4524d4d0]18:14:50.107 [nioEventLoopGroup-2-1] INFOc.c.test.client.NettyClientHandler - 客户端接收到消息的次数:1018:14:50.107 [nioEventLoopGroup-2-1] INFOc.c.test.client.NettyClientHandler - ---------------------------------------------------
推荐阅读
-
-
穿搭|万茜穿“初恋裤”出门真幼稚,素颜搭配文艺眼镜,看着真显小
-
-
[难求]20斤才出1斤肉,抽干水塘一捡几百斤,以前没人吃,现在一斤难求
-
-
-
新华网|国务院发文:大力培育集成电路领域和软件领域企业
-
中新经纬|个人消费贷款违规流入资本市场 中信银行一分行被罚30万
-
-
#欧佳健康#最好去检查一下,癌症可能已经找上你,上厕所的时候出现四个信号
-
三星Galaxy|骁龙888手机卖6000块钱!老外嫌三星Galaxy S21 FE定价过高
-
新华网|7月12日7时至7月13日7时,内蒙古自治区报告新增境外输入确诊病例4例
-
「南方医传媒TB」学医催人老!科学家发现,医生DNA的衰老速度比普通人快了六倍
-
揭秘陈斌|给这些庶族出生的晋升的机会?,隋朝为什么开科举
-
英特尔Intel发布10nm++嵌入式奔腾/赛扬/凌动:4核心只要6.5W
-
港股挖掘机■天津港发展(03382):天津港焦炭码头财务人员涉嫌贪污公款的调查正在进行中
-
经济日报-中国经济网|落实政策助力,小蟹塘做大做强
-
-
-
云高高尔夫|【高尔夫揭秘】那些你不知道的高尔夫小秘密