本篇内容介绍了“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(); Setselectkey = 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 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的实例用法”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!