一、建立服務端代碼java
public class NioServer { private static Map<String, SocketChannel> clientMap = new HashMap<>(); public static void main(String[] args) throws IOException { ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(); serverSocketChannel.configureBlocking(false); ServerSocket serverSocket = serverSocketChannel.socket(); serverSocket.bind(new InetSocketAddress(8899)); Selector selector = Selector.open(); serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT); while (true){ try { selector.select(); Set<SelectionKey> selectionKeys = selector.selectedKeys(); selectionKeys.forEach( selectionKey -> { final SocketChannel client; try{ //客戶端向服務端發起一個鏈接 if(selectionKey.isAcceptable()){ ServerSocketChannel server = (ServerSocketChannel)selectionKey.channel(); client = server.accept(); client.configureBlocking(false); client.register(selector, SelectionKey.OP_READ); String key = "[" + UUID.randomUUID() + "]"; clientMap.put(key, client); } //有數據可讀 else if(selectionKey.isReadable()){ client = (SocketChannel)selectionKey.channel(); ByteBuffer readBuffer = ByteBuffer.allocate(1024); int count = client.read(readBuffer); if(count > 0){ readBuffer.flip(); Charset charset = Charset.forName("utf-8"); String receivedMessage = String.valueOf(charset.decode(readBuffer).array()); System.out.println(client + ": " + receivedMessage); String senderKey = null; for(Map.Entry<String,SocketChannel> entry : clientMap.entrySet()){ if(client == entry.getValue()){ senderKey = entry.getKey(); break; } } for(Map.Entry<String,SocketChannel> entry : clientMap.entrySet()){ SocketChannel value = entry.getValue(); ByteBuffer writeBuffer = ByteBuffer.allocate(1024); writeBuffer.put((senderKey + ":" + receivedMessage).getBytes()); writeBuffer.flip(); value.write(writeBuffer); } } } }catch (Exception ex){ ex.printStackTrace(); } }); selectionKeys.clear(); }catch (Exception ex){ ex.printStackTrace(); } } } }
二、建立客戶端代碼dom
public class NioClient { public static void main(String[] args) { try{ SocketChannel socketChannel = SocketChannel.open(); socketChannel.configureBlocking(false); Selector selector = Selector.open(); socketChannel.register(selector, SelectionKey.OP_CONNECT); socketChannel.connect(new InetSocketAddress("127.0.0.1",8899)); while (true){ selector.select(); Set<SelectionKey> keySet = selector.selectedKeys(); for(SelectionKey selectionKey : keySet){ //與服務端已經創建好了鏈接 if(selectionKey.isConnectable()){ SocketChannel client = (SocketChannel)selectionKey.channel(); if(client.isConnectionPending()){ client.finishConnect(); //向服務端發數據 ByteBuffer writeBuffer = ByteBuffer.allocate(1024); writeBuffer.put((LocalDateTime.now() + "鏈接成功").getBytes()); writeBuffer.flip(); client.write(writeBuffer); // 啓動一個線程,不斷的讀取標準輸入的內容。而後將內容向回個服務端 ExecutorService executorService = Executors.newSingleThreadExecutor(Executors.defaultThreadFactory()); executorService.submit(() -> { while (true){ try { writeBuffer.clear(); InputStreamReader input = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(input); String sendMessage = br.readLine(); writeBuffer.put(sendMessage.getBytes()); writeBuffer.flip(); client.write(writeBuffer); }catch (Exception ex){ ex.printStackTrace(); } } }); } client.register(selector, SelectionKey.OP_READ); }else if(selectionKey.isReadable()){ SocketChannel client = (SocketChannel)selectionKey.channel(); ByteBuffer readBuffer = ByteBuffer.allocate(1024); int count = client.read(readBuffer); if(count > 0){ String receivedMessage = new String(readBuffer.array(), 0, count); System.out.println(receivedMessage); } } } keySet.clear(); } }catch (Exception ex){ ex.printStackTrace(); } } }
三、測試socket
1) 啓動服務端測試
2) 啓動兩個客戶端線程
3) 輸出結果3d
NioServer輸出code
第一個NioClient輸出。鏈接創建後,輸入helloserver
第二個NioClient輸出。 鏈接創建後,輸入worldblog