Demo实例

编码步骤:

当客户端连接时,会通过ServerSocketChannel 得到 SocketChannel,Selector 进行监听 select 方法, 返回有事件发生的通道的个数.

将socketChannel注册到Selector上, register(Selector sel, int ops), 一个selector上可以注册多个SocketChannel,注册后返回一个 SelectionKey, 会和该Selector 关联(集合),进一步得到各个 SelectionKey (有事件发生)
在通过 SelectionKey 反向获取 SocketChannel , 方法 channel()
判断该Channel的事件类型,对不同事件进行不同的业务处理

NIO入门案例:实现服务器和客户端的简单通讯

  1. @Test
  2. public void Server() throws IOException {
  3. //创建ServerSocketChannel -> ServerSocket
  4. ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
  5. //得到一个Selector对象
  6. Selector selector = Selector.open();
  7. //绑定一个端口6666
  8. serverSocketChannel.socket().bind(new InetSocketAddress(6666));
  9. //设置非阻塞
  10. serverSocketChannel.configureBlocking(false);
  11. //把 serverSocketChannel 注册到 selector ,关心事件为:OP_ACCEPT,有新的客户端连接
  12. SelectionKey register = serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
  13. System.out.println();
  14. //循环等待客户端连接
  15. while (true) {
  16. //等待1秒,如果没有事件发生,就返回
  17. if (selector.select(1000) == 0) {
  18. System.out.println("服务器等待了1秒,无连接");
  19. continue;
  20. }
  21. //如果返回的 > 0,表示已经获取到关注的事件
  22. // 就获取到相关的 selectionKey 集合,反向获取通道
  23. Set<SelectionKey> selectionKeys = selector.selectedKeys();
  24. //遍历 Set<SelectionKey>,使用迭代器遍历
  25. Iterator<SelectionKey> keyIterator = selectionKeys.iterator();
  26. while (keyIterator.hasNext()) {
  27. //获取到SelectionKey
  28. SelectionKey key = keyIterator.next();
  29. //根据 key 对应的通道发生的事件,做相应的处理
  30. if (key.isAcceptable()) {//如果是 OP_ACCEPT,有新的客户端连接
  31. //该客户端生成一个 SocketChannel
  32. SocketChannel socketChannel = serverSocketChannel.accept();
  33. System.out.println("客户端连接成功,生成了一个SocketChannel:" + socketChannel.hashCode());
  34. //将SocketChannel设置为非阻塞
  35. socketChannel.configureBlocking(false);
  36. //将socketChannel注册到selector,关注事件为 OP_READ,同时给SocketChannel关联一个Buffer
  37. socketChannel.register(selector, SelectionKey.OP_READ, ByteBuffer.allocate(1024));
  38. }
  39. if (key.isReadable()) {
  40. //通过key,反向获取到对应的Channel
  41. SocketChannel channel = (SocketChannel) key.channel();
  42. //获取到该channel关联的Buffer
  43. ByteBuffer buffer = (ByteBuffer) key.attachment();
  44. channel.read(buffer);
  45. System.out.println("from 客户端:" + new String(buffer.array()));
  46. }
  47. //手动从集合中移除当前的 selectionKey,防止重复操作
  48. keyIterator.remove();
  49. }
  50. }
  51. }
  1. @Test
  2. public void Client() throws IOException {
  3. //得到一个网络通道
  4. SocketChannel socketChannel = SocketChannel.open();
  5. //设置非阻塞
  6. socketChannel.configureBlocking(false);
  7. //提供服务器端的IP和端口
  8. InetSocketAddress socketAddress = new InetSocketAddress("127.0.0.1", 6666);
  9. //连接服务器
  10. if (!socketChannel.connect(socketAddress)){ //如果不成功
  11. while (!socketChannel.finishConnect()){
  12. System.out.println("因为连接需要时间,客户端不会阻塞,可以做其他工作。。。");
  13. }
  14. }
  15. //如果连接成功,就发送数据
  16. String str = "hello, 尚硅谷";
  17. ByteBuffer byteBuffer = ByteBuffer.wrap(str.getBytes());
  18. //发送数据,实际上就是将buffer数据写入到channel
  19. socketChannel.write(byteBuffer);
  20. System.in.read();
  21. }

