写的比较简单,没有异步输入消息,参考韩顺平老师的讲解
server.java
package com.company; import java.io.IOException; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.channels.*; import java.nio.charset.StandardCharsets; import java.util.Iterator; import java.util.Set; public class Server { static Selector selector ; static ServerSocketChannel serverSocketChannel; public static void main(String[] args) throws Exception { selector = Selector.open(); serverSocketChannel = ServerSocketChannel.open(); serverSocketChannel.bind(new InetSocketAddress(8080)); serverSocketChannel.configureBlocking(false); serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT); while (true) { int count = selector.select(1000); if (count > 0) { Set<SelectionKey> selectionKeys = selector.selectedKeys(); Iterator<SelectionKey> iterator = selectionKeys.iterator(); while (iterator.hasNext()) { SelectionKey key = iterator.next(); if (key.isAcceptable()) { SocketChannel accept = serverSocketChannel.accept(); accept.configureBlocking(false); accept.register(selector, SelectionKey.OP_READ); System.out.println(accept.getRemoteAddress() + " 上线了"); } if (key.isReadable()) { // todo readData(key); } iterator.remove(); } } else { // System.out.println("等待ing"); } } } private static void readData(SelectionKey key) { SocketChannel channel = null; try { channel = (SocketChannel)key.channel(); ByteBuffer buffer = ByteBuffer.allocate(1024); int read = channel.read(buffer); if (read>0) { String string = new String(buffer.array(), 0, buffer.position(), StandardCharsets.UTF_8); System.out.println("客户端发送"+string); sendInfoToOther(string, channel); } } catch (IOException e) { try { System.out.println(channel.getRemoteAddress() + "离线了"); key.cancel(); channel.close(); e.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } } } private static void sendInfoToOther(String string, SocketChannel self) throws IOException { for (SelectionKey key : selector.keys()) { Channel channel = key.channel(); if(channel instanceof SocketChannel && channel != self ){ SocketChannel target = (SocketChannel) channel; ByteBuffer buffer = ByteBuffer.wrap(string.getBytes(StandardCharsets.UTF_8)); target.write(buffer); } } } }
Client.java
package com.company; 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.SocketChannel; import java.nio.charset.StandardCharsets; import java.util.Iterator; import java.util.Set; public class Client { public static void main(String[] args) throws IOException { Selector selector = Selector.open(); SocketChannel sc = SocketChannel.open(new InetSocketAddress("127.0.0.1", 8080)); sc.configureBlocking(false); sc.register(selector, SelectionKey.OP_READ); String username = sc.getLocalAddress().toString().substring(1); System.out.println("username" + username); sc.write(ByteBuffer.wrap((username + "说 你好").getBytes(StandardCharsets.UTF_8))); while (true) { int count = selector.select(1000); if (count > 0) { Set<SelectionKey> selectionKeys = selector.selectedKeys(); Iterator<SelectionKey> iterator = selectionKeys.iterator(); while (iterator.hasNext()) { SelectionKey next = iterator.next(); if (next.isReadable()) { SocketChannel channel = (SocketChannel) next.channel(); ByteBuffer buff = ByteBuffer.allocate(1024); channel.read(buff); String s = new String(buff.array(), 0, buff.position(), StandardCharsets.UTF_8); System.out.println(s); } else { System.out.println("没有可用的通道"); } iterator.remove(); } } } } }