From 604c958f7d2198266c12d7d2606bbf5095bfa3b6 Mon Sep 17 00:00:00 2001 From: Bunny <1319900154@qq.com> Date: Thu, 23 May 2024 22:57:59 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E6=96=B0=E5=A2=9E):=20:rocket:=20netty?= =?UTF-8?q?=E7=9A=84EventLoopIo=E7=9A=84=E5=AE=A2=E6=88=B7=E7=AB=AF?= =?UTF-8?q?=E5=92=8C=E6=9C=8D=E5=8A=A1=E7=AB=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../netty/demo2/EventLoopIoClient.java | 32 +++++++++++++++++ .../netty/demo2/EventLoopIoServer.java | 36 +++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 netty/service/src/main/java/cn/bunny/service/netty/demo2/EventLoopIoClient.java create mode 100644 netty/service/src/main/java/cn/bunny/service/netty/demo2/EventLoopIoServer.java diff --git a/netty/service/src/main/java/cn/bunny/service/netty/demo2/EventLoopIoClient.java b/netty/service/src/main/java/cn/bunny/service/netty/demo2/EventLoopIoClient.java new file mode 100644 index 0000000..eeffb40 --- /dev/null +++ b/netty/service/src/main/java/cn/bunny/service/netty/demo2/EventLoopIoClient.java @@ -0,0 +1,32 @@ +package cn.bunny.service.netty.demo2; + +import io.netty.bootstrap.Bootstrap; +import io.netty.channel.Channel; +import io.netty.channel.ChannelInitializer; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.nio.NioSocketChannel; +import io.netty.handler.codec.string.StringEncoder; + +import java.net.InetSocketAddress; + +public class EventLoopIoClient { + public static void main(String[] args) throws InterruptedException { + Channel channel = new Bootstrap() + .group(new NioEventLoopGroup()) + .channel(NioSocketChannel.class) + .handler(new ChannelInitializer() { + @Override + protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception { + nioSocketChannel.pipeline().addLast(new StringEncoder()); + } + }) + .connect(new InetSocketAddress("localhost", 8080)) + .sync() + .channel(); + + + System.out.println(channel); + channel.writeAndFlush("你好啊啊啊"); + System.out.println(); + } +} diff --git a/netty/service/src/main/java/cn/bunny/service/netty/demo2/EventLoopIoServer.java b/netty/service/src/main/java/cn/bunny/service/netty/demo2/EventLoopIoServer.java new file mode 100644 index 0000000..bfa2ad8 --- /dev/null +++ b/netty/service/src/main/java/cn/bunny/service/netty/demo2/EventLoopIoServer.java @@ -0,0 +1,36 @@ +package cn.bunny.service.netty.demo2; + +import io.netty.bootstrap.ServerBootstrap; +import io.netty.buffer.ByteBuf; +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.nio.NioServerSocketChannel; +import io.netty.channel.socket.nio.NioSocketChannel; +import lombok.extern.slf4j.Slf4j; + +import java.nio.charset.Charset; + +@Slf4j +public class EventLoopIoServer { + public static void main(String[] args) { + new ServerBootstrap() + .group(new NioEventLoopGroup()) + .channel(NioServerSocketChannel.class) + + .childHandler(new ChannelInitializer() { + @Override + protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception { + nioSocketChannel.pipeline().addLast(new ChannelInboundHandlerAdapter() { + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + ByteBuf buf = (ByteBuf) msg; + log.debug(buf.toString(Charset.defaultCharset())); + } + }); + } + }) + .bind(8080); + } +}