玩转Netty,从“Hello World”开始!( 二 )

  • 导入依赖:我们直接用4.x最新的版本
  • <dependency><groupId>io.netty</groupId><artifactId>netty-all</artifactId><version>4.1.92.Final</version></dependency>
    1. 编写代码:那么我们就开始编写这个Demo的服务器和客户端相关代码
    • NettyServer:基于Netty的客户端
     /** * <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();}}


    推荐阅读