使用NIO实现非阻塞式(相对的)多人聊天室
发布日期:2021-10-12 20:08:33
浏览次数:3
分类:技术文章
本文共 4216 字,大约阅读时间需要 14 分钟。
1.服务端
package d02_test;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.ServerSocketChannel;import java.nio.channels.SocketChannel;import java.util.Iterator;import java.util.Set;/** * zt * 2020/8/11 * 18:02 */public class Server { public static void main(String[] args) { //1.创建ServerSockChannel ServerSocketChannel listener = null; try { listener = ServerSocketChannel.open(); //2.绑定地址 listener.bind(new InetSocketAddress("10.0.139.160",10086)); //3.设置非阻塞模式 listener.configureBlocking(false); //4.创建selector选择器 Selector selector = Selector.open(); //5.把通道注册到选择器,并指定注册事件 listener.register(selector, SelectionKey.OP_ACCEPT); //6.轮询(Select()阻塞方法。没有事件发送阻塞) System.out.println("服务器启动.....0.0"); while (selector.select()>0){ //7.处理 SetselectionKeys = selector.selectedKeys(); Iterator iterator = selectionKeys.iterator(); while (iterator.hasNext()){ SelectionKey selectionKey = iterator.next(); if(selectionKey.isAcceptable()){ SocketChannel socketChannel = listener.accept(); System.out.println(socketChannel.getRemoteAddress()+"进入聊天室"); //设置非阻塞式模式 socketChannel.configureBlocking(false); //注册到selector socketChannel.register(selector, SelectionKey.OP_READ); }else if(selectionKey.isReadable()){ //读取数据 SocketChannel channel = (SocketChannel) selectionKey.channel(); ByteBuffer buffer = ByteBuffer.allocate(1024); int len = 0; //不会阻塞 try { while ((len=channel.read(buffer))>0){ buffer.flip(); String data = new String(buffer.array(),0,buffer.limit()); System.out.println(channel.getRemoteAddress()+"说:"+data); buffer.clear(); } //开启线程回复 if(len==1){ System.out.println(channel.getRemoteAddress()+"退出 了"); channel.close(); } } catch (IOException e) { System.out.println(channel.getRemoteAddress()+"异常退出了"); channel.close(); } } //已经处理过了键删除掉 iterator.remove(); } } } catch (IOException e) { e.printStackTrace(); }finally { try { listener.close(); } catch (IOException e) { e.printStackTrace(); } } }}
2.客户端
package d02_test;import java.net.InetSocketAddress;import java.nio.ByteBuffer;import java.nio.channels.SocketChannel;import java.util.Scanner;/** * zt * 2020/8/11 * 18:02 */public class Client { public static void main(String[] args) throws Exception { //1.创建socketchannel SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress("10.0.139.160",10086)); //2.设置为非阻塞模式 socketChannel.configureBlocking(false); //3.控制台录入并写出 Scanner sc = new Scanner(System.in); while (true){ String data = sc.next(); ByteBuffer buffer = ByteBuffer.allocate(1024); buffer.put(data.getBytes()); buffer.flip(); socketChannel.write(buffer); buffer.clear(); if(data.equals("886")){ break; } } //关闭 socketChannel.close(); }}
3.运行结果:
Server
服务器启动.....0.0/10.0.139.160:60397进入聊天室/10.0.139.160:60397说:呱?/10.0.139.160:60397说:你好啊
Client
呱?你好啊
转载地址:https://blog.csdn.net/qq_39773004/article/details/107943063 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
哈哈,博客排版真的漂亮呢~
[***.90.31.176]2024年04月15日 21时33分39秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
tianxiawuzhei_linux中触摸屏驱动的实现——基于s3c6410处理器
2019-04-30
四线触摸屏原理
2019-04-30
Hi-Z(高阻态)
2019-04-30
小议Linux staging tree
2019-04-30
内核配置中的:USB Gadget Support
2019-04-30
内核配置中的:Multifunction device drivers
2019-04-30
关于内核中 #ifdef CONFIG_**的问题
2019-04-30
Linux Kernel and Android 休眠与唤醒(中文版)
2019-04-30
Android中间件开发----Windows下Android环境搭建(最新最方便)
2019-04-30
C/C++如何返回一个数组/指针
2019-04-30
如何在 Ubuntu 16.04 上安装并使用 TensorFlow
2019-04-30
基于Android NDK的学习之旅-----JNI LOG打印
2019-04-30
语音命令词识别
2019-04-30
腾讯AI语音识别API踩坑记录
2019-04-30
linux中Oops信息的调试及栈回溯—Linux人都知道,这是好东西!
2019-04-30
C语言与javascript的不同之处
2019-04-30
Android照相功能驱动层中HAL的实现(基于OK6410开发板+OV9650摄像头)
2019-04-30
Android Camera HAL设计初步
2019-04-30
基于U-boot上TFTP服务器更新系统
2019-04-30