环境搭建及使用)
一、项目创建在 Eclipse 中右键新建-项目-Maven-Maven Project-下一步-选择 quickstart下一步-设置如图参数自取点击完成。项目会自动创建 pom.xml 文件打开该文件点击 Dependencies标签-Add..设置如图参数由 Netty 版本决定点击 OK保存文件观察 Maven Dependencies 下netty 的 jar 包已经添加完毕。到此为止项目配置完毕了下面就来添加代码吧。二、实现一个简单的服务端 demo1. 首选我们需要自定义一个类处理服务器接收到的消息1 package com.coder.server; 2 3 import io.netty.buffer.ByteBuf; 4 import io.netty.channel.ChannelHandlerContext; 5 import io.netty.channel.ChannelInboundHandlerAdapter; 6 import io.netty.util.CharsetUtil; 7 import io.netty.util.ReferenceCountUtil; 8 9 /** 10 * 输出接收到的消息 11 * author Coder 12 * 13 */ 14 public class HelloServerHandler extends ChannelInboundHandlerAdapter { 15 /** 16 * 收到数据时调用 17 */ 18 Override 19 public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { 20 try { 21 ByteBuf in (ByteBuf)msg; 22 System.out.print(in.toString(CharsetUtil.UTF_8)); 23 } finally { 24 // 抛弃收到的数据 25 ReferenceCountUtil.release(msg); 26 } 27 28 // ctx.write(msg); 29 // ctx.flush(); 30 } 31 32 /** 33 * 当Netty由于IO错误或者处理器在处理事件时抛出异常时调用 34 */ 35 Override 36 public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { 37 // 当出现异常就关闭连接 38 cause.printStackTrace(); 39 ctx.close(); 40 } 41 }该类主要是实现了接收客户端发来的消息并输出到控制台。2. 然后我们就能实现一个简单的服务端程序了Netty 服务器的通信步骤为创建两个NIO线程组一个专门用于接收来自客户端的连接另一个则用于处理已经被接收的连接。创建一个ServerBootstrap对象配置Netty的一系列参数例如接受传出数据的缓存大小等。创建一个用于实际处理数据的类ChannelInitializer进行初始化的准备工作比如设置接受传出数据的字符集、格式以及实际处理数据的接口。绑定端口执行同步阻塞方法等待服务器端启动即可。1 package com.coder.server; 2 3 import io.netty.bootstrap.ServerBootstrap; 4 import io.netty.channel.ChannelFuture; 5 import io.netty.channel.ChannelInitializer; 6 import io.netty.channel.ChannelOption; 7 import io.netty.channel.EventLoopGroup; 8 import io.netty.channel.nio.NioEventLoopGroup; 9 import io.netty.channel.socket.SocketChannel; 10 import io.netty.channel.socket.nio.NioServerSocketChannel; 11 12 public class HelloServer { 13 private int port; 14 15 public HelloServer(int port) { 16 this.port port; 17 } 18 19 public void run() throws Exception { 20 EventLoopGroup bossGroup new NioEventLoopGroup(); // 用来接收进来的连接 21 EventLoopGroup workerGroup new NioEventLoopGroup(); // 用来处理已经被接收的连接 22 System.out.println(准备运行端口 port); 23 24 try { 25 ServerBootstrap b new ServerBootstrap(); 26 b.group(bossGroup, workerGroup) 27 .channel(NioServerSocketChannel.class) // 这里告诉Channel如何接收新的连接 28 .childHandler( new ChannelInitializerSocketChannel() { 29 Override 30 protected void initChannel(SocketChannel ch) throws Exception { 31 // 自定义处理类 32 ch.pipeline().addLast(new HelloServerHandler()); 33 } 34 }) 35 .option(ChannelOption.SO_BACKLOG, 128) 36 .childOption(ChannelOption.SO_KEEPALIVE, true); 37 38 // 绑定端口开始接收进来的连接 39 ChannelFuture f b.bind(port).sync(); 40 41 // 等待服务器socket关闭 42 f.channel().closeFuture().sync(); 43 } catch (Exception e) { 44 workerGroup.shutdownGracefully(); 45 bossGroup.shutdownGracefully(); 46 } 47 } 48 49 public static void main(String[] args) throws Exception { 50 int port 10110; 51 new HelloServer(port).run(); 52 } 53 54 }那么这便是一个可执行的服务端程序了。运行后控制台输出如下三、测试我们可以去自定义客户端程序这里为了方便使用 telnet 充当客户端。需要注意的是Windows 默认是没有开启 telnet 客户端的需要我们手动开启。菜单-控制面板-程序-打开或关闭Windows功能设置如图这样我们就能用 telnet 客户端了。在 cmd 窗口输入命令如下打开窗口如下这时候我们在该窗口输入什么eclipse 控制台也会对应输出相应内容。如下注意telnet 窗口会看不到输入的字符只要输入 ctrlL 就可以看到输入的字符了。