java NIO SocketClinet和ServerSocket的实例用法
短信预约 -IT技能 免费直播动态提醒
本篇内容介绍了“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的实例用法”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注编程网网站,小编将为大家输出更多高质量的实用文章!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341