Compare commits
No commits in common. "8275bdc2815d1dbe9f2667f51af1ecc2b14e80b9" and "f0ff7a82f1fff7b9217f0c5c85c153bc00682e0b" have entirely different histories.
8275bdc281
...
f0ff7a82f1
|
@ -3,8 +3,9 @@ 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.StringEncoder;
|
import io.netty.handler.codec.string.StringDecoder;
|
||||||
|
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
|
|
||||||
|
@ -18,15 +19,14 @@ public class StartClient01 {
|
||||||
// 添加 事件循环
|
// 添加 事件循环
|
||||||
.group(new NioEventLoopGroup())
|
.group(new NioEventLoopGroup())
|
||||||
// 选择客户端 channel 实现
|
// 选择客户端 channel 实现
|
||||||
.channel(NioSocketChannel.class)
|
.channel(NioServerSocketChannel.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 StringEncoder());
|
nioSocketChannel.pipeline().addLast(new StringDecoder());
|
||||||
}
|
}
|
||||||
|
|
||||||
})
|
})
|
||||||
.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;
|
|
||||||
|
|
||||||
@Slf4j
|
import static java.lang.StringTemplate.STR;
|
||||||
|
|
||||||
public class StartServer01 {
|
public class StartServer01 {
|
||||||
/*
|
/*
|
||||||
服务器端处理内容
|
服务器端处理内容
|
||||||
|
@ -37,7 +37,7 @@ public class StartServer01 {
|
||||||
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
|
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
|
||||||
// 打印上一步转化好的字符串
|
// 打印上一步转化好的字符串
|
||||||
System.out.println(STR."消息为\{msg}");
|
System.out.println(STR."消息为\{msg}");
|
||||||
log.debug("消息:{}", msg);
|
super.channelRead(ctx, msg);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,32 +0,0 @@
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,36 +0,0 @@
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,45 +0,0 @@
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,34 +0,0 @@
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue