public class NIOServer { private Selector selector; public void startServer(int port) throws IOException { ServerSocketChannel serverChannel = ServerSocketChannel.open(); // 设置通道为非阻塞 serverChannel.configureBlocking(false); serverChannel.socket().bind(new InetSocketAddress(port)); this.selector = Selector.open(); serverChannel.register(selector, SelectionKey.OP_ACCEPT); } public void listen() throws IOException { while (true) { //当注册的事件到达时,方法返回;否则,该方法会一直阻塞 selector.select(); Iterator<SelectionKey> it = this.selector.selectedKeys().iterator(); while (it.hasNext()) { SelectionKey key = (SelectionKey) it.next(); // 此处一定要删除 it.remove(); if (key.isAcceptable()) { ServerSocketChannel server = (ServerSocketChannel) key .channel(); SocketChannel channel = server.accept(); System.out.println("remote socket is:"+channel.socket().getInetAddress()); channel.configureBlocking(false); channel.write(ByteBuffer.wrap(new String("send one msg to client").getBytes())); channel.register(this.selector, SelectionKey.OP_READ); } else if (key.isReadable()) { read(key); } } } } public void read(SelectionKey key) throws IOException{ SocketChannel channel = (SocketChannel) key.channel(); ByteBuffer buffer = ByteBuffer.allocate(1024); int len = channel.read(buffer); byte[] data = new byte[len]; System.arraycopy(buffer.array(), 0, data, 0, len); String msg = new String(data).trim(); System.out.println("server receive one msg:"+msg); ByteBuffer outBuffer = ByteBuffer.wrap(msg.getBytes()); channel.write(outBuffer); } public static void main(String[] args) throws IOException { NIOServer server = new NIOServer(); server.startServer(8080); server.listen(); } }
NIO客户端代码:
public class NIOClient { private Selector selector; public void initClient(String ip,int port) throws IOException { SocketChannel channel = SocketChannel.open(); channel.configureBlocking(false); this.selector = Selector.open(); channel.connect(new InetSocketAddress(ip,port)); channel.register(selector, SelectionKey.OP_CONNECT); } public void listen() throws IOException { while (true) { selector.select(); Iterator<SelectionKey> it = this.selector.selectedKeys().iterator(); while (it.hasNext()) { SelectionKey key = (SelectionKey) it.next(); it.remove(); if (key.isConnectable()) { SocketChannel channel = (SocketChannel) key.channel(); if(channel.isConnectionPending()){ channel.finishConnect(); } channel.configureBlocking(false); channel.write(ByteBuffer.wrap(new String("你在他乡还好吗").getBytes())); channel.register(this.selector, SelectionKey.OP_READ); } else if (key.isReadable()) { read(key); } } } } public void read(SelectionKey key) throws IOException{ SocketChannel channel = (SocketChannel) key.channel(); ByteBuffer buffer = ByteBuffer.allocate(1024); int len = channel.read(buffer); byte[] data = new byte[len]; System.arraycopy(buffer.array(), 0, data, 0, len); String msg = new String(data).trim(); System.out.println("client receive one msg:"+msg); ByteBuffer outBuffer = ByteBuffer.wrap(msg.getBytes()); } public static void main(String[] args) throws IOException { NIOClient client = new NIOClient(); client.initClient("localhost",8080); client.listen(); } }