1 public void selector() throws IOException { 2 ByteBuffer buffer = ByteBuffer.allocate(1024); 3 Selector selector = Selector.open(); 4 ServerSocketChannel ssc = ServerSocketChannel.open(); 5 ssc.configureBlocking(false);//设置为非阻塞模式 6 ssc.socket().bind(new InetSocketAddress(8080)); 7 ssc.register(selector, SelectionKey.OP_ACCEPT);//注册监听事件 8 while(true) { 9 Set selectorKeys = selector.selectorKeys(); //取得所有key的集合10 Iterator it = selectorKeys.iterator();11 while(it.hasNext()) { //检查序列是否还有元素12 SelectionKey key = (SelectionKey) it.next();13 if((key.readyOps() & SelectionKey.OP_ACCEPT) == SelectionKey.OP_ACCEPT) {14 ServerSocketChannel ssChannel = (ServerSocketChannel) key.channel();15 SocketChannel sc = ssChannel.accept();//接受到服务端的请求16 sc.configureBlocking(false);17 sc.register(selector,SelectionKey.OP_ACCEPT);18 it.remove();19 } else if((key.readyOps() & SelectionKey.OP_READ) == SelectionKey.OP_READ) {20 SocketChannel sc = (SocketChannel) key.channel;21 while(true) {22 buffer.clear();23 int n = sc.read(buffer);//读取数据24 if(n <= 0) {25 break;26 } buffer.flip();28 } it.remove();30 } } } }
The above code is a code example of the specific working process of Nio in the book. Its working process can be elaborated as follows:
There are two key classes: Channel and Selector, they are the core concepts in NIO. Channel can be compared to a means of transportation, and it is a specific means of transportation, such as cars and bicycles. The Selector can be compared to a vehicle dispatching system, responsible for monitoring vehicle operating conditions and specific dispatching work.
The serverSocketChannel class is an optional channel for stream-oriented listening sockets. After the server creates such an instance, it uses the Socket() method (to obtain the information associated with this channel). Server Socket) returns an instance of the ServerSocket class, and uses the void bind(SocketAddress endpoint) method of this instance to bind ServerSocket
to a specific address (IP address and port number). Just like every taxi in a taxi company must be registered with the company, we also need to register the ServerSocketChannel instance to the specified selector through the register() method.
Then call the Selector's selectedKeys method to check whether all communication channels that have been registered on this selector have the required events. If an event occurs, all the events will be returned. SelectionKey, since key.readyOps() obtains the ready operation set of this key, I personally think that key.readyOps()&SelectionKey.OP_ACCEPT means that the ready operation set of this key is in the ACCEPT state, that is, the server is in the listening state , the communication channel object can be obtained through the Channel method of this object, and then the accept() method is called to return a socket channel, which defaults to a blocking state. You can also use the communication channel object obtained by the Channel method to read the communication data, and The data read here is Buffer, and this Buffer is a buffer we can control.
The above is the detailed content of Sample code about the specific working process of Nio. For more information, please follow other related articles on the PHP Chinese website!