1、NIOServerTest.java
package com.neohope.multisocket; import java.io.IOException; import java.net.InetSocketAddress; import java.net.Socket; 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.util.Iterator; import java.util.Set; public class NIOServerTest { public static void StartSockets() throws IOException { Selector selector = Selector.open(); int[] ports = {4000, 4001, 4002}; for (int port : ports) { ServerSocketChannel server = ServerSocketChannel.open(); server.configureBlocking(false); server.socket().bind(new InetSocketAddress(port)); //只处理了建立连接的消息 server.register(selector, SelectionKey.OP_ACCEPT); } int serverPort = 0; ByteBuffer byteBuffer = null; ServerSocketChannel serverChannel = null; SocketChannel clientChannel = null; while (selector.isOpen()) { selector.select(); Set<SelectionKey> readyKeys = selector.selectedKeys(); Iterator<SelectionKey> keyIterator = readyKeys.iterator(); while (keyIterator.hasNext()) { SelectionKey selectedKey = keyIterator.next(); keyIterator.remove(); if (selectedKey.isAcceptable()) { serverChannel = (ServerSocketChannel) selectedKey.channel(); serverPort = serverChannel.socket().getLocalPort(); clientChannel = serverChannel.accept(); clientChannel.configureBlocking(false); switch (serverPort) { case 4000: byteBuffer=ByteBuffer.wrap("welcome to port 4000".getBytes(Charset.forName("UTF-8"))); clientChannel.write(byteBuffer); break; case 4001: byteBuffer=ByteBuffer.wrap("welcome to port 4001".getBytes(Charset.forName("UTF-8"))); clientChannel.write(byteBuffer); break; case 4002: byteBuffer=ByteBuffer.wrap("welcome to port 4002".getBytes(Charset.forName("UTF-8"))); clientChannel.write(byteBuffer); break; } } } } } public static void main(String[] args) throws IOException { StartSockets(); } }
2、NIOClientTest.java
package com.neohope.multisocket; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.net.InetSocketAddress; import java.net.SocketAddress; import java.nio.ByteBuffer; import java.nio.channels.SocketChannel; public class NIOClientTest { public static void StartClient(String host, int port) { int readSize = 0; SocketChannel clientChannel = null; SocketAddress socketAddress = new InetSocketAddress(host, port); byte[] bytes; ByteBuffer byteBuffer = ByteBuffer.allocate(1024); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); try { clientChannel = SocketChannel.open(); clientChannel.connect(socketAddress); try { while ((readSize = clientChannel.read(byteBuffer)) >= 0) { byteBuffer.flip(); bytes = new byte[readSize]; byteBuffer.get(bytes); byteArrayOutputStream.write(bytes); byteBuffer.clear(); //服务端没有主动关闭连接,读取少于1024,假设读取完毕 if(readSize<1024)break; } System.out.println(byteArrayOutputStream.toString()); } catch (IOException ex) { ex.printStackTrace(); } finally { try { byteArrayOutputStream.close(); } catch(Exception ex) {} } } catch (IOException ex) { ex.printStackTrace(); } finally { try { clientChannel.close(); } catch(Exception ex) {} } } public static void main(String[] args) throws IOException { StartClient("localhost",4000); StartClient("localhost",4001); StartClient("localhost",4002); } }