狠狠撸

狠狠撸Share a Scribd company logo
初窥 Java 网络 IO

          DigitalSonic
             2011-10
同步?异步?阻塞?非阻塞?




UNIX? Network Programming Volume 1, Third Edition: The Sockets Networking
IO in Java
●   JDK 1.4 —— NIO
    ●   我们有了 Non-Blocking IO
●   JDK 1.7—— NIO2
    ●   我们有了 Asynchronous IO
    ●   Linux 下通过 epoll 模拟实现
    ●   Windows 下通过 IOCP 实现
传统 Socket 服务器实现方式
●   用 ServerSocket 绑定某个端口
●   循环 accept
    ●   开启一个新线程处理 Socket
    ●   处理完毕后关闭 Socket
NIO – How To
●   非阻塞 ServerSocketChannel 绑定端口
●   注册选择器
●   循环 select
    ●   处理 select 的结果 key
    ●   处理完毕移除 key
NIO 范例
NIO 范例
AIO – How To
●   配置线程池
●   AsynchronousServerSocketChannel 绑定端口
●   accept
    ●   返回 Future 做进一步处理
    ●   设定 CompletionHandler ,在其中做进一步处理
AIO 范例
AIO 范例
AIO 范例
AIO 范例




Start listening on 8888.
sun.nio.ch.UnixAsynchronousServerSocketChannelImpl[/0:0:0:0:0:0:0:0:8888]
AsynchronousSocketChannel Accepted!sun.nio.ch.UnixAsynchronousSocketChannelImpl
[connected local=/0:0:0:0:0:0:0:1:8888 remote=/0:0:0:0:0:0:0:1:48716]
sun.nio.ch.UnixAsynchronousSocketChannelImpl
[connected local=/0:0:0:0:0:0:0:1:8888 remote=/0:0:0:0:0:0:0:1:48716] said: abc
sun.nio.ch.UnixAsynchronousSocketChannelImpl
[connected local=/0:0:0:0:0:0:0:1:8888 remote=/0:0:0:0:0:0:0:1:48716] said: exit

More Related Content

初窥闯补惫补网络滨翱

  • 1. 初窥 Java 网络 IO DigitalSonic 2011-10
  • 2. 同步?异步?阻塞?非阻塞? UNIX? Network Programming Volume 1, Third Edition: The Sockets Networking
  • 3. IO in Java ● JDK 1.4 —— NIO ● 我们有了 Non-Blocking IO ● JDK 1.7—— NIO2 ● 我们有了 Asynchronous IO ● Linux 下通过 epoll 模拟实现 ● Windows 下通过 IOCP 实现
  • 4. 传统 Socket 服务器实现方式 ● 用 ServerSocket 绑定某个端口 ● 循环 accept ● 开启一个新线程处理 Socket ● 处理完毕后关闭 Socket
  • 5. NIO – How To ● 非阻塞 ServerSocketChannel 绑定端口 ● 注册选择器 ● 循环 select ● 处理 select 的结果 key ● 处理完毕移除 key
  • 8. AIO – How To ● 配置线程池 ● AsynchronousServerSocketChannel 绑定端口 ● accept ● 返回 Future 做进一步处理 ● 设定 CompletionHandler ,在其中做进一步处理
  • 12. AIO 范例 Start listening on 8888. sun.nio.ch.UnixAsynchronousServerSocketChannelImpl[/0:0:0:0:0:0:0:0:8888] AsynchronousSocketChannel Accepted!sun.nio.ch.UnixAsynchronousSocketChannelImpl [connected local=/0:0:0:0:0:0:0:1:8888 remote=/0:0:0:0:0:0:0:1:48716] sun.nio.ch.UnixAsynchronousSocketChannelImpl [connected local=/0:0:0:0:0:0:0:1:8888 remote=/0:0:0:0:0:0:0:1:48716] said: abc sun.nio.ch.UnixAsynchronousSocketChannelImpl [connected local=/0:0:0:0:0:0:0:1:8888 remote=/0:0:0:0:0:0:0:1:48716] said: exit