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 {