From e69a5d1e3b5ec3cdc1ef20b942dac6981aaf0923 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=A6=E8=8B=B1=E6=9D=B0?= <327782001@qq.com> Date: Sun, 23 Apr 2023 21:58:10 +0800 Subject: [PATCH] =?UTF-8?q?fix:channelFuture=E7=BB=83=E4=B9=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../TestEventLoop_05_group.java" | 2 +- .../c3_channel/TestEventLoop_01_Client.java | 43 ++++++++++++++++++ .../TestEventLoop_02_ChannelFuture.java | 45 +++++++++++++++++++ 3 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/kwan/shuyu/heima/netty_04_netty/c3_channel/TestEventLoop_01_Client.java create mode 100644 src/main/java/com/kwan/shuyu/heima/netty_04_netty/c3_channel/TestEventLoop_02_ChannelFuture.java diff --git "a/src/main/java/com/kwan/shuyu/heima/netty_04_netty/c2_\347\273\204\344\273\266/TestEventLoop_05_group.java" "b/src/main/java/com/kwan/shuyu/heima/netty_04_netty/c2_\347\273\204\344\273\266/TestEventLoop_05_group.java" index 4023fd6..1c15447 100644 --- "a/src/main/java/com/kwan/shuyu/heima/netty_04_netty/c2_\347\273\204\344\273\266/TestEventLoop_05_group.java" +++ "b/src/main/java/com/kwan/shuyu/heima/netty_04_netty/c2_\347\273\204\344\273\266/TestEventLoop_05_group.java" @@ -11,7 +11,7 @@ import lombok.extern.slf4j.Slf4j; import java.nio.charset.Charset; /** - * NioEventLoopGroup + * 作用NioEventLoopGroup * * @author : qinyingjie * @version : 2.2.0 diff --git a/src/main/java/com/kwan/shuyu/heima/netty_04_netty/c3_channel/TestEventLoop_01_Client.java b/src/main/java/com/kwan/shuyu/heima/netty_04_netty/c3_channel/TestEventLoop_01_Client.java new file mode 100644 index 0000000..e1f2000 --- /dev/null +++ b/src/main/java/com/kwan/shuyu/heima/netty_04_netty/c3_channel/TestEventLoop_01_Client.java @@ -0,0 +1,43 @@ +package com.kwan.shuyu.heima.netty_04_netty.c3_channel; + +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; + + +/** + * Channel + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/4/23 16:43 + */ +public class TestEventLoop_01_Client { + public static void main(String[] args) throws InterruptedException { + //1.启动类 + final Channel channel = new Bootstrap() + // 2.添加 EventLoop + .group(new NioEventLoopGroup()) + //3.选择客户端channel实现 + .channel(NioSocketChannel.class) + // 4.添加处理器 + .handler(new ChannelInitializer() { + @Override //在连接建立后被调用 + protected void initChannel(NioSocketChannel ch) throws Exception { + ch.pipeline().addLast(new StringEncoder()); + } + }) + //连接到服务器 + .connect(new InetSocketAddress("localhost", 8080)) + .sync() + .channel(); + //发送数据 + channel.writeAndFlush("hello world"); + + } +} \ No newline at end of file diff --git a/src/main/java/com/kwan/shuyu/heima/netty_04_netty/c3_channel/TestEventLoop_02_ChannelFuture.java b/src/main/java/com/kwan/shuyu/heima/netty_04_netty/c3_channel/TestEventLoop_02_ChannelFuture.java new file mode 100644 index 0000000..722d5d4 --- /dev/null +++ b/src/main/java/com/kwan/shuyu/heima/netty_04_netty/c3_channel/TestEventLoop_02_ChannelFuture.java @@ -0,0 +1,45 @@ +package com.kwan.shuyu.heima.netty_04_netty.c3_channel; + +import io.netty.bootstrap.Bootstrap; +import io.netty.channel.Channel; +import io.netty.channel.ChannelFuture; +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; + + +/** + * ChannelFuture + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/4/23 16:43 + */ +public class TestEventLoop_02_ChannelFuture { + public static void main(String[] args) throws InterruptedException { + //1.启动类 + final ChannelFuture channelFuture = new Bootstrap() + // 2.添加 EventLoop + .group(new NioEventLoopGroup()) + //3.选择客户端channel实现 + .channel(NioSocketChannel.class) + // 4.添加处理器 + .handler(new ChannelInitializer() { + @Override //在连接建立后被调用 + protected void initChannel(NioSocketChannel ch) throws Exception { + ch.pipeline().addLast(new StringEncoder()); + } + }) + //连接到服务器 + .connect(new InetSocketAddress("localhost", 8080)); + channelFuture.sync(); + //获取Channel + final Channel channel = channelFuture.channel(); + //发送数据 + channel.writeAndFlush("hello world"); + + } +} \ No newline at end of file -- GitLab