feat(新增): 🚀 netty开始的客户端和服务端
This commit is contained in:
parent
f0ff7a82f1
commit
d2fa8a8cf9
|
@ -3,9 +3,8 @@ package cn.bunny.service.netty.demo1;
|
||||||
import io.netty.bootstrap.Bootstrap;
|
import io.netty.bootstrap.Bootstrap;
|
||||||
import io.netty.channel.ChannelInitializer;
|
import io.netty.channel.ChannelInitializer;
|
||||||
import io.netty.channel.nio.NioEventLoopGroup;
|
import io.netty.channel.nio.NioEventLoopGroup;
|
||||||
import io.netty.channel.socket.nio.NioServerSocketChannel;
|
|
||||||
import io.netty.channel.socket.nio.NioSocketChannel;
|
import io.netty.channel.socket.nio.NioSocketChannel;
|
||||||
import io.netty.handler.codec.string.StringDecoder;
|
import io.netty.handler.codec.string.StringEncoder;
|
||||||
|
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
|
|
||||||
|
@ -19,14 +18,15 @@ public class StartClient01 {
|
||||||
// 添加 事件循环
|
// 添加 事件循环
|
||||||
.group(new NioEventLoopGroup())
|
.group(new NioEventLoopGroup())
|
||||||
// 选择客户端 channel 实现
|
// 选择客户端 channel 实现
|
||||||
.channel(NioServerSocketChannel.class)
|
.channel(NioSocketChannel.class)
|
||||||
// 添加处理器
|
// 添加处理器
|
||||||
.handler(new ChannelInitializer<NioSocketChannel>() {
|
.handler(new ChannelInitializer<NioSocketChannel>() {
|
||||||
// 连接后被调用
|
// 连接后被调用
|
||||||
@Override
|
@Override
|
||||||
protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception {
|
protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception {
|
||||||
nioSocketChannel.pipeline().addLast(new StringDecoder());
|
nioSocketChannel.pipeline().addLast(new StringEncoder());
|
||||||
}
|
}
|
||||||
|
|
||||||
})
|
})
|
||||||
.connect(new InetSocketAddress("localhost", 8080))
|
.connect(new InetSocketAddress("localhost", 8080))
|
||||||
.sync()
|
.sync()
|
||||||
|
|
|
@ -8,9 +8,9 @@ import io.netty.channel.nio.NioEventLoopGroup;
|
||||||
import io.netty.channel.socket.nio.NioServerSocketChannel;
|
import io.netty.channel.socket.nio.NioServerSocketChannel;
|
||||||
import io.netty.channel.socket.nio.NioSocketChannel;
|
import io.netty.channel.socket.nio.NioSocketChannel;
|
||||||
import io.netty.handler.codec.string.StringDecoder;
|
import io.netty.handler.codec.string.StringDecoder;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
import static java.lang.StringTemplate.STR;
|
@Slf4j
|
||||||
|
|
||||||
public class StartServer01 {
|
public class StartServer01 {
|
||||||
/*
|
/*
|
||||||
服务器端处理内容
|
服务器端处理内容
|
||||||
|
@ -26,22 +26,22 @@ public class StartServer01 {
|
||||||
.childHandler(
|
.childHandler(
|
||||||
// 代码客户端读写通道 添加别的 handler
|
// 代码客户端读写通道 添加别的 handler
|
||||||
new ChannelInitializer<NioSocketChannel>() {
|
new ChannelInitializer<NioSocketChannel>() {
|
||||||
@Override
|
|
||||||
protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception {
|
|
||||||
// 添加解码,将传输过来的 ByteBuffer 转为字符串
|
|
||||||
nioSocketChannel.pipeline().addLast(new StringDecoder());
|
|
||||||
// 自定处理器
|
|
||||||
nioSocketChannel.pipeline().addLast(new ChannelInboundHandlerAdapter() {
|
|
||||||
// 发生读事件
|
|
||||||
@Override
|
@Override
|
||||||
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
|
protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception {
|
||||||
// 打印上一步转化好的字符串
|
// 添加解码,将传输过来的 ByteBuffer 转为字符串
|
||||||
System.out.println(STR."消息为\{msg}");
|
nioSocketChannel.pipeline().addLast(new StringDecoder());
|
||||||
super.channelRead(ctx, msg);
|
// 自定处理器
|
||||||
|
nioSocketChannel.pipeline().addLast(new ChannelInboundHandlerAdapter() {
|
||||||
|
// 发生读事件
|
||||||
|
@Override
|
||||||
|
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
|
||||||
|
// 打印上一步转化好的字符串
|
||||||
|
System.out.println(STR."消息为\{msg}");
|
||||||
|
log.debug("消息:{}", msg);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
});
|
})
|
||||||
}
|
|
||||||
})
|
|
||||||
// 绑定端口
|
// 绑定端口
|
||||||
.bind(8080);
|
.bind(8080);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue