Compare commits

...

4 Commits

6 changed files with 167 additions and 20 deletions

View File

@ -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()

View File

@ -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);
} }

View File

@ -0,0 +1,32 @@
package cn.bunny.service.netty.demo2;
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;
public class EventLoopIoClient {
public static void main(String[] args) throws InterruptedException {
Channel channel = new Bootstrap()
.group(new NioEventLoopGroup())
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<NioSocketChannel>() {
@Override
protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception {
nioSocketChannel.pipeline().addLast(new StringEncoder());
}
})
.connect(new InetSocketAddress("localhost", 8080))
.sync()
.channel();
System.out.println(channel);
channel.writeAndFlush("你好啊啊啊");
System.out.println();
}
}

View File

@ -0,0 +1,36 @@
package cn.bunny.service.netty.demo2;
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 EventLoopIoServer {
public static void main(String[] args) {
new ServerBootstrap()
.group(new NioEventLoopGroup())
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<NioSocketChannel>() {
@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);
}
}

View File

@ -0,0 +1,45 @@
package cn.bunny.service.netty.demo2;
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.DefaultEventLoopGroup;
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 EventLoopIoServer向下传递 {
public static void main(String[] args) {
DefaultEventLoopGroup group = new DefaultEventLoopGroup();
new ServerBootstrap()
.group(new NioEventLoopGroup(), new NioEventLoopGroup(2))
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<NioSocketChannel>() {
@Override
protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception {
nioSocketChannel.pipeline().addLast(group, "handler", new ChannelInboundHandlerAdapter() {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf buf = (ByteBuf) msg;
log.debug(buf.toString(Charset.defaultCharset()));
// 让消息继续传递下去
ctx.fireChannelRead(msg);
}
})
.addLast(group, "handler2", new ChannelInboundHandlerAdapter() {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf buf = (ByteBuf) msg;
log.debug(buf.toString(Charset.defaultCharset()));
}
});
}
}).bind(8080);
}
}

View File

@ -0,0 +1,34 @@
package cn.bunny.service.netty.demo2;
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 EventLoopIoThreadServer {
public static void main(String[] args) {
new ServerBootstrap()
.group(new NioEventLoopGroup(), new NioEventLoopGroup(2))
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<NioSocketChannel>() {
@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);
}
}