From 26acd1c7c6290b340caa25774f57c790378a2b4b Mon Sep 17 00:00:00 2001 From: Bunny <1319900154@qq.com> Date: Sat, 25 May 2024 00:47:29 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E6=96=B0=E5=A2=9E):=20:rocket:=20netty?= =?UTF-8?q?=E7=9A=84=20=E5=8C=85=E7=9A=84=E5=8F=91=E9=80=81=EF=BC=8C?= =?UTF-8?q?=E5=88=8610=E6=AC=A1=E5=8F=91=E9=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/bunny/service/netty/PackageClient.java | 21 ++++++++++++++----- .../cn/bunny/service/netty/PackageServer.java | 5 +++-- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/netty/service/src/main/java/cn/bunny/service/netty/PackageClient.java b/netty/service/src/main/java/cn/bunny/service/netty/PackageClient.java index b7c762c..781e97c 100644 --- a/netty/service/src/main/java/cn/bunny/service/netty/PackageClient.java +++ b/netty/service/src/main/java/cn/bunny/service/netty/PackageClient.java @@ -9,11 +9,20 @@ import io.netty.channel.ChannelInitializer; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; +import io.netty.handler.logging.LogLevel; +import io.netty.handler.logging.LoggingHandler; import lombok.extern.slf4j.Slf4j; @Slf4j public class PackageClient { public static void main(String[] args) { + // 分 10 次发送 + for (int i = 0; i < 10; i++) { + send(); + } + } + + private static void send() { NioEventLoopGroup worker = new NioEventLoopGroup(); try { Bootstrap bootstrap = new Bootstrap(); @@ -22,15 +31,17 @@ public class PackageClient { bootstrap.handler(new ChannelInitializer() { @Override protected void initChannel(SocketChannel ch) throws Exception { + log.debug("conneted..."); + ch.pipeline().addLast(new LoggingHandler(LogLevel.DEBUG)); ch.pipeline().addLast(new ChannelInboundHandlerAdapter() { @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { log.debug("sending..."); - for (int i = 0; i < 10; i++) { - ByteBuf buffer = ctx.alloc().buffer(); - buffer.writeBytes(new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}); - ctx.writeAndFlush(buffer); - } + ByteBuf buffer = ctx.alloc().buffer(); + buffer.writeBytes(new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18}); + ctx.writeAndFlush(buffer); + // 发完即关 + ctx.close(); } }); } diff --git a/netty/service/src/main/java/cn/bunny/service/netty/PackageServer.java b/netty/service/src/main/java/cn/bunny/service/netty/PackageServer.java index c79badf..4361089 100644 --- a/netty/service/src/main/java/cn/bunny/service/netty/PackageServer.java +++ b/netty/service/src/main/java/cn/bunny/service/netty/PackageServer.java @@ -19,8 +19,9 @@ public class PackageServer { ServerBootstrap serverBootstrap = new ServerBootstrap(); serverBootstrap.channel(NioServerSocketChannel.class); serverBootstrap.group(boss, work); - // 接受缓冲区 - serverBootstrap.option(ChannelOption.SO_RCVBUF, 10); + // 接受缓冲区,接受为10个字节 + // serverBootstrap.option(ChannelOption.SO_RCVBUF, 10); + serverBootstrap.childOption(ChannelOption.RCVBUF_ALLOCATOR, new AdaptiveRecvByteBufAllocator(16, 16, 16)); serverBootstrap.childHandler(new ChannelInitializer() { @Override protected void initChannel(SocketChannel socketChannel) throws Exception {