import com.sun.corba.se.spi.activation.Server;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import java.net.InetSocketAddress;
public class EchoServer {
public void bind(int port) throws Exception {
//配置服务端的NIO线程组
//实际上EventLoopGroup就是Reactor线程组
//两个Reactor一个用于服务端接收客户端的连接,另一个用于进行SocketChannel的网络读写
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try{
/**
* 由于我们使用在 NIO 传输,我们
已指定 NioEventLoopGroup接受和处理新连接,指定 NioServerSocketChannel
为信道类型。在此之后,我们设置本地地址是 InetSocketAddress 与所选择的端口(6)如。
服务器将绑定到此地址来监听新的连接请求。
*/
//ServerBootstrap对象是Netty用于启动NIO服务端的辅助启动类,目的是降低服务端开发的复杂度
ServerBootstrap b = new ServerBootstrap();
//Set the EventLoopGroup for the parent (acceptor) and the child (client).
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.localAddress(new InetSocketAddress(port))
//绑定I/O事件的处理类ChildChannelHandler,作用类似于Reactor模式中的Handler类
//主要用于处理网络I/O事件,例如记录日志,对消息进行编解码等
.childHandler(new ChannelInitializer<SocketChannel>(){
//添加ServerHandler到Channel的ChannelPipeline
//通过ServerHandler给每一个新来的Channel初始化
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new ServerHandler());
}
});
//绑定监听端口,调用sync同步阻塞方法等待绑定操作完成,完成后返回ChannelFuture类似于JDK中Future
ChannelFuture f = b.bind(port).sync();
//使用sync方法进行阻塞,等待服务端链路关闭之后Main函数才退出
f.channel().closeFuture().sync();
}finally {
//优雅退出,释放线程池资源
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
public static void main(String[] args) throws Exception {
int port = 8080;
if(args != null && args.length > 0) {
try {
port = Integer.valueOf(args[0]);
}catch (NumberFormatException e) {
//采用默认值
}
}
new EchoServer().bind(port);
}
private class ServerHandler extends ChannelInboundHandlerAdapter {
//每个信息入站都会调用
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf buf = (ByteBuf) msg;
System.out.println("The server receive msg :" + buf.toString());
ctx.write(buf);
}
//通知处理器最后的channelread()是当前批处理中的最后一条消息时调用
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
ctx.flush();
}
//读操作时捕获到异常时调用
@Override
public void exceptionCaught (ChannelHandlerContext ctx, Throwable cause) {
ctx.close();
}
}
}
基于netty的java游戏服务器.zip
版权申诉
35 浏览量
2023-12-17
21:43:00
上传
评论
收藏 14KB ZIP 举报
马coder
- 粉丝: 1201
- 资源: 6602
最新资源
- ARM Limited 发布的《RealView 编译工具 4.0 版编译器参考指南》
- 《2024音视频技术发展报告》,由LiveVideoStack出品,旨在深入了解流媒体和RTC(实时通信技术)的从业情情况
- 2023-04-06-项目笔记 - 第一百二十五阶段 - 4.4.2.123全局变量的作用域-123 -2024.05.06
- MATLAB编程高效实战:涵盖核心数学、科学计算、数据可视化及算法应用,助力工程师与研究人员的必备函数代码集
- halcon 3D图像重建
- 现有student.txt和student-score.txt 将两个文件上传到hdfs上 使用Map/Reduce框架完成下面
- 测试数据如下 1)文件一:data01.txt,内容:Beijing is beautiful I love Beijing
- 1_notepad_share_encrypt.hdoc..bin
- 基于混合遗传算法的三维装箱问题分析-数学建模
- 2023-04-06-项目笔记 - 第一百二十五阶段 - 4.4.2.123全局变量的作用域-123 -2024.05.06
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