NIO Socket非阻塞模式是什么

发布时间:2021-12-21 14:20:13 作者:iii
来源:亿速云 阅读:123

本篇内容主要讲解“NIO Socket非阻塞模式是什么”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“NIO Socket非阻塞模式是什么”吧!

NIO主要原理和适用

NIO 有一个主要的类Selector,这个类似一个观察者,只要我们把需要探知的socketchannel告诉Selector,我们接着做别的事情,当有 事件发生时,他会通知我们,传回一组SelectionKey,我们读取这些Key,就会获得我们刚刚注册过的socketchannel,然后,我们从 这个Channel中读取数据,放心,包准能够读到,接着我们可以处理这些数据。

Selector内部原理实际是在做一个对所注册的channel的轮询访问,不断的轮询(目前就这一个算法),一旦轮询到一个channel有所注册的事情发生,比如数据来了,他就会站起来报告,交出一把钥匙,让我们通过这把钥匙来读取这个channel的内容。

jdk供的无阻塞I/O(NIO)有效解决了多线程服务器存在的线程开销问题,但在使用上略显得复杂一些。在NIO中使用多线程,主要目的已不是为了应对 每个客户端请求而分配独立的服务线程,而是通过多线程充分使用用多个CPU的处理能力和处理中的等待时间,达到提高服务能力的目的。

这段时间在研究NIO,写篇博客来记住学过的东西。还是从最简单的Hello World开始,client多线程请求server端,server接收client的名字,并返回Hello! +名字的字符格式给client。当然实际应用并不这么简单,实际可能是访问文件或者数据库获取信息返回给client。非阻塞的NIO有何神秘之处?

代 码:

1)server端代码

public class HelloWorldServer {        static int BLOCK = 1024;       static String name = "";       protected Selector selector;       protected ByteBuffer clientBuffer = ByteBuffer.allocate(BLOCK);       protected CharsetDecoder decoder;       static CharsetEncoder encoder = Charset.forName("GB2312").newEncoder();        public HelloWorldServer(int port) throws IOException {           selector = this.getSelector(port);           Charset charset = Charset.forName("GB2312");           decoder = charset.newDecoder();       }        // 获取Selector       protected Selector getSelector(int port) throws IOException {           ServerSocketChannel server = ServerSocketChannel.open();           Selector sel = Selector.open();           server.socket().bind(new InetSocketAddress(port));           server.configureBlocking(false);           server.register(sel, SelectionKey.OP_ACCEPT);           return sel;       }        // 监听端口       public void listen() {           try {               for (;;) {                   selector.select();                   Iterator iter = selector.selectedKeys().iterator();                   while (iter.hasNext()) {                       SelectionKey key = (SelectionKey) iter.next();                       iter.remove();                       process(key);                   }               }           } catch (IOException e) {               e.printStackTrace();           }       }        // 处理事件       protected void process(SelectionKey key) throws IOException {           if (key.isAcceptable()) { // 接收请求               ServerSocketChannel server = (ServerSocketChannel) key.channel();               SocketChannel channel = server.accept();               //设置非阻塞模式               channel.configureBlocking(false);               channel.register(selector, SelectionKey.OP_READ);           } else if (key.isReadable()) { // 读信息               SocketChannel channel = (SocketChannel) key.channel();               int count = channel.read(clientBuffer);               if (count > 0) {                   clientBuffer.flip();                   CharBuffer charBuffer = decoder.decode(clientBuffer);                   name = charBuffer.toString();                   // System.out.println(name);                   SelectionKey sKey = channel.register(selector,                           SelectionKey.OP_WRITE);                   sKey.attach(name);               } else {                   channel.close();               }                clientBuffer.clear();           } else if (key.isWritable()) { // 写事件               SocketChannel channel = (SocketChannel) key.channel();               String name = (String) key.attachment();                              ByteBuffer block = encoder.encode(CharBuffer                       .wrap("Hello !" + name));                               channel.write(block);                //channel.close();            }       }        public static void main(String[] args) {           int port = 8888;           try {               HelloWorldServer server = new HelloWorldServer(port);               System.out.println("listening on " + port);                              server.listen();                          } catch (IOException e) {               e.printStackTrace();           }       }   }

2)client端代码

public class HelloWorldClient {        static int SIZE = 10;       static InetSocketAddress ip = new InetSocketAddress("localhost", 8888);       static CharsetEncoder encoder = Charset.forName("GB2312").newEncoder();        static class Message implements Runnable {           protected String name;           String msg = "";            public Message(String index) {               this.name = index;           }            public void run() {               try {                   long start = System.currentTimeMillis();                   //打开Socket通道                   SocketChannel client = SocketChannel.open();                   //设置为非阻塞模式                   client.configureBlocking(false);                   //打开选择器                   Selector selector = Selector.open();                   //注册连接服务端socket动作                   client.register(selector, SelectionKey.OP_CONNECT);                   //连接                   client.connect(ip);                   //分配内存                   ByteBuffer buffer = ByteBuffer.allocate(8 * 1024);                   int total = 0;                    _FOR: for (;;) {                       selector.select();                       Iterator iter = selector.selectedKeys().iterator();                        while (iter.hasNext()) {                           SelectionKey key = (SelectionKey) iter.next();                           iter.remove();                           if (key.isConnectable()) {                               SocketChannel channel = (SocketChannel) key                                       .channel();                               if (channel.isConnectionPending())                                   channel.finishConnect();                               channel                                       .write(encoder                                               .encode(CharBuffer.wrap(name)));                                channel.register(selector, SelectionKey.OP_READ);                           } else if (key.isReadable()) {                               SocketChannel channel = (SocketChannel) key                                       .channel();                               int count = channel.read(buffer);                               if (count > 0) {                                   total += count;                                   buffer.flip();                                    while (buffer.remaining() > 0) {                                       byte b = buffer.get();                                       msg += (char) b;                                                                          }                                    buffer.clear();                               } else {                                   client.close();                                   break _FOR;                               }                           }                       }                   }                   double last = (System.currentTimeMillis() - start) * 1.0 / 1000;                   System.out.println(msg + "used time :" + last + "s.");                   msg = "";               } catch (IOException e) {                   e.printStackTrace();               }           }       }        public static void main(String[] args) throws IOException {                  String names[] = new String[SIZE];            for (int index = 0; index < SIZE; index++) {               names[index] = "jeff[" + index + "]";               new Thread(new Message(names[index])).start();           }              }   }

到此,相信大家对“NIO Socket非阻塞模式是什么”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

推荐阅读:
  1. java中nio和io的区别是什么
  2. Java NIO核心组件有哪些

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

socket nio

上一篇:Arduino如何实现上传运行程序

下一篇:怎么将WordPress远程附件存储到云对象存COS上

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》