5. register accept handler Selector ssc accept int num = selector.select(); Set selectedKeys = selector.selectedKeys(); Iterator it = selectedKeys.iterator(); while (it.hasNext()) { SelectionKey key = (SelectionKey)it.next(); if ((key.readyOps() & SelectionKey.OP_ACCEPT) == SelectionKey.OP_ACCEPT) { // Accept the new connection and send to a handler. } }
7. register accept handler Selector ssc accept read sc read handler if ((key.readyOps() & SelectionKey.OP_ACCEPT) == SelectionKey.OP_ACCEPT) { // Accept the new connection and send to a handler. } else if ((key.readyOps() & SelectionKey.OP_READ) == SelectionKey.OP_READ) { // Read the data and send the handler. SocketChannelsc = (SocketChannel)key.channel(); }
8. register accept handler Selector ssc accept read sc read handler write sc write handler It’s the same process with the write handler.
9. register accept handler Selector ssc accept read sc read handler write sc write handler You can have an array of handlers.
10. C L I E N T S register accept handler Selector ssc accept read sc read handler write sc write handler For each client event, the selector will notify the handler, and return to listening to event again.