
NIO
发布日期:2021-05-06 22:37:55
浏览次数:23
分类:技术文章
本文共 4262 字,大约阅读时间需要 14 分钟。
服务端
import java.io.IOException;import java.net.InetSocketAddress;import java.nio.ByteBuffer;import java.nio.channels.*;import java.util.Iterator;public class MyServer { private int size = 1024; private ServerSocketChannel serverSocketChannel; private ByteBuffer byteBuffer; private Selector selector; private int remoteClientNum = 0; public MyServer(int port) throws IOException { initChannel(port); } public void initChannel(int port) throws IOException { serverSocketChannel = ServerSocketChannel.open(); serverSocketChannel.configureBlocking(false); serverSocketChannel.bind(new InetSocketAddress(port)); selector =Selector.open(); serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT); byteBuffer = ByteBuffer.allocate(size); } private void listener() throws IOException { while(true){ int select = selector.select(); if(select==0){ continue; } Iteratoriterator = selector.selectedKeys().iterator(); while(iterator.hasNext()){ SelectionKey key = iterator.next(); if(key.isAcceptable()){ ServerSocketChannel server = (ServerSocketChannel) key.channel(); SocketChannel channel = server.accept(); registerChannel(selector,channel,SelectionKey.OP_READ); remoteClientNum++; System.out.println("客户端连接数"+remoteClientNum); write(channel,"hello client".getBytes()); } if(key.isReadable()){ read(key); } iterator.remove(); } } } private void registerChannel(Selector selector,SocketChannel socketChannel,int opRead) throws IOException { if(socketChannel == null){ return; } socketChannel.configureBlocking(false); socketChannel.register(selector,opRead); } private void write(SocketChannel socketChannel,byte[] writeDate) throws IOException { byteBuffer.clear(); byteBuffer.put(writeDate); byteBuffer.flip(); socketChannel.write(byteBuffer); } private void read(SelectionKey key) throws IOException { SocketChannel socketChannel = (SocketChannel)key.channel(); int count; byteBuffer.clear(); while ((count=socketChannel.read(byteBuffer))>0){ byteBuffer.flip(); while (byteBuffer.hasRemaining()){ System.out.print((char) byteBuffer.get()); } byteBuffer.clear(); } if(count < 0 ){ socketChannel.close(); } } public static void main(String[] args) throws IOException { MyServer myServer = new MyServer(9999); myServer.listener(); }}
客户端
import java.io.IOException;import java.net.InetSocketAddress;import java.nio.ByteBuffer;import java.nio.channels.SocketChannel;public class MyClient { private int size = 1024; private ByteBuffer byteBuffer; private SocketChannel socketChannel; public void connectServer() throws IOException { socketChannel = SocketChannel.open(); socketChannel.connect(new InetSocketAddress("127.0.0.1",9999)); socketChannel.configureBlocking(false); byteBuffer = ByteBuffer.allocate(size); receive(); } private void receive() throws IOException { while(true){ byteBuffer.clear(); int count; while ((count=socketChannel.read(byteBuffer))>0){ byteBuffer.flip(); while (byteBuffer.hasRemaining()){ System.out.print((char)byteBuffer.get()); } send2Server("sayhi".getBytes()); byteBuffer.clear(); } } } private void send2Server(byte[] bytes) throws IOException { byteBuffer.clear(); byteBuffer.put(bytes); byteBuffer.flip(); socketChannel.write(byteBuffer); } public static void main(String[] args) throws IOException { new MyClient().connectServer(); }}
发表评论
最新留言
路过按个爪印,很不错,赞一个!
[***.219.124.196]2025年03月11日 05时19分28秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
【论文泛读03】卷积LSTM网络:一种短时降雨量预测的机器学习方法
2019-03-04
中科大-凸优化 笔记(lec45)-强凸性等价不等式
2019-03-04
linux 中 alien命令的使用
2019-03-04
【论文泛读29】关系抽取:卷积神经网络的视角
2019-03-04
shell 中的 set命令 -e -o 选项作用
2019-03-04
Python中JSON的基本使用
2019-03-04
函数的默认参数值,即在定义参数的时候给它一个默认值
2019-03-04
ubuntu install baidu inputmethod
2019-03-04
程序员建议(忘记从哪里转的了,反正是csdn上的一个兄弟)
2019-03-04
电脑重装系统后提示invalid partition table怎么解决
2019-03-04
c++ primer 5th 练习11.9自己编写的答案
2019-03-04
web实现断点续传
2019-03-04
自定义BootstrapTable扩展:分页跳转到指定页码
2019-03-04
Python3逻辑运算符
2019-03-04
【学习笔记】欧拉函数,欧拉公式
2019-03-04
Python3序列
2019-03-04
vue-cli中找不到jquery的原因,以使用ztree为例
2019-03-04
React中设置404页面
2019-03-04
BootstrapValidator手动触发部分验证
2019-03-04
vue调试工具vue-devtools安装及使用
2019-03-04