- 主页 > 生活百科 > >
详解什么是 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 - ---------------------------------------------------
推荐阅读
-
|中方将采取反制措施!,特朗普签署涉港法规制裁中国,外交部:废纸一张
-
-
青年|王者荣耀: 天蓬元帅必出的法师装备, 后期每秒千血, 大神最后补!
-
-
-
海底探探|叫“上短下短”,时髦又保暖,初冬穿恰好,最近火了一种穿衣法则
-
新华网|江苏通报2起违法违规“小化工”背后官商勾结典型案例
-
-
一米一饭|关羽仅排第4,前2名太强了,这才是真实三国武将排名
-
冬季用车注意事项课程5:暖风系统的正确用法&异味解决方式
-
-
“果郡王”李东学不红,是因为他正在“另一个圈子”里,红得发紫
-
电竞|梦幻西游:梧桐逆天改命!1个月连炸5件无级别神器,这波又赚台车
-
足球部落|却正中兰帕德下怀,尤文与切尔西谈等价互换!萨里用弃子换来爱徒
-
「薛之谦」薛之谦郁可唯新歌发布,一首《纸船》,唱到你心坎里
-
谈军论事|美大选对海外美军有何影响?专家:特朗普不知如何撤军才不像夹着尾巴逃跑
-
扬眼|遭专业乐迷质疑不够“地下”,白举纲:狭隘的定义不能代表摇滚
-
国内知名医美平台更美APP,受邀加入移动应用程序安全委员会
-
羽绒服上圆珠笔印怎么去除 羽绒服上圆珠笔印怎么去除掉
-
穿搭|海清真是放得开,这么低的抹胸裙也敢直接穿出来,身材丰满真撩人