- 主页 > 生活百科 > >
详解什么是 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 - ---------------------------------------------------
推荐阅读
-
-
上海市公积金管理中心|住房公积金离退休提取纳入企业职工退休“一件事”办理
-
气质|街拍美女小姐姐的清爽穿搭,展现出了灵动迷人的典雅气质
-
-
-
烹饪|美食分享:豆芽杏鲍菇炒肉丝、腐竹黄瓜炒木耳、醋溜包菜的做法
-
日产骐达|等新款高尔夫不如选它,1.6L引擎,比轩逸个性,顶配13.59万
-
『搞笑段子趣图』混进去多长时间了!该出来了!,GIF搞笑趣图:你的胆子还挺大
-
三体|TVB前花旦离巢后转战大荧幕!两年前男友求婚成功,或做兔年新娘
-
-
-
怪咖搞笑|于是我就试了试,幽默笑话:有人说喝洗脚水可以力大无穷
-
番茄|7月份,我家早餐这样吃,粗粮多,蔬菜多, 家人越吃身体越苗条了
-
章宇|高考名额被顶替,最后自杀又杀死亲生父亲,章宇到底经历了什么?
-
-
-
回响|《回响》大结局:好家伙,这是我今年所看,最离谱的大结局
-
展厅|故宫每日预约量上调至1.2万人 室内展厅重开
-
北青网综合|宁波这位老师趴在瑜伽球上工作的照片让人心疼!
-