- 主页 > 生活百科 > >
玩转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岁上大学女孩父亲私塾被查封:属非法办学,硬件师资都不合格
-
吧吧嘛嘛你画对眼线了吗?你的眼型适合这样的漂亮眼线!
-
异常|上海专家成功挑战高难度手术 10岁罕见畸形女童上海找回女儿身
-
-
普拉达女王▲Jennie 晒画报,大秀锁骨直角肩,太高级了?BlackPink
-
通讯每日观察 网友:我还是支持p40,小米销量又超华为
-
-
郭德纲现身快手总部,在平台选择上,他犯了三个明显错误
-
雪佛兰|吉利帝豪与本田锋范对撞,新车被撞得面目全非,防撞梁发生断裂!
-
金色田野|只有他们对你不离不弃,大难临头各自飞?生肖鼠一定要珍惜的人
-
游人馆|玩家为何花五位数自购服务器开黑?硬核指挥官的需求你不懂!
-
-
吴卓羲|披哥成团夜22进17,胜率看哭浪姐,吴卓羲输了又好像没输
-
导体|兴森科技:预计上半年净利润3.68亿元~3.80亿元,同比增长164.83%~173.46%
-
我国的植树节是哪一天设定的 我国的植树节是哪年哪月哪天设定的
-
-
荷花|公园荷塘惨遭“摧花辣手” 当地相关部门:提升水位“断路”
-
-