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 4023fd6efaca5adb633f765c19f7719e5923e017..1c15447679a451b87386c936a7115af7c50bf2c6 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 0000000000000000000000000000000000000000..e1f2000e1dfc9bb5981dff20aea5e1cf8b6ecf91 --- /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 0000000000000000000000000000000000000000..722d5d4fbcce783273f362d44c666793fdbb75e0 --- /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