/** * 阻塞模式下的ServerSocketChannel */ public static void startBlockingServerChannel() { new Thread(new Runnable() { @Override public void run() { ServerSocketChannel serverSocketChannel = null; try { while (true) { serverSocketChannel = ServerSocketChannel.open(); serverSocketChannel.configureBlocking(true); serverSocketChannel.bind(new InetSocketAddress(90)); System.out.println("[server]accepting..."); SocketChannel client = serverSocketChannel.accept(); ByteBuffer dst = ByteBuffer.allocate(1024); client.read(dst); String result = new String(dst.array(), "UTF-8"); System.out.println("->" + result); serverSocketChannel.close(); } } catch (IOException e) { e.printStackTrace(); } finally { serverSocketChannel.close(); } } }).start(); } /** * 阻塞SocketChanel调用ServerSocketChannel */ public static void startBlockingClinetUsingSocketChannel() { new Thread(new Runnable() { @Override public void run() { SocketChannel s = null; try { System.out.println("[client]start a client socket:"); s = SocketChannel.open(); //s.configureBlocking(true); System.out.println("[client]try connect..."); s.connect(new InetSocketAddress("127.0.0.1", 90)); System.out.println("[client]after connect"); ByteBuffer src = ByteBuffer.allocate(20); src.put("Hello".getBytes("UTF-8")); src.flip(); // 为读准备好 s.write(src); s.close(); } catch (Exception e) { e.printStackTrace(); } finally { try { s.close(); } catch (IOException e) { e.printStackTrace(); } } } }).start(); }
public static void startNoBlocingSocketChannelAsClinet() { SocketChannel socketChannel = null; try { System.out.println("[client]start a client socket:"); s = SocketChannel.open(); s.configureBlocking(false); s.connect(new InetSocketAddress("127.0.0.1", 90)); while (true) { if (s.finishConnect()) { System.out.println("[client]connected..."); ByteBuffer src = ByteBuffer.allocate(100); src.put("Hello".getBytes("UTF-8")); src.flip(); s.write(src); break; } } } catch (Exception e) { } finally { /*close*/ } }
public static void startNoBlockingServerChannel() { new Thread(new Runnable() { @Override public void run() { ServerSocketChannel serverSocketChannel = null; Selector selector = null; try { selector = Selector.open(); serverSocketChannel = ServerSocketChannel.open(); serverSocketChannel.configureBlocking(false); serverSocketChannel.socket() .bind(new InetSocketAddress(90)); // register to selector serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT); // 轮询selector while (true) { System.out.println( "[server] Selector select and blocking ..."); selector.select(); // blocking,wait until 1 event Set<SelectionKey> keys = selector.selectedKeys(); // not .keys() Iterator iter = keys.iterator(); while (iter.hasNext()) { SelectionKey key = (SelectionKey) iter.next(); iter.remove(); // 以下判断最好使用 if else if (key.isValid() == false) { continue; } else if (key.isAcceptable()) { SocketChannel client = ((ServerSocketChannel) key.channel()).accept(); client.configureBlocking(false); // 继续设置为非阻塞模式(也可以是阻塞,直接处理) client.register(selector, SelectionKey.OP_READ); // 继续向Selector注册 } else if (key.isReadable()) { // start read SocketChannel client = (SocketChannel) key.channel(); ByteBuffer bf = ByteBuffer.allocate(1000); int count = client.read(bf); System.out.println("-->" + new String(bf.array(), "UTF-8")); // bf.flip(); key.cancel(); } else if (key.isWritable()) { // 不做处理 } } } } catch (IOException e) { e.printStackTrace(); } finally { /* close selector and ServerSocketChannel*/ } } }).start(); }