NIO core components and network programming (b)

Selector (selector)

basic introduction

  • Java's NIO, IO mode with non-blocking. One thread can handle multiple client connections, will be used to Selector (selector)
  • Selector is able to detect whether there are multiple channels registered events (Note: Multiple Channel as an event can be registered with a Selector), if an event occurs, the event will then get the appropriate treatment for each event. You can use only a single thread to manage a plurality of channels, i.e. managing multiple connections and requests.
  • Only when connecting / write channel real events, will read and write, greatly reducing system overhead, and do not have to create a thread for each connection, you do not have to maintain multiple threads
  • Avoiding the overhead of context switching between multiple threads of lead

Selector schematic description and characteristics
Here Insert Picture Description

  • Netty IO thread NioEventLoop polymerized Selector (selection unit, also called a multiplexer) can handle hundreds of concurrent client connections.
  • When data from one thread to read and write the client Socket channel, if no data is available, the thread can perform other tasks.
  • The threads are usually non-blocking IO idle time for performing IO operations on other channels, so a separate thread to manage a plurality of input and output channels.
  • Since the read and write operations are non-blocking, which can fully enhance the efficiency of IO threads, to avoid the frequent I / O thread is suspended due to obstruction.
  • An I / O threads may concurrently process N client connections and read and write operations, which fundamentally solve the conventional synchronous blocking I / O is connected to a threading model a performance architecture, elastically stretchable and reliability have been extremely big improvement.

Selector class correlation method

  • Common method
    public abstract class Selector implements Closeable { 
    	public static Selector open();//得到一个选择器对象
    	//监控所有注册的通道,当其中有 IO 操作可以进行时,将对应的 SelectionKey 加入到内部集合中并返回,参数用来设置超时时间
    	public int select(long timeout);
    	public Set<SelectionKey> selectedKeys();//从内部集合中得到所有的 SelectionKey	
    }
    
  • Supplement
    • selector.select () // blocked
    • selector.select (1000); // 1000 milliseconds obstruction, returned after 1000 milliseconds
    • selector.wakeup (); // wake
    • selectorselector.selectNow (); // do not block, return immediately

NIO non-blocking network programming principles

step

  • establish connection

    • When the client connection will be obtained by ServerSocketChannel SocketChannel
    • Number Selector listens select method returns an event occurs in channel
    • SocketChannel registered to the Selector, register (Selectorsel, intops), can be registered on a plurality of selector SocketChannel
    • After registering a return SelectionKey, and the association will Selector (collection)
  • Reading and writing events

    • Further each SelectionKey (an event occurs)
    • Obtaining by reverse SocketChannel SelectionKey, the method Channel ()
    • Channel can be obtained, to complete the business process
  • Program: https: //blog.csdn.net/weixin_43934607/article/details/104417876

SelectionKey

  • Sign up networks and channels

    • int OP_ACCEPT: new network connection can accept, is 16
    • int OP_CONNECT: Representative connection has been established, the value 8
    • int OP_READ: for read operations, the value of 1
    • int OP_WRITE: Representative write operation, a value of 4
  • Related Methods

    public abstract class SelectionKey {
         public abstract Selector selector();//得到与之关联的 Selector 对象
         public abstract SelectableChannel channel();//得到与之关联的通道
         public final Object attachment();//得到与之关联的共享数据
         public abstract SelectionKey interestOps(int ops);//设置或改变监听事件
         public final boolean isAcceptable();//是否可以 accept
         public final boolean isReadable();//是否可以读
         public final boolean isWritable();//是否可以写
    }
    

ServerSocketChannel
on the server side Socket listen for new client connections

  • Related Methods
    public abstract class ServerSocketChannel extends AbstractSelectableChannel  implements NetworkChannel{
    	public static ServerSocketChannel open()//得到一个 ServerSocketChannel 通道
    	public final ServerSocketChannel bind(SocketAddress local)//设置服务器端端口号
    	public final SelectableChannel configureBlocking(boolean block)//设置阻塞或非阻塞模式,取值 false 表示采用非阻塞模式
    	public SocketChannel accept()//接受一个连接,返回代表这个连接的通道对象
    	public final SelectionKey register(Selector sel, int ops)//注册一个选择器并设置监听事件
    }
    

