Sample code about the specific working process of Nio
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

As a modern programming language, Go language plays an important role in development. The Go language provides some built-in time functions and structures to make time processing more convenient. In this article, we will introduce some commonly used time processing methods in the Go language. time.Now() We can use the time.Now() function to get the current time: now:=time.Now()fmt.Println(now) output: 2019-06-131

When we talk about programming languages and jobs, one programming language that comes to our mind is Java. Most companies around the world use Java. It's popular and there are many job opportunities. If you want to get a job with the help of Java skills in 2023, then this is good for you as Java skills can get you a job quickly. Plus, it can quickly advance your career. There is no magic trick that will make you find a job quickly. But your skills are like magic to you. Choose a job that satisfies you and a good environment that can greatly enhance your career. If you are a newbie and have experience, Java also provides you with a good job. Many companies use Java as the main program for their development. it

Analysis of front-end engineer responsibilities: What is the main job? With the rapid development of the Internet, front-end engineers play a very important professional role, playing a vital role as a bridge between users and website applications. So, what do front-end engineers mainly do? This article will analyze the responsibilities of front-end engineers, let us find out. 1. Basic responsibilities of front-end engineers Website development and maintenance: Front-end engineers are responsible for the front-end development of the website, including writing the website’s HTML, CSS and JavaScr

The successive release of large models such as ChatGPT has made many people feel stressed and afraid that AI will soon take over their jobs. In this regard, OpenAI has also published a study showing that the impact of ChatGPT covers all income classes, and high-income jobs may face greater risks. What are the facts? Should we automate all jobs, even the satisfying ones? It's one of several questions raised recently by the Future of Life Institute, which has called for a moratorium on large-scale artificial intelligence experiments and has now been signed by more than 10,000 people, including Elon Musk, Steve Wozniak and Andrew Yang.

Jobs you can find by learning Java: 1. Enterprise-level application development; 2. Website development; 3. Android development; 4. Embedded field; 5. Big data and cloud computing; 6. Game development; 7. Scientific applications; 8. Software development and maintenance; 9. System and network programming; 10. Security and encryption; 11. Education and training; 12. Consulting and consulting. Detailed introduction: 1. Enterprise-level application development. Java occupies a very important position in enterprise-level application development. Using Java, various complex enterprise-level applications can be developed, such as OA systems and so on.

Possible jobs: 1. Enterprise-level application development; 2. Website development; 3. Mobile application development; 4. Game development; 5. Big data analysis; 6. Scientific computing and artificial intelligence; 7. Embedded system development; 8 , financial industry application development, etc. Detailed introduction: 1. Enterprise-level application development: Java occupies an important position in enterprise-level application development and can be used to develop large and complex systems, such as ERP, CRM, etc. These systems usually need to process large amounts of data, support high concurrency, and ensure system stability; 2. Website development: including front-end and back-end, etc.

The jobs that C language can do in the future: 1. System development; 2. Game development; 3. Network development; 4. Application development; 5. Compiler development; 6. Algorithm engineer; 7. Network security; 8. Hardware development; 9 , Education field; 10. Data analysis and machine learning; 11. Software development and maintenance; 12. Software testing. Detailed introduction: 1. System development. C language is a common language for system-level programming. It can be used to develop operating systems and embedded systems. If you master C language, you can become a system development engineer; 2. Game development, etc.

Chrome will automatically record the URLs that have been entered in the address bar, and will automatically "associate query content" in the future. But many times we don't need some URLs, how to delete them? The editor often encounters this problem. Addresses that have been entered before will be blocked in front of commonly used addresses, resulting in the need to select several times to enter the desired website. I have looked for how to delete it at least three times because... I forget it every time. In the address bar shortcuts of Chrome's official help Chrome keyboard shortcuts, the delete shortcut key is clarified: ▍Windows deletes the address bar association content. Press the down arrow key to highlight the corresponding content, and then press the Shift+Delete key ▍macOS deletes the address Bar association content click down
