java NIO SocketClinet ServerSocket
//SocketClient.java //--------------------------------------------------------------------------------- package niotest; 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.util.Iterator; import java.util.Set; public class SocketClient { public static void main(String[] args) throws IOException { Selector sc = Selector.open(); SocketChannel skc = SocketChannel.open(); skc.configureBlocking(false); skc.connect(new InetSocketAddress("127.0.0.1", 44444)); skc.register(sc, SelectionKey.OP_CONNECT); while(true) { sc.select(); Set<SelectionKey> selectkey = sc.selectedKeys(); Iterator it = selectkey.iterator(); while(it.hasNext()) { SelectionKey key = (SelectionKey) it.next(); if(key.isConnectable()) { SocketChannel msk = (SocketChannel) key.channel(); if(!msk.isConnected()) { while(!msk.finishConnect()) {} } msk.register(sc, SelectionKey.OP_WRITE); }else if (key.isReadable()) { }else if(key.isWritable()) { SocketChannel msk = (SocketChannel) key.channel(); ByteBuffer bb = ByteBuffer.wrap(new String("hellow world!").getBytes()); msk.write(bb); while (bb.hasRemaining()){ msk.write(bb); } key.cancel(); }else if(key.isAcceptable()) { }else { throw new RuntimeException("unknow selection type!"); } it.remove(); } } } } //SocketServer.java //---------------------------------------------------------------------------------------------------------- package niotest; import java.io.IOException; import java.net.InetSocketAddress; import java.net.SocketAddress; 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.util.Iterator; import java.util.Set; public class SocketServer { public static void main(String[] args) throws Exception { ServerSocketChannel sc = ServerSocketChannel.open(); sc.configureBlocking(false); sc.bind(new InetSocketAddress(44444)); Selector tor = Selector.open(); sc.register(tor, SelectionKey.OP_ACCEPT); while(true) { tor.select(); Set<SelectionKey> selectkey = tor.selectedKeys(); Iterator it = selectkey.iterator(); while(it.hasNext()) { SelectionKey key = (SelectionKey) it.next(); if(key.isAcceptable()) { ServerSocketChannel msk = (ServerSocketChannel) key.channel(); SocketChannel msc = msk.accept(); msc.configureBlocking(false); msc.register(tor, SelectionKey.OP_READ); }else if(key.isConnectable()) { }else if (key.isWritable()) { }else if(key.isReadable()) { SocketChannel msc = (SocketChannel) key.channel(); ByteBuffer bb = ByteBuffer.allocate(17); while(bb.hasRemaining()) { msc.read(bb); } System.out.println("服務端:"+new String(bb.array())); }else { throw new RuntimeException("unknow selection!"); } it.remove(); } } } }
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/31506529/viewspace-2200357/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Java NIOJava
- JAVA NIO BufferJava
- JAVA 探究NIOJava
- Java NIO SocketChannelJava
- Java NIO - BufferJava
- Java NIO - 群聊Java
- Java NIO:通道Java
- Java NIO filesJava
- Java NIO之SelectorJava
- Java NIO之BufferJava
- Java IO 和 NIOJava
- Java NIO 概覽Java
- Java Socket 之 NIOJava
- 詳解 Java NIOJava
- 淺析Java NIOJava
- Asyncdb(三):Java NIOJava
- Java IO之NIOJava
- Java NIO Channel 使用Java
- Java BIO,NIO,AIOJavaAI
- Java nio記錄Java
- 【譯】Java NIO 簡明教程系列之 NIO 概述Java
- day2 Java NIOJava
- 深入的聊聊 Java NIOJava
- Java NIO 程式碼示例Java
- java BIO、NIO學習Java
- Java NIO 之 Channel(通道)Java
- Java NIO - Channel 與 SelectorJava
- Java NIO - 零拷貝Java
- Java NIO - 零複製Java
- Java NIO:選擇器Java
- Java NIO:緩衝區Java
- Java NIO Selector 的使用Java
- Java-NIO之SelectorJava
- 【譯】Java NIO 簡明教程系列之 NIO 簡介Java
- Java NIO學習系列四:NIO和IO對比Java
- Java NIO和NIO.2有什麼區別? | baeldungJava
- Java NIO:Buffer、Channel 和 SelectorJava
- Java nio 空輪詢bugJava