zoukankan      html  css  js  c++  java
  • NIO网络访问模式实践

    1、创建NioNest12类

    一个线程监听5个端口的事件

    public class NioTest12 {
    
        public static void main(String[] args) throws Exception {
    
            int[] ports = new int[5];
            ports[0] = 5000;
            ports[1] = 5001;
            ports[2] = 5002;
            ports[3] = 5003;
            ports[4] = 5004;
    
            Selector selector = Selector.open();
    
            for(int i = 0; i < ports.length; ++i){
                ServerSocketChannel serverSocketChannel =  ServerSocketChannel.open();
                serverSocketChannel.configureBlocking(false);
                ServerSocket serverSocket = serverSocketChannel.socket();
                InetSocketAddress address = new InetSocketAddress(ports[i]);
                serverSocket.bind(address);
                serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
    
                System.out.println("监听端口:" + ports[i]);
    
            }
            while (true){
                int numbers = selector.select();
                System.out.println("numbers:" + numbers);
                Set<SelectionKey> selectionKeys =  selector.selectedKeys();
    
                System.out.println("selectedKeys: " + selectionKeys);
    
                Iterator<SelectionKey> iter = selectionKeys.iterator();
    
                while (iter.hasNext()){
                    SelectionKey selectionKey = iter.next();
                    //判断是否有客户端连接
                    if(selectionKey.isAcceptable()){
                        ServerSocketChannel serverSocketChannel = (ServerSocketChannel)selectionKey.channel();
                        SocketChannel socketChannel = serverSocketChannel.accept();
                        socketChannel.configureBlocking(false);
                        socketChannel.register(selector, SelectionKey.OP_READ);
    
                        iter.remove();
    
                        System.out.println("获得客户端连接:" +socketChannel);
                    }
                    //判断是否有可读的数据
                    else if(selectionKey.isReadable()){
                        SocketChannel socketChannel = (SocketChannel) selectionKey.channel();
    
                        int bytesRead = 0;
                        while (true){
                            ByteBuffer byteBuffer = ByteBuffer.allocate(512);
    
                            int read = socketChannel.read(byteBuffer);
    
                            if(read <= 0){
                                break;
                            }
    
                            byteBuffer.flip();
    
                            socketChannel.write(byteBuffer);
    
                            bytesRead += read;
    
                        }
    
                        System.out.println("读取:" + bytesRead +",  来源于:" + socketChannel);
    
                        iter.remove();
                    }
                }
            }
    
        }
    }
    

      

     启动NioTest12,监听如下五个端口

    使用命令行访问

    2、telnet localhost 5000,并发送hello wold

    输出如下:

     3、telnet localhost 5001,并发送hello wold

     

    输出如下:

  • 相关阅读:
    sgdisk基本用法
    影响性能的关键部分-ceph的osd journal写
    SSD固态盘应用于Ceph集群的四种典型使用场景
    Ceph在OpenStack中的地位
    Ceph的工作原理及流程
    Ceph系统的层次结构
    SharePoint运行状况分析器有关磁盘空间不足的警告
    Microsoft Exchange本地和Exchange Online可以与第三方服务共享
    SharePoint Server 2016 WEB 网站浏览器支持
    禁用自动映射的 Exchange 邮箱
  • 原文地址:https://www.cnblogs.com/linlf03/p/11368576.html
Copyright © 2011-2022 走看看