目录
用netty实现一个简单的“傻瓜服务器”
- 将接收到的数据打印在控制台上
环境要求:
- JDK 1.8
- Maven 3.3.3
- Netty 4.1
项目结构:
代码详情:
1、FoolServerHandler.java
public class FoolServerHandler extends ChannelInboundHandlerAdapter {
public void channelRead(ChannelHandlerContext ctx, Object msg){
//没用指定decoder和encoder,就用byteBuf来解析
ByteBuf in = (ByteBuf) msg;
try {
while (in.isReadable()) {
//将读取到的数据打印出来
System.out.print((char) in.readByte());
System.out.flush();
}
} finally {
((ByteBuf) msg).release();
}
}
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
cause.printStackTrace();
ctx.close();
}
}
2、FoolServer.java
public class FoolServer {
int port;
public FoolServer(int port) {
this.port = port;
}
public void run() throws Exception {
//服务端的应用,需要2个 NioEventLoopGroup
//第一个经常被叫做boss,用来接收进来的连接。
//第二个经常被叫做worker,用来处理已经被接收的连接,一旦‘boss’接收到连接,就会把连接信息注册到‘worker’上。
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
//启动 NIO 服务的辅助启动类
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
socketChannel.pipeline().addLast(new FoolServerHandler());
}
})
//socket的标准参数,并不是netty自己的
//标识当服务器请求处理线程全满时,用于临时存放已完成三次握手的请求的队列的最大长度
.option(ChannelOption.SO_BACKLOG, 128)
//是否启用心跳保活机制
.childOption(ChannelOption.SO_KEEPALIVE, true);
// 绑定端口,开始接收连接
ChannelFuture f = serverBootstrap.bind(port).sync();
// 等待服务器 socket 关闭
f.channel().closeFuture().sync();
} finally {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
}
public static void main(String[] args) throws Exception {
int port;
if (args.length > 0) {
port = Integer.parseInt(args[0]);
} else {
port = 8080;
}
new FoolServer(port).run();
}
}
运行效果:
源码地址:
https://github.com/tianyaning/code.git