代码拉取完成,页面将自动刷新
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();
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。