zbb20180930 java,nio,netty Netty5.0用法
2021-06-15 11:05
class ServerHandler extends ChannelHandlerAdapter {
/**
* 当通道被调用,执行该方法
*/
@Override
publicvoid channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
// 接收数据
String value = (String) msg;
System.out.println("Server msg:" + value);
// 回复给客户端 “您好!”
String res = "好的...";
ctx.writeAndFlush(Unpooled.copiedBuffer(res.getBytes()));
}
}
publicclass NettyServer {
publicstaticvoid main(String[] args) throws InterruptedException {
System.out.println("服务器端已经启动....");
// 1.创建2个线程,一个负责接收客户端连接, 一个负责进行 传输数据
NioEventLoopGroup pGroup = new NioEventLoopGroup();
NioEventLoopGroup cGroup = new NioEventLoopGroup();
// 2. 创建服务器辅助类
ServerBootstrap b = new ServerBootstrap();
b.group(pGroup, cGroup).channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, 1024)
// 3.设置缓冲区与发送区大小
.option(ChannelOption.SO_SNDBUF, 32 * 1024).option(ChannelOption.SO_RCVBUF, 32 * 1024)
.childHandler(new ChannelInitializer
@Override
protectedvoid initChannel(SocketChannel sc) throws Exception {
sc.pipeline().addLast(new StringDecoder());
sc.pipeline().addLast(new ServerHandler());
}
});
ChannelFuture cf = b.bind(8080).sync();
cf.channel().closeFuture().sync();
pGroup.shutdownGracefully();
cGroup.shutdownGracefully();
}
}
文章标题:zbb20180930 java,nio,netty Netty5.0用法
文章链接:http://soscw.com/index.php/essay/94146.html