Chapter1 章节结构

NIO 特点                                 OIO 比较

ServerSocketChannel    -->  ServerSocket
SocketChannel              --> Socket
Selector (多路复用)

SelectionKey

特点:一个服务员服务多个客户,主要通过Selector多路服务

代码:

package com.john.netty.learn.ch01.nio;import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;public class NioServer {private Selector selector = null;private int port;public NioServer(int port) throws IOException {ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();// 不堵塞serverSocketChannel.configureBlocking(false);serverSocketChannel.socket().bind(new InetSocketAddress(port));selector = Selector.open();// 注册我感兴趣的事件, 这里 Accept 新的客户接入serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);this.port = port;}public void listen() throws IOException {System.out.println("启动服务" + port);while (true) {selector.select();Iterator<SelectionKey> it = selector.selectedKeys().iterator();while (it.hasNext()) {SelectionKey selectionKey = it.next();// 避免重复it.remove();handler(selectionKey);}}}private void handler(SelectionKey selectionKey) throws IOException {//当触发Socket.accpet 事件时,即有新的客户连接if (selectionKey.isAcceptable()) {// Key 包含 serverSocketChannel// Refer to serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);handlerAccept(selectionKey);return;}if (selectionKey.isReadable()) {handlerRead(selectionKey);}}private void handlerRead(SelectionKey selectionKey) throws IOException {SocketChannel socketChannel = (SocketChannel) selectionKey.channel();ByteBuffer buffer = ByteBuffer.allocate(1024);socketChannel.read(buffer);System.out.println("接收到消息:" + new String(buffer.array()));buffer.flip();socketChannel.write(buffer);}private void handlerAccept(SelectionKey selectionKey) throws IOException {ServerSocketChannel serverSocketChannel = (ServerSocketChannel) selectionKey.channel();// 获取新的客户端-与新客户端握手SocketChannel socketChannel = serverSocketChannel.accept();socketChannel.configureBlocking(false);System.out.println("一个新客户连入...");// 注册SocketChannel 读的感兴趣socketChannel.register(this.selector, SelectionKey.OP_READ);}public static void main(String[] args) throws IOException {NioServer nioServer = new NioServer(2000);nioServer.listen();}}

问题解答:

1. Selector.select();阻塞,那为什么说NIO是非阻塞的IO?
  a. 一个Socket系统对于阻塞和非阻塞,主要关心的不是在Accept 、Select 上, 而是在 read socket 流的时候,是否堵塞。
  b. selector.select(1000);不阻塞
     selector.wakeup();也可以唤醒selector (select方法) -- 这个非常重要,再随后的Netty源码中将讲解
     selector.selectNow();也可以立马返还

2、客户端关闭的时候会抛出异常,死循环

3、SelectionKey.OP_WRITE是代表什么意思?
   OP_WRITE表示底层缓冲区是否有空间,是则响应返还true

所有源码下载 :https://download.csdn.net/download/netcobol/10308871

Netty 快速入门系列 - Chapter 1 传统OIO与NIO - NIO 【第二讲】相关推荐

  1. c# wpf listbox 高度_WPF快速入门系列(1)——WPF布局概览

    一.引言 关于WPF早在一年前就已经看过<深入浅出WPF>这本书,当时看完之后由于没有做笔记,以至于我现在又重新捡起来并记录下学习的过程,本系列将是一个WPF快速入门系列,主要介绍WPF中 ...

  2. WPF快速入门系列(6)——WPF资源和样式

    WPF快速入门系列(6)--WPF资源和样式 一.引言 WPF资源系统可以用来保存一些公有对象和样式,从而实现重用这些对象和样式的作用.而WPF样式是重用元素的格式的重要手段,可以理解样式就如CSS一 ...

  3. python r转义_Python快速入门系列之二:还学不会我直播跪搓衣板

    Python作为一个,目前最火的编程语言之一,已经渗透到了各行各业.它易学好懂,拥有着丰富的库,功能齐全.人生苦短,就用Python. 这个快速入门系列分为六篇,包含了Python大部分基础知识,每篇 ...

  4. 2021-08-26 转载 Scala快速入门系列博客文章

    作者:Huidoo_Yang 出处:http://www.cnblogs.com/yangp/ 本文版权归作者Huidoo_Yang和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面 ...

  5. 树莓派从零开始快速入门系列汇总

    树莓派从零开始快速入门系列汇总 树莓派从零开始快速入门第0讲--环境安装 树莓派从零开始快速入门第1讲--命令行 树莓派从零开始快速入门第2讲--更换国内源 树莓派从零开始快速入门第3讲--文件编辑 ...

  6. 【物体检测快速入门系列 | 01 】基于Tensorflow2.x Object Detection API构建自定义物体检测器

    这是机器未来的第1篇文章 原文首发地址:https://blog.csdn.net/RobotFutures/article/details/124745966 CSDN话题挑战赛第1期 活动详情地址 ...

  7. NumPy 快速入门系列:应用统计学基础概念、相关统计指标与NumPy的实现

    NumPy 快速入门系列:应用统计学基础概念.相关统计指标与NumPy的实现 前言: 统计学导论: 统计学定义: 统计学分类: 统计学基本概念: 统计过程: 统计指标与NumPy: 用 Python ...

  8. RHEL8.0快速入门系列笔记--理论知识储备(一)

    RHEL8.0快速入门系列笔记–理论知识储备(一) 红帽公司发布Linux8.0系统已经有一段时间,最近准备学习关于RHEL8.0的相关新特性.根据官方介绍:RHEL8.0在云/容器化工作负载方面做了 ...

  9. 【Python零基础快速入门系列 | 03】AI数据容器底层核心之Python列表

    • 这是机器未来的第7篇文章 原文首发地址:https://blog.csdn.net/RobotFutures/article/details/124957520 <Python零基础快速入门 ...

最新文章

  1. android 登录组件开发,Android组件化开发路由的设计
  2. 华为交换机同步linux时间服务器,华为s5720s系列交换机同步时间
  3. 基于OpenCL的mean filter性能
  4. Intel Hex概述
  5. 实现类似shared_ptr的引用计数
  6. AS+图灵机器人官网+HTTP POST(json)+JsonReader实现安卓课设《智能聊天机器人》填坑记录
  7. 观李永乐老师讲音律有感——《管子·地员》之“三分损益法”的探究
  8. linux下通过inode删除文件
  9. 设计模式二 单例模式
  10. 云数智驱动数据高速增长,浪潮存储提供EB级容量扩展
  11. [转]伽利略卫星导航系统2019年7月14日起的宕机事件
  12. navicat 解析sql_使用 Navicat 查询分析器优化查询性能(第 1 部分)
  13. 数据结构与算法--递归(factorial)
  14. 智慧的提问(不做慵懒的寄生虫)
  15. SAP_PS常用增强
  16. 用python玩转数据第一周答案_用Python玩转数据_答案
  17. python安装talib
  18. 设计要解决什么问题?
  19. python装饰器模式带参数_python函数装饰器、类装饰器和带参数的装饰器——装饰器模式...
  20. 深圳有哪些学计算机的学校,深圳计算机专业学校

热门文章

  1. macOS SwiftUI 指示器组件规范之 01 液位指示器Level Indicators
  2. C++入门——Day5_分支语句和逻辑运算符
  3. Bulletproof零知识证明
  4. 使用 Excel 中的函数准确计算周岁年龄
  5. 配置NSG限制VM访问Internet
  6. HEG安装教程(windows平台)
  7. 坑了程序员的政府项目是什么样的?
  8. 算法笔记 A+B 输入输出练习VIII
  9. [Pixhawk] ardupilot源码windows编译教程
  10. 支付平台--网联详解