publicclassNIOServer{privatestaticfinalLoggerLOGGER=LoggerFactory.getLogger(NIOServer.class);publicstaticvoidmain(String[]args)throwsIOException{Selectorselector=Selector.open();ServerSocketChannelserverSocketChannel=ServerSocketChannel.open();serverSocketChannel.configureBlocking(false);serverSocketChannel.bind(newInetSocketAddress(1234));serverSocketChannel.register(selector,SelectionKey.OP_ACCEPT);while(selector.select()>0){Set<SelectionKey>keys=selector.selectedKeys();Iterator<SelectionKey>iterator=keys.iterator();while(iterator.hasNext()){SelectionKeykey=iterator.next();iterator.remove();if(key.isAcceptable()){ServerSocketChannelacceptServerSocketChannel=(ServerSocketChannel)key.channel();SocketChannelsocketChannel=acceptServerSocketChannel.accept();socketChannel.configureBlocking(false);LOGGER.info("Accept request from {}",socketChannel.getRemoteAddress());socketChannel.register(selector,SelectionKey.OP_READ);}elseif(key.isReadable()){SocketChannelsocketChannel=(SocketChannel)key.channel();ByteBufferbuffer=ByteBuffer.allocate(1024);intcount=socketChannel.read(buffer);if(count<=0){socketChannel.close();key.cancel();LOGGER.info("Received invalide data, close the connection");continue;}LOGGER.info("Received message {}",newString(buffer.array()));}keys.remove(key);}}}}
publicclassNIOServer{privatestaticfinalLoggerLOGGER=LoggerFactory.getLogger(NIOServer.class);publicstaticvoidmain(String[]args)throwsIOException{Selectorselector=Selector.open();ServerSocketChannelserverSocketChannel=ServerSocketChannel.open();serverSocketChannel.configureBlocking(false);serverSocketChannel.bind(newInetSocketAddress(1234));serverSocketChannel.register(selector,SelectionKey.OP_ACCEPT);while(true){if(selector.selectNow()<0){continue;}Set<SelectionKey>keys=selector.selectedKeys();Iterator<SelectionKey>iterator=keys.iterator();while(iterator.hasNext()){SelectionKeykey=iterator.next();iterator.remove();if(key.isAcceptable()){ServerSocketChannelacceptServerSocketChannel=(ServerSocketChannel)key.channel();SocketChannelsocketChannel=acceptServerSocketChannel.accept();socketChannel.configureBlocking(false);LOGGER.info("Accept request from {}",socketChannel.getRemoteAddress());SelectionKeyreadKey=socketChannel.register(selector,SelectionKey.OP_READ);readKey.attach(newProcessor());}elseif(key.isReadable()){Processorprocessor=(Processor)key.attachment();processor.process(key);}}}}}
publicclassNIOServer{privatestaticfinalLoggerLOGGER=LoggerFactory.getLogger(NIOServer.class);publicstaticvoidmain(String[]args)throwsIOException{Selectorselector=Selector.open();ServerSocketChannelserverSocketChannel=ServerSocketChannel.open();serverSocketChannel.configureBlocking(false);serverSocketChannel.bind(newInetSocketAddress(1234));serverSocketChannel.register(selector,SelectionKey.OP_ACCEPT);intcoreNum=Runtime.getRuntime().availableProcessors();Processor[]processors=newProcessor[coreNum];for(inti=0;i<processors.length;i++){processors[i]=newProcessor();}intindex=0;while(selector.select()>0){Set<SelectionKey>keys=selector.selectedKeys();for(SelectionKeykey:keys){keys.remove(key);if(key.isAcceptable()){ServerSocketChannelacceptServerSocketChannel=(ServerSocketChannel)key.channel();SocketChannelsocketChannel=acceptServerSocketChannel.accept();socketChannel.configureBlocking(false);LOGGER.info("Accept request from {}",socketChannel.getRemoteAddress());Processorprocessor=processors[(int)((index++)%coreNum)];processor.addChannel(socketChannel);processor.wakeup();}}}}}