java NIO SocketClinet和ServerSocket的实例用法
发表于:2025-11-12 作者:千家信息网编辑
千家信息网最后更新 2025年11月12日,本篇内容介绍了"java NIO SocketClinet和ServerSocket的实例用法"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些
千家信息网最后更新 2025年11月12日java NIO SocketClinet和ServerSocket的实例用法
本篇内容介绍了"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 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 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的实例用法"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!
实例
内容
更多
知识
实用
学有所成
接下来
困境
实际
情况
文章
案例
编带
网站
行业
过程
高质量
学习
有关
服务
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
小学网络安全主题班会简报
郧西软件开发质量保证
蓝田网络安全培训
上海软件开发工程师年薪
互联网招聘软件开发
个人对网络安全的认识
计算机数据库技术考什么
数据库还原备份报错
数据库打不开了图片
软件开发费的税务分类编码
drupal8 数据库
数据库输出到页面乱码
软件开发百分百复制别人的吗
王国纪元中国版服务器
吉林省展荣网络技术有限公司
数据库某同学报名男子三级跳项目
数据库id自动增长失效
信创服务器批发供应
淮北点餐系统软件开发公司哪家好
网络安全红蓝对
网络安全教育想法和小建议
我的世界服务器游戏怎么退出
网络技术交换机h3c
云海服务器管理公司
网络安全运维工程师防火墙
服务器的ups电源运用场合
找回服务器的管理账号
软件开发阐释设计模式
我的世界服务器移植玩家数据
软件开发的公司怎么记账