- 主页 > 生活百科 > >
玩转Netty,从“Hello World”开始!( 二 )
导入依赖:我们直接用4.x最新的版本<dependency><groupId>io.netty</groupId><artifactId>netty-all</artifactId><version>4.1.92.Final</version></dependency> - 编写代码:那么我们就开始编写这个Demo的服务器和客户端相关代码
/** * <p>Date: 2023/5/14 10:29</p> * <p>Author: fighter3</p> * <p>Description: Netty服务端Demo</p> */public class NettyServer{// 服务器监听的端口号private int port;public NettyServer(int port) {this.port = port;}/*** 启动Netty服务器* @throws InterruptedException*/public void run() throws InterruptedException {// 创建boss线程组和worker线程组// bossGroup 用于监听客户端的连接请求 , 将连接请求发送给 workerGroup 进行处理NioEventLoopGroup bossGroup = new NioEventLoopGroup();// workerGroup 用于处理客户端连接的数据读写NioEventLoopGroup workerGroup = new NioEventLoopGroup();try {// 创建 ServerBootstrap 对象 , 用于启动 Netty 服务器ServerBootstrap serverBootstrap = new ServerBootstrap();// 绑定线程池事件组serverBootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)// 通道初始化回调函数 , 在启动的时候可以自动调用.childHandler(new ChannelInitializer<SocketChannel>() {@Overridepublic void initChannel(SocketChannel ch) throws Exception {ChannelPipeline pipeline = ch.pipeline();// 添加消息处理器pipeline.addLast(new NettyServerHandler());}});// 绑定端口 , 开始接收客户端请求ChannelFuture channelFuture = serverBootstrap.bind(port).sync();System.out.println("Netty服务器监听端口:"+port);// 等待服务端监听端口关闭channelFuture.channel().closeFuture().sync();} finally {//释放线程组资源bossGroup.shutdownGracefully();workerGroup.shutdownGracefully();}}public static void main(String[] args) throws InterruptedException {// 创建服务器对象 , 监听端口号为 8888NettyServer server = new NettyServer(8888);System.out.println("============Netty服务器启动...=============");// 启动服务器server.run();System.out.println("============Netty服务器停止...=============");}} - NettyServerHandler:服务器的消息处理器 , 用于处理各种事件
/** * <p>Date: 2023/5/14 10:30</p> * <p>Author: fighter3</p> * <p>Description: Netty服务器消息处理器</p> */public class NettyServerHandler extends ChannelInboundHandlerAdapter {/*** 当客户端上线的时候会触发这个方法* @param ctx* @throws Exception*/@Overridepublic void channelActive(ChannelHandlerContext ctx) throws Exception {String message="你好 , 靓仔!";ByteBuf hello = Unpooled.copiedBuffer(message, CharsetUtil.UTF_8);// 发送消息ctx.writeAndFlush(hello);}/***当 Channel 中有来自客户端的数据时就会触发这个方法*/@Overridepublic void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {ByteBuf buf = (ByteBuf) msg;System.out.println("客户端发来的消息:" + buf.toString(CharsetUtil.UTF_8)); // 接收消息并打印输出}/*** 当有异常时触发这个方法*/@Overridepublic void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {cause.printStackTrace();ctx.close();}} - NettyClient:使用Netty的客户端 , 通过ip和端口连接服务端
/** * <p>Date: 2023/5/14 10:32</p> * <p>Author: fighter3</p> * <p>Description: Netty客户端Demo</p> */public class NettyClient {// 服务器 IPprivate String host;// 服务器监听的端口号private int port;public NettyClient(String host, int port) {this.host = host;this.port = port;}/*** 启动 Netty 客户端*/public void run() throws InterruptedException {// 创建事件循环组NioEventLoopGroup group = new NioEventLoopGroup();try {// 创建 Bootstrap 对象Bootstrap bootstrap = new Bootstrap();// 配置 Bootstrap 对象// 设置线程组bootstrap.group(group)// 设置客户端通信的通道类型为NIO类型.channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {// 通道初始化回调函数 , 在启动的时候可以自动调用@Overridepublic void initChannel(SocketChannel ch) throws Exception {// 添加消息处理器ch.pipeline().addLast(new NettyClientHandler());}});// 连接服务器 , 异步等待连接成功ChannelFuture channelFuture = bootstrap.connect(host, port).sync();System.out.println("===========Netty客户端连接服务端=========");// 等待客户端连接关闭channelFuture.channel().closeFuture().sync();} finally {//释放资源group.shutdownGracefully();}}public static void main(String[] args) throws InterruptedException {// 创建客户端对象 , 并连接到服务器NettyClient client = new NettyClient("127.0.0.1", 8888);// 启动客户端 , 开始发送消息client.run();}}
推荐阅读
-
张柏芝早就被绿了?谢霆锋10年后终于公开女儿,原来王菲早已知晓
-
-
月球|开始行动了!NASA登月联盟在月球上发现水,却将中国排除在外
-
华为荣耀|突然宣布!荣耀营销经理退休,引发网友猜疑
-
-
Logo设计也可以二次元化,说不准某些图标的灵感正是来自于二次元
-
世界上最古老的四种文字分别是什么 世界上最古老的六大文字
-
-
招聘|用招聘网站的后台告诉你,为什么你投了几百份简历,很少有人回复
-
无敌改装车▲RR:Type-RR,最强思域FD2!Mugen
-
张爱玲|张爱玲在晚年崇尚“极简主义生活”,并非穷困潦倒,稿费拿到手软
-
AutoR智驾 逆行外卖小哥、深夜拥堵轻松应对,元戎启行自动驾驶汽车挑战“宇宙最强街道”
-
-
-
三十年体坛故事|7连杀+赛季双杀!上港仍是国安梦魇 御林军争冠梦碎?
-
-
教育部|教育部重要通知,2条升学途径被堵死!家长无语:之前努力白费了
-
妻子伺候二婚丈夫五年,继子来医院探望一趟,妻子含泪提出离婚
-
-