From 89edfb6e4d672b09dc83b7cd45c81ade17d37beb Mon Sep 17 00:00:00 2001 From: Bunny <1319900154@qq.com> Date: Sat, 25 May 2024 00:14:56 +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?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/bunny/service/netty/PackageClient.java | 47 +++++++++++++++++ .../cn/bunny/service/netty/PackageServer.java | 52 +++++++++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 netty/service/src/main/java/cn/bunny/service/netty/PackageClient.java create mode 100644 netty/service/src/main/java/cn/bunny/service/netty/PackageServer.java 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 new file mode 100644 index 0000000..b7c762c --- /dev/null +++ b/netty/service/src/main/java/cn/bunny/service/netty/PackageClient.java @@ -0,0 +1,47 @@ +package cn.bunny.service.netty; + +import io.netty.bootstrap.Bootstrap; +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInboundHandlerAdapter; +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 lombok.extern.slf4j.Slf4j; + +@Slf4j +public class PackageClient { + public static void main(String[] args) { + NioEventLoopGroup worker = new NioEventLoopGroup(); + try { + Bootstrap bootstrap = new Bootstrap(); + bootstrap.channel(NioSocketChannel.class); + bootstrap.group(worker); + bootstrap.handler(new ChannelInitializer() { + @Override + protected void initChannel(SocketChannel ch) throws Exception { + 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); + } + } + }); + } + }); + ChannelFuture channelFuture = bootstrap.connect("127.0.0.1", 8080).sync(); + channelFuture.channel().closeFuture().sync(); + + } catch (InterruptedException e) { + log.error("client error", e); + } finally { + worker.shutdownGracefully(); + } + } +} 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 new file mode 100644 index 0000000..c79badf --- /dev/null +++ b/netty/service/src/main/java/cn/bunny/service/netty/PackageServer.java @@ -0,0 +1,52 @@ +package cn.bunny.service.netty; + +import io.netty.bootstrap.ServerBootstrap; +import io.netty.channel.*; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.SocketChannel; +import io.netty.channel.socket.nio.NioServerSocketChannel; +import io.netty.handler.logging.LogLevel; +import io.netty.handler.logging.LoggingHandler; +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public class PackageServer { + public static void main(String[] args) throws InterruptedException { + NioEventLoopGroup boss = new NioEventLoopGroup(1); + NioEventLoopGroup work = new NioEventLoopGroup(); + + try { + ServerBootstrap serverBootstrap = new ServerBootstrap(); + serverBootstrap.channel(NioServerSocketChannel.class); + serverBootstrap.group(boss, work); + // 接受缓冲区 + serverBootstrap.option(ChannelOption.SO_RCVBUF, 10); + serverBootstrap.childHandler(new ChannelInitializer() { + @Override + protected void initChannel(SocketChannel socketChannel) throws Exception { + socketChannel.pipeline().addLast(new LoggingHandler(LogLevel.DEBUG)); + socketChannel.pipeline().addLast(new ChannelInboundHandlerAdapter() { + @Override + public void channelActive(ChannelHandlerContext ctx) throws Exception { + log.debug("connected {}", ctx.channel()); + super.channelActive(ctx); + } + + @Override + public void channelInactive(ChannelHandlerContext ctx) throws Exception { + log.debug("disconnect {}", ctx.channel()); + super.channelInactive(ctx); + } + }); + } + }); + ChannelFuture channelFuture = serverBootstrap.bind(8080).sync(); + channelFuture.channel().closeFuture().sync(); + } catch (Exception exception) { + log.error("服务器错误。。。", exception); + } finally { + boss.shutdownGracefully(); + work.shutdownGracefully(); + } + } +}