feat(新增): 🚀 netty开始的客户端

This commit is contained in:
Bunny 2024-05-23 22:27:15 +08:00
parent 83449e7e7d
commit f0ff7a82f1
2 changed files with 41 additions and 2 deletions

View File

@ -0,0 +1,36 @@
package cn.bunny.service.netty.demo1;
import io.netty.bootstrap.Bootstrap;
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 io.netty.handler.codec.string.StringDecoder;
import java.net.InetSocketAddress;
public class StartClient01 {
/*
客户端处理内容
*/
public static void main(String[] args) throws InterruptedException {
// 启动类
new Bootstrap()
// 添加 事件循环
.group(new NioEventLoopGroup())
// 选择客户端 channel 实现
.channel(NioServerSocketChannel.class)
// 添加处理器
.handler(new ChannelInitializer<NioSocketChannel>() {
// 连接后被调用
@Override
protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception {
nioSocketChannel.pipeline().addLast(new StringDecoder());
}
})
.connect(new InetSocketAddress("localhost", 8080))
.sync()
.channel()
.writeAndFlush("你好啊啊啊");
}
}

View File

@ -1,4 +1,4 @@
package cn.bunny.service.netty; package cn.bunny.service.netty.demo1;
import io.netty.bootstrap.ServerBootstrap; import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
@ -11,7 +11,10 @@ import io.netty.handler.codec.string.StringDecoder;
import static java.lang.StringTemplate.STR; import static java.lang.StringTemplate.STR;
public class Start01 { public class StartServer01 {
/*
服务器端处理内容
*/
public static void main(String[] args) { public static void main(String[] args) {
// 启动器 // 启动器
new ServerBootstrap() new ServerBootstrap()