- 主页 > 生活百科 > >
玩转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();}}
推荐阅读
-
斗破苍穹电影版预告播出,内容改动太大,网友:斗气化马又来了
-
掌上管理|一套让下属死心塌地为你卖命的薪酬模式,激励方式是需要变化的
-
新疆新增新冠肺炎确诊病例13例 新增无症状感染者18例
-
腾讯|中国首富再度易主,新首富手握4100亿财富,反超马云登顶第一
-
我家有个亲戚,多年的生活目标就是供她的儿子出国读书,可她儿子虽然学习成绩很好,但性格内向,不善交流,自理能力差,我怎样打消她这个念头
-
-
-
-
泪湿青衫|停止证明自己,你将回归真实干净的自己,从羞愧和自责中走出
-
决明子和菟丝子的功效与作用及食用方法 菟丝子的功效与作用及食用方法
-
-
大唐狄公案|《大唐狄公案》即将来袭,画面超美,阵容强大引人期待!
-
-
折叠屏手机手工耿“发明”吃葡萄手机支架:唯一万元内 5G 折叠屏手机被虐惨
-
-
爱辉科技|口碑最好的iPhone,升级iOS13四年不卡,那一年的果粉很幸运!,原创
-
-
-
-
五体投地 刚刚解释《五体投地》打一个生肖动物是什么生肖钟南山:广州疫情防控取得阶段性胜利