package com.thx.netty4.server;
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.handler.timeout.ReadTimeoutHandler;
import java.net.InetSocketAddress;
public class NettyServer {
private int port;
public NettyServer(int port) {
this.port = port;
}
public void run() throws Exception {
/***
* NioEventLoopGroup 是用来处理I/O操作的多线程事件循环器,
* Netty提供了许多不同的EventLoopGroup的实现用来处理不同传输协议。
* 在这个例子中我们实现了一个服务端的应用,
* 因此会有2个NioEventLoopGroup会被使用。
* 第一个经常被叫做‘boss’,用来接收进来的连接。
* 第二个经常被叫做‘worker’,用来处理已经被接收的连接,
* 一旦‘boss’接收到连接,就会把连接信息注册到‘worker’上。
* 如何知道多少个线程已经被使用,如何映射到已经创建的Channels上都需要依赖于EventLoopGroup的实现,
* 并且可以通过构造函数来配置他们的关系。
*/
/** 创建2个线程组,含有的子线程NioEventLoop的个数由于没有指定现场数量,默认为cpu核数的2倍。
* bossGroup只是处理连接请求。workerGroup处理真正和客户端的业务逻辑,*/
EventLoopGroup bossGroup = new NioEventLoopGroup(1); //接收消息循环队列
EventLoopGroup workerGroup = new NioEventLoopGroup(); //发送消息循环队列
System.out.println("准备运行端口:" + port);
try {
/**
* ServerBootstrap 是一个启动NIO服务的辅助启动类
* 你可以在这个服务中直接使用Channel
*/
ServerBootstrap bootstrap = new ServerBootstrap();
/**
* 这一步是必须的,如果没有设置group将会报java.lang.IllegalStateException: group not set异常
*/
bootstrap.group(bossGroup, workerGroup);
/***
* ServerSocketChannel,用来接收新的连接
* 以NIO的selector为基础进行实现
*/
bootstrap.channel(NioServerSocketChannel.class);
/*
* option 设置boss线程组
* childOption 设置worker线程组。只有服务端才有childOption
*/
bootstrap.childOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, 3000);
//bootstrap.option(ChannelOption.SO_SNDBUF, 1024*128); //发包缓冲区
//bootstrap.option(ChannelOption.SO_RCVBUF, 1024*128); //收包换成区
bootstrap.childOption(ChannelOption.TCP_NODELAY, true); //TCP立即发包
/***
* 你可以设置这里指定的通道实现的配置参数。
* 因此我们被允许设置socket的参数选项比如tcpNoDelay和keepAlive。
* 请参考ChannelOption和详细的ChannelConfig实现的接口文档以此可以对ChannelOptions的有一个大概的认识。
*/
//bootstrap.option(ChannelOption.SO_BACKLOG, 128);
/***
* 是利用TCP的SO_KEEPALIVE属性,当SO_KEEPALIVE=true的时候,服务端可以探测客户端的连接是否还存活着,
* 如果客户端因为断电或者网络问题或者客户端挂掉了等,那么服务端的连接可以关闭掉,释放资源。
*/
bootstrap.childOption(ChannelOption.SO_KEEPALIVE, true);
/***
* 这里的事件处理类经常会被用来处理一个最近的已经接收的Channel。
* ChannelInitializer是一个特殊的处理类,
* 他的目的是帮助使用者配置一个新的Channel。
* 也许你想通过增加一些处理类比如NettyServerHandler来配置一个新的Channel
* 或者其对应的ChannelPipeline来实现你的网络程序。
* 当你的程序变的复杂时,可能你会增加更多的处理类到pipline上,
* 然后提取这些匿名类到最顶层的类上。
*/
bootstrap.localAddress(new InetSocketAddress(port)).childHandler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception {
// TODO Auto-generated method stub
ChannelPipeline pipeline = ch.pipeline();
//pipeline.addLast(new StringDecoder()); //Netty4自带的String解码器
//pipeline.addLast(new StringEncoder()); //Netty4自带的String编码器
pipeline.addLast(new TimeServerHandler()); // 自己实现的处理器
pipeline.addLast(new ReadTimeoutHandler(120));//超时链接处理器,120秒没有收到数据就自动断掉链接。
}
});
/***
* 绑定端口并启动去接收进来的连接
*/
ChannelFuture future = bootstrap.bind(port).sync();
if (future.isSuccess()) {
System.out.println("服务端启动成功,端口:"+port);
} else {
System.out.println("服务端启动失败!");
}
/**
* 这里会一直等待,直到socket被关闭
*/
future.channel().closeFuture().sync();
if (future.isSuccess()){}
future.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 = 8000;
}
new NettyServer(port).run();
//通过cmd窗口的telnet 127.0.0.1 8000运行
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
nettytest.zip (51个子文件)
nettytest
pom.xml 634B
out
artifacts
nettytest_war_exploded
WEB-INF
web.xml 304B
classes
com
thx
netty
Client$1.class 1KB
Server$1.class 1KB
Client.class 2KB
ClientHandler.class 2KB
Server.class 2KB
ServerHandler.class 2KB
META-INF
MANIFEST.MF 89B
index.html 142B
target
maven-status
maven-compiler-plugin
compile
default-compile
createdFiles.lst 166B
inputFiles.lst 919B
generated-sources
annotations
classes
com
thx
netty4
server
NettyServer$1.class 1KB
TimeServerHandler.class 3KB
NettyServer.class 3KB
DiscardServerHandler.class 1KB
client
NettyClient.class 3KB
NettyClientHandler.class 3KB
NettyClient$1.class 1KB
netty
Client$1.class 1KB
Server$1.class 1KB
Client.class 2KB
ClientHandler.class 2KB
Server.class 2KB
ServerHandler.class 2KB
tcp
server
TcpServer.class 1010B
TcpServerHandlerExcutePool.class 1KB
TcpServerHanlder.class 2KB
client
TcpClientHanlder.class 2KB
TcpClient.class 2KB
src
test
java
main
resources
java
com
thx
netty4
server
NettyServer.java 6KB
TimeServerHandler.java 2KB
DiscardServerHandler.java 2KB
client
NettyClient.java 3KB
NettyClientHandler.java 3KB
netty
Server.java 2KB
ServerHandler.java 1KB
ClientHandler.java 888B
Client.java 1KB
tcp
server
TcpServer.java 808B
TcpServerHanlder.java 1KB
TcpServerHandlerExcutePool.java 1KB
client
TcpClientHanlder.java 1KB
TcpClient.java 735B
netty5
web
WEB-INF
web.xml 304B
index.html 142B
.idea
workspace.xml 54KB
misc.xml 513B
artifacts
nettytest_war_exploded.xml 511B
compiler.xml 530B
nettytest.iml 639B
共 51 条
- 1
资源评论
djbiiinng
- 粉丝: 0
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功