- 主页 > 生活百科 > >
详解什么是 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 - ---------------------------------------------------
推荐阅读
-
以史为鉴|18岁击败彼得大帝,卡尔十二世统领的瑞典有多强?,15岁继承王位
-
丈夫崩溃!浙江数学女老师突然被迫离开讲台!医生:有人37岁就成了这样……
-
卡哇伊|财源滚滚来,接住!发发发!,7月中旬开门红!属虎人
-
-
-
国际足球|巴萨捡到宝了!17岁小将打入队史第9000球,本赛季已进6球!
-
游戏产业|电视机的春天来了,Xbox确认将推出xCloud串流棒,把其变成高性能主机
-
「出名」河南一个不出名的小镇,把钢卷尺卖到了全世界
-
-
苏宁易购发布2020半年报:线上销售规模大增20%
-
孕妇|这些你喜欢的零食都不能吃了?不一定,看完就明白了!怀孕时
-
找工作办了个假的证书,入职了公司,现在很后悔,离职还是坦白?
-
徐汇滨江|上海徐汇滨江亲水平台遭船只撞击 肇事船只已被控制
-
预告|《部落冲突》:你以为八周年就完了?不对,重磅活动还在后头
-
「华为」HMS又一重拳出击?谷歌追悔莫及,华为海外生态第一战干得漂亮!
-
-
|重磅官宣!福建不再与劳森签约 不当言论或让他彻底无缘CBA
-
-
-
国际足球|「足坛趣味问答100期答案」今晚公布51-100期榜单,前5名有奖品