群聊系统Demo

需要实现客户端和服务器端之间的数据通讯,服务端能够将数据转发给其他所有客户端。

  1. /********************服务端********************/
  2. public class GroupChatServer {
  3. //定义属性
  4. private Selector selector;
  5. private ServerSocketChannel listenChannel;
  6. private static final int PORT = 6666;
  7. //构造器
  8. //初始化工作
  9. public GroupChatServer() {
  10. try {
  11. //得到选择器
  12. selector = Selector.open();
  13. listenChannel = ServerSocketChannel.open();
  14. //绑定端口
  15. listenChannel.socket().bind(new InetSocketAddress(PORT));
  16. //设置非阻塞模式
  17. listenChannel.configureBlocking(false);
  18. //将listenChannel注册到selector,绑定监听事件
  19. listenChannel.register(selector, SelectionKey.OP_ACCEPT);
  20. } catch (IOException e) {
  21. e.printStackTrace();
  22. }
  23. }
  24. //监听
  25. public void listen() {
  26. try {
  27. //循环处理
  28. while (true) {
  29. int count = selector.select();
  30. if (count > 0) { //有事件处理
  31. //遍历得到SelectionKey集合
  32. Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
  33. while (iterator.hasNext()) {
  34. //取出SelectionKey
  35. SelectionKey key = iterator.next();
  36. //监听到accept,连接事件
  37. if (key.isAcceptable()) {
  38. SocketChannel socketChannel = listenChannel.accept();
  39. //将该channel设置非阻塞并注册到selector
  40. socketChannel.configureBlocking(false);
  41. socketChannel.register(selector, SelectionKey.OP_READ);
  42. //提示
  43. System.out.println(socketChannel.getRemoteAddress() + " 上线...");
  44. }
  45. if (key.isReadable()) { //通道可以读取数据,即server端收到客户端的消息,
  46. //处理读(专门写方法)
  47. readData(key);
  48. }
  49. iterator.remove();
  50. }
  51. } else {
  52. System.out.println("等待。。。");
  53. }
  54. }
  55. } catch (Exception e) {
  56. e.printStackTrace();
  57. }
  58. }
  59. //读取客户端消息
  60. private void readData(SelectionKey key) {
  61. //定义一个SocketChannel
  62. SocketChannel channel = null;
  63. try {
  64. //取到关联的channel
  65. channel = (SocketChannel) key.channel();
  66. //创建缓冲buffer
  67. ByteBuffer buffer = ByteBuffer.allocate(1024);
  68. int count = channel.read(buffer);
  69. //根据count值判断是否读取到数据
  70. if (count > 0) {
  71. //把缓冲区的数据转成字符串
  72. String msg = new String(buffer.array());
  73. //输出该消息
  74. System.out.println("from 客户端:" + msg);
  75. //向其他的客户端转发消息(去掉自己),专门写一个方法处理
  76. sendInfoToOtherClients(msg, channel);
  77. }
  78. } catch (IOException e) {
  79. try {
  80. System.out.println(channel.getRemoteAddress() + "离线了...");
  81. //取消注册
  82. key.cancel();
  83. //关闭通道
  84. channel.close();
  85. } catch (IOException ex) {
  86. ex.printStackTrace();
  87. }
  88. }
  89. }
  90. //转发消息给其他客户端(通道)
  91. private void sendInfoToOtherClients(String msg, SocketChannel self) throws IOException {
  92. System.out.println("服务器转发消息中。。。");
  93. //遍历 所有注册到selector上的SocketChannel,并排除self
  94. for (SelectionKey key : selector.keys()) {
  95. //通过key取出对应的SocketChannel
  96. Channel targetChannel = key.channel();
  97. //排除自己
  98. if (targetChannel instanceof SocketChannel && targetChannel != self) {
  99. //转型
  100. SocketChannel dest = (SocketChannel) targetChannel;
  101. //将msg,存储到buffer
  102. ByteBuffer buffer = ByteBuffer.wrap(msg.getBytes());
  103. //将buffer的数据写入通道
  104. dest.write(buffer);
  105. }
  106. }
  107. }
  108. public static void main(String[] args) {
  109. //创建服务器对象
  110. GroupChatServer groupChatServer = new GroupChatServer();
  111. groupChatServer.listen();
  112. }
  113. }
  1. /*****************************客户端**********************/
  2. public class GroupChatClient {
  3. //定义相关的属性
  4. private static final String HOST = "127.0.0.1"; //服务器的IP地址
  5. private static final int PORT = 6666; //服务器端口
  6. private Selector selector;
  7. private SocketChannel socketChannel;
  8. private String username;
  9. //构造器,初始化操作
  10. public GroupChatClient() throws IOException {
  11. selector = Selector.open();
  12. //连接服务器
  13. socketChannel = SocketChannel.open(new InetSocketAddress(HOST, PORT));
  14. //设置非阻塞
  15. socketChannel.configureBlocking(false);
  16. //将channel注册到selector
  17. socketChannel.register(selector, SelectionKey.OP_READ);
  18. //得到username
  19. username = socketChannel.getLocalAddress().toString().substring(1);
  20. System.out.println(username + " is ok...");
  21. }
  22. //向服务器发送消息
  23. public void sendInfo(String info){
  24. info = username + " 说:" + info;
  25. try {
  26. socketChannel.write(ByteBuffer.wrap(info.getBytes()));
  27. }catch (IOException e){
  28. e.printStackTrace();
  29. }
  30. }
  31. //读取从服务器端回复的消息
  32. public void readInfo(){
  33. try {
  34. int readChannels = selector.select();
  35. if (readChannels > 0){//有可用的通道
  36. Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
  37. while (iterator.hasNext()){
  38. SelectionKey key = iterator.next();
  39. if (key.isReadable()){
  40. //得到相关的通道
  41. SocketChannel sc = (SocketChannel)key.channel();
  42. //得到一个buffer
  43. ByteBuffer buf = ByteBuffer.allocate(1024);
  44. //读取
  45. sc.read(buf);
  46. //把缓冲区的数据转成字符串
  47. String msg = new String(buf.array());
  48. System.out.println(msg.trim());
  49. }
  50. }
  51. }else {
  52. System.out.println("没有可以用的通道...");
  53. }
  54. }catch (Exception e){
  55. }
  56. }
  57. public static void main(String[] args) throws IOException {
  58. //启动客户端
  59. GroupChatClient chatClient = new GroupChatClient();
  60. //启动一个线程用于读取服务器的消息
  61. new Thread(() -> {
  62. while (true){
  63. chatClient.readInfo();
  64. try {
  65. Thread.sleep(3000);
  66. }catch (InterruptedException e){
  67. e.printStackTrace();
  68. }
  69. }
  70. }).start();
  71. //主线程用于发送数据给服务器端
  72. Scanner scanner = new Scanner(System.in);
  73. while (scanner.hasNextLine()) {
  74. String s = scanner.nextLine();
  75. chatClient.sendInfo(s);
  76. }
  77. }
  78. }

注意事项:

使用int read = channel.read(buffer)读取数据时,读取的结果情况:
当read=-1时,说明客户端的数据发送完毕,并且主动的关闭socket。所以这种情况下,服务器程序需要关闭socketSocket,并且取消key的注册。注意:这个时候继续使用SocketChannel进行读操作的话,就会抛出:==远程主机强迫关闭一个现有的连接==的IO异常
当read=0时:
某一时刻SocketChannel中当前没有数据可读。
客户端的数据发送完毕。
详情见此博文
但是对于博文中的这一条,经过本人测试,这种情况下返回的是读取的数据的大小,而不是0:ByteBuffer的position等于limit,这个时候也会返回0