本篇内容介绍了“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(); } } } }
“java NIO SocketClinet和ServerSocket的实例用法”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。