详解什么是 TCP 粘包和拆包现象并演示 Netty 是如何解决的( 四 )

  1. 服务器端 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);}
  1. 客户端 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);}}
  1. 客户端 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);}}
  1. 服务器端输出如下
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
  1. 客户端输出如下
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 - ---------------------------------------------------


推荐阅读