I used the following scenario to simulate nio communication: 1. ServerSocketChannel 2. Selector 3. Instructor (ServerSocket) 4. Student (SocketChannel) 5. Employee number/student number (SelectionKey) School: equivalent to our network application, once the school starts, the school will not stop, continue to run until the end of the semester; to start the school, You need: serverSocketChannel ssc = ServerSocketChannel. open (); // create a NIO channel ssc. configureBlocking (false); // make the channel non-blocking Teacher: equivalent to the Socket on the server. A teacher corresponds to multiple students and multiple students ask the teacher for advice. The teacher will answer the question one by one. If the school needs to operate normally, it cannot be a teacher. Therefore, before the school starts, you must hire a professional teacher to teach ServerSocket ss = ssc. socket (); // create a socket connection based on NIO channel // create a socket Channel port ss. bind (new InetSocketAddress ("127.0.0.1", SERVERPORT); Office of Academic Affairs: all teachers have them, but some departments need to manage teachers and students in a unified manner, if you cannot find a student at a school, you can tell the academic affairs office whether the student is a teacher or the employee number is the name of the teacher, if you are a student, the academic office will find the student ID and name to tell you where he is. // Bind the NIO channel to the selector. Of course, the primary key allocated after the binding is skey SelectionKey skey = ssc. register (selector, SelectionKey. OP_ACCEPT); after ssc registers the selector, the teacher ServerSocket under it will also be included in the employee book. So the instructor's number is skey Student: The school, the teacher, and the Academic Affairs office all have it, and now you can enroll students! If a student signs up: while (true) {// wait until the end of the semester until the student int num = selector. select (); // obtain whether there is a selector event in the channel, which means how many students report if (num <1) {continue;} Set selectedKeys = selector. selectedKeys (); // obtain the collection of events of interest in the channel. The collection here is the number set of teachers and students. If the key is for students, it is for old students to ask questions, if the key is a teacher, the teacher of the admissions office will register Iterator it = selectedKeys with a new student. iterator (); while (it. hasNext () {// traverse each key (student key and teacher key ).......}.....} since there are students reporting, there are two possibilities: one is that the new students register with the new students, and the other is that the old students ask questions. The above while (it. hasNext () can be written as follows: while (it. hasNext () {// traverse each event try {SelectionKey key = (SelectionKey) it. next (); // obtain the student ID key first // determine whether the student reports or asks questions if (key. readyOps () & SelectionKey. OP_ACCEPT) = SelectionKey. OP_ACCEPT) {// This is the Key of the new student registration. First, find the new student, and then find the new student. Then, you can register the student ID ServerSocketChannel serverChanel = (ServerSocketChannel) key. channel (); // The school and the teacher are found through the key // create a connection with the client socketCh from the serverSocketChannel Annel has a teacher to have students. It is impossible for me to teach computers. If I want to learn Bruce Lee, I will ask him to register SocketChannel SC = serverChanel. accept (); // student registration successful SC. configureBlocking (false); // register the new connection to the selector. After receiving the new connection, register a new student ID SelectionKey newKey = SC. register (selector, SelectionKey. OP_READ); // The student ID is successfully registered, and the permission of the student is assigned to it. remove (); // the new student registration task is completed. out. println ("Got connection from" + SC);} else // an event that reads client data. At this time, a client sends data and the client event is a question from an old student. If (key. readyOps () & SelectionKey. OP_READ) = SelectionKey. OP_READ) {// read data and accept students' questions SocketChannel SC = (SocketChannel) key. channel (); // The student ID is used to know who asked the question. // The following accepts the question int bytesEchoed = 0; while (bytesEchoed = SC. read (echoBuffer)> 0) {System. out. println ("bytesEchoed:" + bytesEchoed);} echoBuffer. flip (); System. out. println ("limet:" + echoBuffer. limit (); byte [] content = new byte [echoBuffer. limit ()]; echoBuff Er. get (content); String result = new String (content); doPost (result, SC); // the corresponding teacher will answer the question. Write the echoBuffer for details. clear (); it. remove (); // the task is completed. Remember that the same is true. remove the task. Otherwise, the next task will return an endless loop.} catch (Exception e) {}}} added your supplement: ssc. register (selector, SelectionKey. OP_ACCEPT); this method is to bind the ssc registration to the selector so that the next time you want to find ssc or determine whether an object is ssc, you can find it through selector, the search result is obtained by judging the ssc key. As for the second parameter SelectionKey. you can understand OP_ACCEPT as the key type or operation permission of ssc. If ssc is a school teacher, the teacher will have the OP_ACCEPT permission after the binding is successful, or his key type is SelectionKey. OP_ACCEPT Accept indicates acceptance. Is it similar to the accept () method in socket programming? Yes, that's right. We use this parameter to grant teachers the permissions to enroll students and register students. What about students? His permission is SelectionKey. OP_READ, which indicates that he has the permission to send and receive messages, that is, to ask questions. Therefore, he cannot register for other students. So you go back to the above and take a closer look at the while struct and make the following judgment: if (key. readyOps () & SelectionKey. OP_ACCEPT) = SelectionKey. OP_ACCEPT ){...} obviously, the person with the Accept permission may only be a teacher. What will the teacher do with the academic affairs office? It must have been recruited by the admissions office. A student was recruited to register for registration. Then, a keySelectionKey newKey = SC is assigned to the newly connected client. register (selector, SelectionKey. OP_READ); look, here only give it OP_READ, rather than Accept, another if elseif (key. readyOps () & SelectionKey. OP_READ) = SelectionKey. OP_READ) {// obviously, this is the student, because all students with OP_READ have previously been registered by the admissions office teacher.