From 7a7030a0e4714cda54a3e6184555ad17c66e71fa Mon Sep 17 00:00:00 2001 From: bunny <1319900154@qq.com> Date: Thu, 23 May 2024 13:31:47 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E6=96=B0=E5=A2=9E):=20:rocket:=20?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E6=9C=8D=E5=8A=A1=E7=AB=AF=E6=8E=A5=E5=8F=97?= =?UTF-8?q?=E6=B6=88=E6=81=AF=EF=BC=8C=E5=AE=A2=E6=88=B7=E7=AB=AF=E4=BD=BF?= =?UTF-8?q?=E7=94=A8debug=E6=96=B9=E5=BC=8F=E5=8F=91=E9=80=81=E6=B6=88?= =?UTF-8?q?=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/bunny/{demo1 => }/TestDemoClient.java | 21 ++++++++---- .../cn/bunny/demo3/EventLoopServerTest.java | 34 +++++++++++++++++++ 2 files changed, 48 insertions(+), 7 deletions(-) rename netty/service/src/test/java/cn/bunny/{demo1 => }/TestDemoClient.java (73%) create mode 100644 netty/service/src/test/java/cn/bunny/demo3/EventLoopServerTest.java diff --git a/netty/service/src/test/java/cn/bunny/demo1/TestDemoClient.java b/netty/service/src/test/java/cn/bunny/TestDemoClient.java similarity index 73% rename from netty/service/src/test/java/cn/bunny/demo1/TestDemoClient.java rename to netty/service/src/test/java/cn/bunny/TestDemoClient.java index 827af44..51aabd6 100644 --- a/netty/service/src/test/java/cn/bunny/demo1/TestDemoClient.java +++ b/netty/service/src/test/java/cn/bunny/TestDemoClient.java @@ -1,32 +1,39 @@ -package cn.bunny.demo1; +package cn.bunny; 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; import java.util.Date; public class TestDemoClient { public static void main(String[] args) throws InterruptedException { // 启动类 - new Bootstrap() + Channel channel = new Bootstrap() // 添加EventLoop .group(new NioEventLoopGroup()) - // 选择客户端 Channelt + // 设置客户端Channel类型 + .channel(NioSocketChannel.class) + // 选择客户端 Channel .handler(new ChannelInitializer() { @Override// 在连接建立后调用 protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception { nioSocketChannel.pipeline().addLast(new StringEncoder()); } }) - .connect("localhost", 8080) + .connect(new InetSocketAddress("localhost", 8080)) .sync() - .channel() - // 向服务器发送数据 - .writeAndFlush("你好啊啊啊"); + .channel(); + + System.out.println(channel);// 使用debug方式发送消息... + System.out.println(); + // // 向服务器发送数据 + // .writeAndFlush("你好啊啊啊"); } private static void m1() throws InterruptedException { diff --git a/netty/service/src/test/java/cn/bunny/demo3/EventLoopServerTest.java b/netty/service/src/test/java/cn/bunny/demo3/EventLoopServerTest.java new file mode 100644 index 0000000..b223aaf --- /dev/null +++ b/netty/service/src/test/java/cn/bunny/demo3/EventLoopServerTest.java @@ -0,0 +1,34 @@ +package cn.bunny.demo3; + +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 EventLoopServerTest { + 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); + } +}