NIO案例-聊天室
NIO案例-聊天室
1. 聊天室服务端编写
package com.my.io.chat.server;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.nio.charset.StandardCharsets;
import java.util.Iterator;
import java.util.Set;
/*** @author zhupanlin* @version 1.0* @description: 聊天室的服务端* @date 2024/1/27 9:55*/
public class ChatServer {
public static void main(String[] args) {try {// 创建ServerSocketChannelServerSocketChannel serverSocketChannel = ServerSocketChannel.open();// 设置为非阻塞模式serverSocketChannel.configureBlocking(false);// 绑定端口serverSocketChannel.bind(new InetSocketAddress(9001));// 得到selectorSelector selector = Selector.open();// 将channel注册到selector上,并让selector对其接收的就绪状态感兴趣serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);System.out.println("聊天室服务器启动成功");// 轮询处理while (true){// 获得处于就绪状态的channel的总数int select = selector.select();if (select == 0){continue;}// 获得就绪状态所有的selectionKeys集合Set<SelectionKey> selectionKeys = selector.selectedKeys();// 遍历selectionKeys集合Iterator<SelectionKey> iterator = selectionKeys.iterator();while (iterator.hasNext()) {SelectionKey selectionKey = iterator.next();// 判断channel哪个操作处于就绪状态 channel被封装在selectionKey中if (selectionKey.isAcceptable()){// 说明有客户端连接,处理客户端的连接handleAccept(selectionKey);}else if (selectionKey.isReadable()){// 说明有客户端向服务端写数据,处理客户端的写请求handleRead(selectionKey);}//删除当前keyiterator.remove();}}} catch (IOException e) {throw new RuntimeException(e);}}
/*** 处理客户端的连接请求* @param selectionKey*/private static void handleAccept(SelectionKey selectionKey) {try {// 得到ServerSocketChannelServerSocketChannel channel = (ServerSocketChannel) selectionKey.channel();// 获得selectorSelector selector = selectionKey.selector();// 建立连接,获得socketChannel 此socketChannel与客户端的socketChannel为同一个SocketChannel socketChannel = channel.accept();// 将socketChannel设置成非阻塞socketChannel.configureBlocking(false);// 注册到selector中,让Selector对它的读操作感兴趣socketChannel.register(selector, SelectionKey.OP_READ);// 服务端向客户端回复一条消息String message = "欢迎进入聊天室";socketChannel.write(ByteBuffer.wrap(message.getBytes(StandardCharsets.UTF_8)));} catch (IOException e) {throw new RuntimeException(e);}
}
/*** 处理客户端写数据的请求--服务端处于读操作的就绪状态* 获得客户端写的数据,并且广播给其它所有的客户端* @param selectionKey*/private static void handleRead(SelectionKey selectionKey) {try {// 获得SocketChannelSocketChannel socketChannel = (SocketChannel) selectionKey.channel();// 获得selectorSelector selector = selectionKey.selector();// 创建BufferByteBuffer buffer = ByteBuffer.allocate(1024);// 读取数据到buffer中int len = 0;// 存放客户端发来的消息String message = "";while ((len = socketChannel.read(buffer)) > 0){buffer.flip();// 封装messagemessage += new String(buffer.array(), 0, len, StandardCharsets.UTF_8);}// 把客户端发送的消息,广播给其它所有客户端if (message.length() > 0){// 服务端本地打印System.out.println(socketChannel.getRemoteAddress() + ":" + message);// 广播消息handleMessage(message, selectionKey);}} catch (IOException e) {throw new RuntimeException(e);}}
/*** 广播消息给其它所有客户端* @param message * @param selectionKey*/private static void handleMessage(String message, SelectionKey selectionKey) {try {// 获得selectorSelector selector = selectionKey.selector();// 获得selector上面所有注册的channelSet<SelectionKey> keys = selector.keys();// 遍历for (SelectionKey key : keys) {SelectableChannel channel = key.channel();// 不发给自己if (channel != selectionKey.channel() && channel instanceof SocketChannel){// 发送消息// 获得SocketChannelSocketChannel socketChannel = (SocketChannel) channel;// 发送消息socketChannel.write(ByteBuffer.wrap(message.getBytes(StandardCharsets.UTF_8)));}}} catch (IOException e) {throw new RuntimeException(e);}}
}
2.聊天室客户端编写
package com.my.io.chat.client;
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.nio.charset.StandardCharsets;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;
/*** @author zhupanlin* @version 1.0* @description: 客户端程序* 1. 发送数据到服务端* 2. 接收服务端发来的消息* @date 2024/1/27 11:07*/
public class ChatClient {
/*** 启动客户端的方法* @param name 聊天室昵称*/public void start(String name){try {// 创建channelSocketChannel socketChannel = SocketChannel.open(new InetSocketAddress("localhost", 9001));// 设置成非阻塞socketChannel.configureBlocking(false);// 获得selectorSelector selector = Selector.open();// 注册channel到selector上,让selector对其读操作感兴趣,读取服务端发来的消息socketChannel.register(selector, SelectionKey.OP_READ);// 创建线程 专门负责发送消息new SendMessageThread(socketChannel, name).start();// 循环selectorwhile (true){// 获得这次就绪状态的channel数量int select = selector.select();if (select == 0){continue;}// 获得所有就绪状态的channelSet<SelectionKey> selectionKeys = selector.selectedKeys();Iterator<SelectionKey> iterator = selectionKeys.iterator();// 遍历while (iterator.hasNext()) {SelectionKey key = iterator.next();if (key.isReadable()){// 如果是读操作就绪,说明服务端广播数据了// 获得channelSocketChannel channel = (SocketChannel) key.channel();// 创建bufferByteBuffer buffer = ByteBuffer.allocate(1024);// 读取int len = 0;String message = "";while ((len = channel.read(buffer)) > 0){buffer.flip();// 封装messagemessage += new String(buffer.array(), 0, len, StandardCharsets.UTF_8);buffer.clear();}// 打印消息System.out.println(message);// 将channel再次注册到selector上,并让selector对读感兴趣channel.register(selector, SelectionKey.OP_READ);}// 移除keyiterator.remove();}}} catch (IOException e) {throw new RuntimeException(e);}}}
class SendMessageThread extends Thread{// 要发消息的socketChaneelprivate SocketChannel socketChannel;//自己的昵称private String name;
public SendMessageThread(SocketChannel socketChannel, String name) {this.socketChannel = socketChannel;this.name = name;}
@Overridepublic void run() {// 控制台输入Scanner sc = new Scanner(System.in);while (sc.hasNextLine()){String message = sc.nextLine();// 带上名字String sendMessage = name + ":" + message;// 发消息到channeltry {if (message.length() > 0){socketChannel.write(ByteBuffer.wrap(sendMessage.getBytes(StandardCharsets.UTF_8)));}} catch (IOException e) {throw new RuntimeException(e);}}}
}
相关文章:
NIO案例-聊天室
NIO案例-聊天室 1. 聊天室服务端编写 package com.my.io.chat.server; import java.io.IOException; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.channels.*; import java.nio.charset.StandardCharsets; import java.util.Iterato…...

文心一言情感关怀之旅
【AGIFoundathon】文心一言情感关怀之旅,让我们一起来体验吧! 上传一张照片,用ernie-bot生成专属于你的小故事! 此项目主要使用clip_interrogator获取图片的关键信息,然后将此关键信息用百度翻译API翻译成中文后,使用封装了⼀⾔API的Ernie Bot SDK(ernie-bot)生成故事…...

mac电脑安卓文件传输工具:Android File Transfer直装版
Android File Transfer(AFT)是一款用于在Mac操作系统上与Android设备之间传输文件。它允许用户将照片、音乐、视频和其他文件从他们的Android手机或平板电脑传输到Mac电脑,以及将文件从Mac上传到Android设备。 下载地址:https://w…...

第九篇【传奇开心果系列】beeware的toga开发移动应用示例:人口普查手机应用
传奇开心果博文系列 系列博文目录beeware的toga开发移动应用示例系列博文目录一、项目目标二、安装依赖三、实现应用雏形示例代码四、扩展功能和组件的考量五、添加更多输入字段示例代码六、添加验证功能示例代码七、添加数据存储功能示例代码八、添加数据展示功能示例代码九、…...

14.5 Flash查询和添加数据库数据
14.5 Flash查询和添加数据库数据 在Flash与数据库通讯的实际应用中,如何实现用户的登录与注册是经常遇到的一个问题。登录实际上就是ASP根据Flash提供的数据查询数据库的过程,而注册则是ASP将Flash提供的数据写入数据库的过程。 1.启动Access2003&…...

[C#]winform部署yolov7+CRNN实现车牌颜色识别车牌号检测识别
【官方框架地址】 https://github.com/WongKinYiu/yolov7.git 【框架介绍】 Yolov7是一种目标检测算法,全称You Only Look Once version 7。它是继Yolov3和Yolov4之后的又一重要成果,是目标检测领域的一个重要里程碑。 Yolov7在算法结构上继承了其前…...

VBA技术资料MF111:将表对象转换为正常范围
我给VBA的定义:VBA是个人小型自动化处理的有效工具。利用好了,可以大大提高自己的工作效率,而且可以提高数据的准确度。我的教程一共九套,分为初级、中级、高级三大部分。是对VBA的系统讲解,从简单的入门,到…...

Nginx代理服务器、HTTP调度、TCP/UDP调度、Nginx优化、HTTP错误代码、状态页面、压力测试
1 案例1:Nginx反向代理 1.1 问题 使用Nginx实现Web反向代理功能,实现如下功能: 后端Web服务器两台,可以使用httpd实现Nginx采用轮询的方式调用后端Web服务器两台Web服务器的权重要求设置为不同的值最大失败次数为2,…...

从 React 到 Qwik:开启高效前端开发的新篇章
1. Qwik Qwik 是一个为构建高性能的 Web 应用程序而设计的前端 JavaScript 框架,它专注于提供即时启动性能,即使是在移动设备上。Qwik 的关键特性是它采用了称为“恢复性”的技术,该技术消除了传统前端框架中常见的 hydration 过程。 恢复性是一种序列化和恢复应用程序状态…...

【lodash.js】非常好用高性能的 JavaScript 实用工具库,防抖,深克隆,排序等
前言:lodash是一款前端必须要知道的js库,它里面提供了许多常用的功能和实用的工具函数 基本上我参与的项目中都有lodash,只能说lodash太强大了,lodash.js 提供了超过 300 个实用的工具函数,涵盖了很多常见的编程任务 l…...

JS中的try...catch
一、定义和结构 作用:捕获同步执行代码下的异常错误 在没有使用try...catch的情况下,同步代码执行遇到异常会报错,并中断后续代码执行; 在使用try...catch的情况下,同步代码执行遇到异常会抛出异常,并继续…...

选择海外云手机需要考虑什么?
随着跨境电商行业的蓬勃发展,企业们纷纷寻找提升平台流量和广告投放效果的方法,这已成为业界的当务之急。传统的宣传模式在国内受到直播和链接带货等新兴方式的冲击,而在国外,类似的趋势也在悄然兴起,呈现出广阔的发展…...

物联网协议Coap之C#基于Mozi的CoapClient调用解析
目录 前言 一、CoapClient相关类介绍 1、CoapClient类图 2、CoapClient的设计与实现 3、SendMessage解析 二、Client调用分析 1、创建CoapClient对象 2、实际发送请求 3、Server端请求响应 4、控制器寻址 总结 前言 在之前的博客内容中,关于在ASP.Net Co…...
java中如何使用Lambda表达式(一)
什么是Lambda表达式 Lambda 表达式(lambda expression)是一个匿名函数,基于数学中的λ演算得名,直接对应于其中的lambda抽象(lambda abstraction),是一个没有函数名的函数。Lambda表达式可以表…...

C++继承详解
继承 1. 继承的概念和定义1.1 继承的概念1.2 继承的定义1.2.1 继承的格式1.2.2 继承方式 2. 基类和派生类对象的赋值转换3.继承中的作用域4. 继承中的默认成员函数5. 继承和友元6. 继承和静态成员 1. 继承的概念和定义 1.1 继承的概念 继承是面向对象编程中的一个重要概念&…...
docker数据卷的使用
文章目录 1、数据卷产生背景2、数据卷的使用2.1、创建数据卷2.2、挂载数据卷2.3、共享数据卷2.4、删除数据卷2.5、备份和迁移数据卷 总结 1、数据卷产生背景 Docker的镜像是由一系列的只读层组合而来,当启动一个容器时,Docker加载镜像的所有只读层&…...

2024獬豸杯完整Writeup
文章目录 手机手机基本信息- 1、IOS手机备份包是什么时候开始备份的。(标准格式:2024-01-20.12:12:12)手机基本信息- 2、请分析,该手机共下载了几款即时通讯工具。(标准格式:阿拉伯数字)手机基本信息- 3、手…...
Vue学习笔记之应用创建和基础知识
1、安装方式 CDN方式安装: <script src"https://unpkg.com/vue3/dist/vue.global.js"></script> 2、创建应用 使用Vue内置对象创建一个应用,基本代码结构如下: <script src"https://unpkg.com/vue3/dist/…...

CSS3基础知识总结
目录 一、CSS3 边框 1.border-radius:圆角边框 2.box-shadow:添加阴影 3.border-image:图片边框 二、CSS3 渐变 1.线性渐变(Linear Gradients) a.由上到下(默认) b.从左到右 c.对角 d.使用角度 2.径向渐变(…...

80.网游逆向分析与插件开发-背包的获取-自动化助手显示物品数据1
内容参考于:易道云信息技术研究院VIP课 上一个内容:升级Notice类获得背包基址-CSDN博客 码云地址(ui显示角色数据 分支):https://gitee.com/dye_your_fingers/sro_-ex.git 码云版本号:3be017de38c50653b…...
【根据当天日期输出明天的日期(需对闰年做判定)。】2022-5-15
缘由根据当天日期输出明天的日期(需对闰年做判定)。日期类型结构体如下: struct data{ int year; int month; int day;};-编程语言-CSDN问答 struct mdata{ int year; int month; int day; }mdata; int 天数(int year, int month) {switch (month){case 1: case 3:…...

安宝特方案丨XRSOP人员作业标准化管理平台:AR智慧点检验收套件
在选煤厂、化工厂、钢铁厂等过程生产型企业,其生产设备的运行效率和非计划停机对工业制造效益有较大影响。 随着企业自动化和智能化建设的推进,需提前预防假检、错检、漏检,推动智慧生产运维系统数据的流动和现场赋能应用。同时,…...
Qt Widget类解析与代码注释
#include "widget.h" #include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget) {ui->setupUi(this); }Widget::~Widget() {delete ui; }//解释这串代码,写上注释 当然可以!这段代码是 Qt …...
连锁超市冷库节能解决方案:如何实现超市降本增效
在连锁超市冷库运营中,高能耗、设备损耗快、人工管理低效等问题长期困扰企业。御控冷库节能解决方案通过智能控制化霜、按需化霜、实时监控、故障诊断、自动预警、远程控制开关六大核心技术,实现年省电费15%-60%,且不改动原有装备、安装快捷、…...

Cilium动手实验室: 精通之旅---20.Isovalent Enterprise for Cilium: Zero Trust Visibility
Cilium动手实验室: 精通之旅---20.Isovalent Enterprise for Cilium: Zero Trust Visibility 1. 实验室环境1.1 实验室环境1.2 小测试 2. The Endor System2.1 部署应用2.2 检查现有策略 3. Cilium 策略实体3.1 创建 allow-all 网络策略3.2 在 Hubble CLI 中验证网络策略源3.3 …...

Cinnamon修改面板小工具图标
Cinnamon开始菜单-CSDN博客 设置模块都是做好的,比GNOME简单得多! 在 applet.js 里增加 const Settings imports.ui.settings;this.settings new Settings.AppletSettings(this, HTYMenusonichy, instance_id); this.settings.bind(menu-icon, menu…...
C++ 基础特性深度解析
目录 引言 一、命名空间(namespace) C 中的命名空间 与 C 语言的对比 二、缺省参数 C 中的缺省参数 与 C 语言的对比 三、引用(reference) C 中的引用 与 C 语言的对比 四、inline(内联函数…...

mysql已经安装,但是通过rpm -q 没有找mysql相关的已安装包
文章目录 现象:mysql已经安装,但是通过rpm -q 没有找mysql相关的已安装包遇到 rpm 命令找不到已经安装的 MySQL 包时,可能是因为以下几个原因:1.MySQL 不是通过 RPM 包安装的2.RPM 数据库损坏3.使用了不同的包名或路径4.使用其他包…...
鸿蒙DevEco Studio HarmonyOS 5跑酷小游戏实现指南
1. 项目概述 本跑酷小游戏基于鸿蒙HarmonyOS 5开发,使用DevEco Studio作为开发工具,采用Java语言实现,包含角色控制、障碍物生成和分数计算系统。 2. 项目结构 /src/main/java/com/example/runner/├── MainAbilitySlice.java // 主界…...
Redis的发布订阅模式与专业的 MQ(如 Kafka, RabbitMQ)相比,优缺点是什么?适用于哪些场景?
Redis 的发布订阅(Pub/Sub)模式与专业的 MQ(Message Queue)如 Kafka、RabbitMQ 进行比较,核心的权衡点在于:简单与速度 vs. 可靠与功能。 下面我们详细展开对比。 Redis Pub/Sub 的核心特点 它是一个发后…...