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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 【NIO】Java NIO之通道Java
- Java NIOJava
- 【NIO】Java NIO之緩衝Java
- Java NIO系列2:NIO概述Java
- 【NIO】Java NIO之選擇器Java
- Java NIO - BufferJava
- Java NIO - 群聊Java
- JAVA NIO BufferJava
- Java NIO filesJava
- JAVA 探究NIOJava
- Java NIO:通道Java
- Java NIO 通道Java
- ServerSocket的問題Server
- java基礎-java NIOJava
- 淺析Java NIOJava
- Asyncdb(三):Java NIOJava
- Java NIO Channel 使用Java
- Java NIO SocketChannelJava
- Java IO 和 NIOJava
- Java NIO之BufferJava
- Java nio記錄Java
- Java BIO,NIO,AIOJavaAI
- 詳解 Java NIOJava
- Java IO之NIOJava
- Java Socket 之 NIOJava
- Java--NIO&&AIOJavaAI
- Java NIO 概覽Java
- Java review--NIOJavaView
- Java NIO 學習Java
- Java Nio通訊Java
- 【譯】Java NIO 簡明教程系列之 NIO 概述Java
- Java NIO - 零複製Java
- Java NIO - 零拷貝Java
- 深入的聊聊 Java NIOJava
- Java NIO 程式碼示例Java
- java BIO、NIO學習Java
- Java NIO之SelectorJava
- day2 Java NIOJava