Ai
1 Star 0 Fork 0

spitman/learnnetty

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
NettyIO_2.java 1.51 KB
一键复制 编辑 原始数据 按行查看 历史
zhouyijin 提交于 2021-04-27 10:22 +08:00 . first commit
package org.zyj.io;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
/**
* 4条线程处理所有事件
*/
public class NettyIO_2 {
public static void main(String[] args) throws InterruptedException {
//boss线程设置4个
//线程既是boss也是worker
NioEventLoopGroup boss = new NioEventLoopGroup(4);
ServerBootstrap boot = new ServerBootstrap();
boot.group(boss, boss)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<NioSocketChannel>() {
@Override
protected void initChannel(NioSocketChannel channel) throws Exception {
ChannelPipeline pipeline = channel.pipeline();
pipeline.addLast(new MyInbound());
}
});
ChannelFuture future = boot.bind(8080).sync();
future.channel().closeFuture().sync();
}
static class MyInbound extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ctx.write(msg);//echo的作用, 将读到的东西,写回客户端
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
ctx.flush();
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/spitman/learnnetty.git
git@gitee.com:spitman/learnnetty.git
spitman
learnnetty
learnnetty
master

搜索帮助