
NIO工作流程如图package nio;import java.io.IOException;import java.net.InetSocketAddress;import java.nio.ByteBuffer;import java.nio.channels.SelectionKey;import java.nio.channels.Selector;import java.nio.channels.ServerSocketChannel;import java.nio.channels.SocketChannel;import java.nio.charset.Charset;import java.nio.charset.CharsetDecoder;import java.util.Iterator;import java.util.Set;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;public class Service {private static Charset charsetCharset.forName(UTF-8); private static CharsetDecoder decodercharset.newDecoder(); public static void main(String[] args) throws IOException { //1创建一个注册中心 Selector selectorSelector.open(); //2创建通道 ServerSocketChannel channelServerSocketChannel.open(); int port9200; //3把通道绑定到服务端口 channel.bind(new InetSocketAddress(port)); //4把通道注册到selector 非阻塞 channel.configureBlocking(false); channel.register(selector, SelectionKey.OP_ACCEPT); int connectionCount0; ExecutorService executorServiceExecutors.newFixedThreadPool(3); while(true){ //得到就绪的channel的数量 int readChannelCountselector.select(); if(readChannelCount0){ continue; } //得到就绪的channel的key SetSelectionKey selectedKeysselector.selectedKeys(); IteratorSelectionKey keyIteratorselectedKeys.iterator(); while(keyIterator.hasNext()){ SelectionKey keykeyIterator.next(); //可接收的通道 if(key.isAcceptable()){ ServerSocketChannel channel2(ServerSocketChannel) key.channel(); SocketChannel socketChannelchannel2.accept(); socketChannel.configureBlocking(false); socketChannel.register(selector, SelectionKey.OP_READ, connectionCount); }else if(key.isReadable()){ //交给线程处理读取数据 executorService.execute(new SocketProcess(key)); key.cancel(); } keyIterator.remove(); } } } static class SocketProcess implements Runnable{ SelectionKey key; private SocketProcess(SelectionKey key){ super(); this.keykey; } Override public void run() { try{ System.out.println(连接key.attachment()发来了readFormChannel()); }catch(Exception e){ e.printStackTrace(); } } private String readFormChannel() throws IOException{ SocketChannel channel(SocketChannel) key.channel(); int size1024; ByteBuffer bufferByteBuffer.allocateDirect(size); //定义一个更大的buffer ByteBuffer bigBuffernull; int count0; while((channel.read(buffer))!-1){ count; ByteBuffer tempByteBuffer.allocateDirect(size*(count1)); if(bigBuffer!null){ bigBuffer.flip(); temp.put(bigBuffer); } bigBuffertemp; buffer.flip(); bigBuffer.put(buffer); buffer.clear(); if(bigBuffer!null){ bigBuffer.flip(); try{ return decoder.decode(bigBuffer).toString(); }catch(Exception e){ e.printStackTrace(); } } } return null; } }}package nio;import java.net.InetSocketAddress;import java.nio.ByteBuffer;import java.nio.channels.SocketChannel;import java.nio.charset.Charset;import java.util.Scanner;public class Client {private static Charset charsetCharset.forName(UTF-8); public static void main(String[] args) { try{ SocketChannel channelSocketChannel.open(); boolean connectionchannel.connect(new InetSocketAddress(localhost, 9200)); System.out.println(connection); Scanner scannernew Scanner(System.in); System.out.println(请输入); String messagescanner.nextLine(); ByteBuffer bufferByteBuffer.wrap(message.getBytes(charset)); while(buffer.hasRemaining()){ int writedCountchannel.write(buffer); } }catch(Exception e){ e.printStackTrace(); } }}