SocketChannel
network IO channel, responsible read and write operations (data written to the buffer channel or the channel in the read data buffer)

  • Related Methods
    public abstract class SocketChannel extends AbstractSelectableChannel implements ByteChannel, ScatteringByteChannel, GatheringByteChannel, NetworkChannel{
    	public static SocketChannel open();//得到一个 SocketChannel 通道
    	public final SelectableChannel configureBlocking(boolean block);//设置阻塞或非阻塞模式,取值 false 表示采用非阻塞模式
    	public boolean connect(SocketAddress remote);//连接服务器
    	public boolean finishConnect();//如果上面的方法连接失败,接下来就要通过该方法完成连接操作
    	public int write(ByteBuffer src);//往通道里写数据
    	public int read(ByteBuffer dst);//从通道里读数据
    	public final SelectionKey register(Selector sel, int ops, Object att);//注册一个选择器并设置监听事件,最后一个参数可以设置共享数据
    	public final void close();//关闭通道
    }
    

NIO group chat system

  • GroupChatClient
    public class GroupChatClient {
    
        //定义相关的属性
        private final String HOST = "127.0.0.1"; // 服务器的ip
        private final int PORT = 6667; //服务器端口
        private Selector selector;
        private SocketChannel socketChannel;
        private String username;
    
        //构造器, 完成初始化工作
        public GroupChatClient() throws IOException {
    
            selector = Selector.open();
            //连接服务器
            socketChannel = socketChannel.open(new InetSocketAddress("127.0.0.1", PORT));
            //设置非阻塞
            socketChannel.configureBlocking(false);
            //将channel 注册到selector
            socketChannel.register(selector, SelectionKey.OP_READ);
            //得到username
            username = socketChannel.getLocalAddress().toString().substring(1);
            System.out.println(username + " is ok...");
    
        }
    
        //向服务器发送消息
        public void sendInfo(String info) {
    
            info = username + " 说:" + info;
    
            try {
                socketChannel.write(ByteBuffer.wrap(info.getBytes()));
            }catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        //读取从服务器端回复的消息
        public void readInfo() {
    
            try {
    
                int readChannels = selector.select();
                if(readChannels > 0) {//有可以用的通道
    
                    Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
                    while (iterator.hasNext()) {
    
                        SelectionKey key = iterator.next();
                        if(key.isReadable()) {
                            //得到相关的通道
                           SocketChannel sc = (SocketChannel) key.channel();
                           //得到一个Buffer
                            ByteBuffer buffer = ByteBuffer.allocate(1024);
                            //读取
                            sc.read(buffer);
                            //把读到的缓冲区的数据转成字符串
                            String msg = new String(buffer.array());
                            System.out.println(msg.trim());
                        }
                    }
                    iterator.remove(); //删除当前的selectionKey, 防止重复操作
                } else {
                    //System.out.println("没有可以用的通道...");
                }
    
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        public static void main(String[] args) throws Exception {
    
            //启动我们客户端
            GroupChatClient chatClient = new GroupChatClient();
    
            //启动一个线程, 每个3秒,读取从服务器发送数据
            new Thread() {
                public void run() {
    
                    while (true) {
                        chatClient.readInfo();
                        try {
                            Thread.currentThread().sleep(3000);
                        }catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }.start();
    
            //发送数据给服务器端
            Scanner scanner = new Scanner(System.in);
    
            while (scanner.hasNextLine()) {
                String s = scanner.nextLine();
                chatClient.sendInfo(s);
            }
        }
    }
    
  • GroupChatClient
    public class GroupChatServer {
        //定义属性
        private Selector selector;
        private ServerSocketChannel listenChannel;
        private static final int PORT = 6667;
    
        //构造器
        //初始化工作
        public GroupChatServer() {
    
            try {
    
                //得到选择器
                selector = Selector.open();
                //ServerSocketChannel
                listenChannel =  ServerSocketChannel.open();
                //绑定端口
                listenChannel.socket().bind(new InetSocketAddress(PORT));
                //设置非阻塞模式
                listenChannel.configureBlocking(false);
                //将该listenChannel 注册到selector
                listenChannel.register(selector, SelectionKey.OP_ACCEPT);
    
            }catch (IOException e) {
                e.printStackTrace();
            }
    
        }
    
        //监听
        public void listen() {
    
            System.out.println("监听线程: " + Thread.currentThread().getName());
            try {
    
                //循环处理
                while (true) {
    
                    int count = selector.select();
                    if(count > 0) {//有事件处理
    
                        //遍历得到selectionKey 集合
                        Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
                        while (iterator.hasNext()) {
                            //取出selectionkey
                            SelectionKey key = iterator.next();
    
                            //监听到accept
                            if(key.isAcceptable()) {
                                SocketChannel sc = listenChannel.accept();
                                sc.configureBlocking(false);
                                //将该 sc 注册到seletor
                                sc.register(selector, SelectionKey.OP_READ);
    
                                //提示
                                System.out.println(sc.getRemoteAddress() + " 上线 ");
    
                            }else if(key.isReadable()) { //通道发送read事件,即通道是可读的状态
                                //处理读 (专门写方法..)
    
                                readData(key);
    
                            }
                            //当前的key 删除,防止重复处理
                            iterator.remove();
                        }
    
                    } else {
                        System.out.println("等待....");
                    }
                }
    
            }catch (Exception e) {
                e.printStackTrace();
    
            }finally {
                //发生异常处理....
    
            }
        }
    
        //读取客户端消息
        private void readData(SelectionKey key) {
    
            //取到关联的channle
            SocketChannel channel = null;
    
            try {
               //得到channel
                channel = (SocketChannel) key.channel();
                //创建buffer
                ByteBuffer buffer = ByteBuffer.allocate(1024);
    
                int count = channel.read(buffer);
                //根据count的值做处理
                if(count > 0) {
                    //把缓存区的数据转成字符串
                    String msg = new String(buffer.array());
                    //输出该消息
                    System.out.println("form 客户端: " + msg);
    
                    //向其它的客户端转发消息(去掉自己), 专门写一个方法来处理
                    sendInfoToOtherClients(msg, channel);
                }
    
            }catch (IOException e) {
                try {
                    System.out.println(channel.getRemoteAddress() + " 离线了..");
                    //取消注册
                    key.cancel();
                    //关闭通道
                    channel.close();
                }catch (IOException e2) {
                    e2.printStackTrace();;
                }
            }
        }
    
        //转发消息给其它客户(通道)
        private void sendInfoToOtherClients(String msg, SocketChannel self ) throws  IOException{
    
            System.out.println("服务器转发消息中...");
            System.out.println("服务器转发数据给客户端线程: " + Thread.currentThread().getName());
            //遍历 所有注册到selector 上的 SocketChannel,并排除 self
            for(SelectionKey key: selector.keys()) {
    
                //通过 key  取出对应的 SocketChannel
                Channel targetChannel = key.channel();
    
                //排除自己
                if(targetChannel instanceof  SocketChannel && targetChannel != self) {
    
                    //转型
                    SocketChannel dest = (SocketChannel)targetChannel;
                    //将msg 存储到buffer
                    ByteBuffer buffer = ByteBuffer.wrap(msg.getBytes());
                    //将buffer 的数据写入 通道
                    dest.write(buffer);
                }
            }
    
        }
    
        public static void main(String[] args) {
    
            //创建服务器对象
            GroupChatServer groupChatServer = new GroupChatServer();
            groupChatServer.listen();
        }
    }
    
Published 109 original articles · won praise 47 · views 30000 +

Guess you like

Origin blog.csdn.net/weixin_43934607/article/details/104419287