Java NIO实战之多人聊天室
作者:红字V 发布时间:2022-02-28 15:05:00
标签:java,NIO,聊天室
本文实例为大家分享了Java NIO实战之多人聊天室的具体代码,供大家参考,具体内容如下
NIO服务端
public class NioServer {
/**
* 启动
*/
public void start() throws IOException {
/**
* 1. 创建Selector
*/
Selector selector = Selector.open();
/**
* 2. 通过ServerSocketChannel创建channel通道
*/
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
/**
* 3. 为channel通道绑定监听端口
*/
serverSocketChannel.bind(new InetSocketAddress(8000));
/**
* 4. **设置channel为非阻塞模式**
*/
serverSocketChannel.configureBlocking(false);
/**
* 5. 将channel注册到selector上,监听连接事件
*/
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
System.out.println("服务器启动成功!");
/**
* 6. 循环等待新接入的连接
*/
for (;;) { // while(true) c for;;
/**
* TODO 获取可用channel数量
*/
int readyChannels = selector.select();
/**
* TODO 为什么要这样!!?
*/
if (readyChannels == 0) continue;
/**
* 获取可用channel的集合
*/
Set<SelectionKey> selectionKeys = selector.selectedKeys();
Iterator iterator = selectionKeys.iterator();
while (iterator.hasNext()) {
/**
* selectionKey实例
*/
SelectionKey selectionKey = (SelectionKey) iterator.next();
/**
* **移除Set中的当前selectionKey**
*/
iterator.remove();
/**
* 7. 根据就绪状态,调用对应方法处理业务逻辑
*/
/**
* 如果是 接入事件
*/
if (selectionKey.isAcceptable()) {
acceptHandler(serverSocketChannel, selector);
}
/**
* 如果是 可读事件
*/
if (selectionKey.isReadable()) {
readHandler(selectionKey, selector);
}
}
}
}
/**
* 接入事件处理器
*/
private void acceptHandler(ServerSocketChannel serverSocketChannel,
Selector selector)
throws IOException {
/**
* 如果要是接入事件,创建socketChannel
*/
SocketChannel socketChannel = serverSocketChannel.accept();
/**
* 将socketChannel设置为非阻塞工作模式
*/
socketChannel.configureBlocking(false);
/**
* 将channel注册到selector上,监听 可读事件
*/
socketChannel.register(selector, SelectionKey.OP_READ);
/**
* 回复客户端提示信息
*/
socketChannel.write(Charset.forName("UTF-8")
.encode("你与聊天室里其他人都不是朋友关系,请注意隐私安全"));
}
/**
* 可读事件处理器
*/
private void readHandler(SelectionKey selectionKey, Selector selector)
throws IOException {
/**
* 要从 selectionKey 中获取到已经就绪的channel
*/
SocketChannel socketChannel = (SocketChannel) selectionKey.channel();
/**
* 创建buffer
*/
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
/**
* 循环读取客户端请求信息
*/
String request = "";
while (socketChannel.read(byteBuffer) > 0) {
/**
* 切换buffer为读模式
*/
byteBuffer.flip();
/**
* 读取buffer中的内容
*/
request += Charset.forName("UTF-8").decode(byteBuffer);
}
/**
* 将channel再次注册到selector上,监听他的可读事件
*/
socketChannel.register(selector, SelectionKey.OP_READ);
/**
* 将客户端发送的请求信息 广播给其他客户端
*/
if (request.length() > 0) {
// 广播给其他客户端
broadCast(selector, socketChannel, request);
}
}
/**
* 广播给其他客户端
*/
private void broadCast(Selector selector,
SocketChannel sourceChannel, String request) {
/**
* 获取到所有已接入的客户端channel
*/
Set<SelectionKey> selectionKeySet = selector.keys();
/**
* 循环向所有channel广播信息
*/
selectionKeySet.forEach(selectionKey -> {
Channel targetChannel = selectionKey.channel();
// 剔除发消息的客户端
if (targetChannel instanceof SocketChannel
&& targetChannel != sourceChannel) {
try {
// 将信息发送到targetChannel客户端
((SocketChannel) targetChannel).write(
Charset.forName("UTF-8").encode(request));
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
/**
* 主方法
* @param args
*/
public static void main(String[] args) throws IOException {
new NioServer().start();
}
}
NIO客户端
public class NioClient {
/**
* 启动
*/
public void start(String nickname) throws IOException {
/**
* 连接服务器端
*/
SocketChannel socketChannel = SocketChannel.open(
new InetSocketAddress("127.0.0.1", 8000));
/**
* 接收服务器端响应
*/
// 新开线程,专门负责来接收服务器端的响应数据
// selector , socketChannel , 注册
Selector selector = Selector.open();
socketChannel.configureBlocking(false);
socketChannel.register(selector, SelectionKey.OP_READ);
new Thread(new NioClientHandler(selector)).start();
/**
* 向服务器端发送数据
*/
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextLine()) {
String request = scanner.nextLine();
if (request != null && request.length() > 0) {
socketChannel.write(
Charset.forName("UTF-8")
.encode(nickname + " : " + request));
}
}
}
public static void main(String[] args) throws IOException {
// new NioClient().start();
}
}
客户端线程,处理服务器端响应的的消息
public class NioClientHandler implements Runnable {
private Selector selector;
public NioClientHandler(Selector selector) {
this.selector = selector;
}
@Override
public void run() {
try {
for (;;) {
int readyChannels = selector.select();
if (readyChannels == 0) continue;
/**
* 获取可用channel的集合
*/
Set<SelectionKey> selectionKeys = selector.selectedKeys();
Iterator iterator = selectionKeys.iterator();
while (iterator.hasNext()) {
/**
* selectionKey实例
*/
SelectionKey selectionKey = (SelectionKey) iterator.next();
/**
* **移除Set中的当前selectionKey**
*/
iterator.remove();
/**
* 7. 根据就绪状态,调用对应方法处理业务逻辑
*/
/**
* 如果是 可读事件
*/
if (selectionKey.isReadable()) {
readHandler(selectionKey, selector);
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 可读事件处理器
*/
private void readHandler(SelectionKey selectionKey, Selector selector)
throws IOException {
/**
* 要从 selectionKey 中获取到已经就绪的channel
*/
SocketChannel socketChannel = (SocketChannel) selectionKey.channel();
/**
* 创建buffer
*/
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
/**
* 循环读取服务器端响应信息
*/
String response = "";
while (socketChannel.read(byteBuffer) > 0) {
/**
* 切换buffer为读模式
*/
byteBuffer.flip();
/**
* 读取buffer中的内容
*/
response += Charset.forName("UTF-8").decode(byteBuffer);
}
/**
* 将channel再次注册到selector上,监听他的可读事件
*/
socketChannel.register(selector, SelectionKey.OP_READ);
/**
* 将服务器端响应信息打印到本地
*/
if (response.length() > 0) {
System.out.println(response);
}
}
}
我们定义三个客户端,模拟三个用户在聊天室发送消息
public class AClient {
public static void main(String[] args)
throws IOException {
new NioClient().start("AClient");
}
}
public class BClient {
public static void main(String[] args)
throws IOException {
new NioClient().start("BClient");
}
}
public class CClient {
public static void main(String[] args)
throws IOException {
new NioClient().start("CClient");
}
}
NIO 聊天室到此结束
来源:https://blog.csdn.net/sinat_36899414/article/details/106169245
0
投稿
猜你喜欢
- Spring和SpringMVC的容器具有父子关系,Spring容器为父容器,SpringMVC为子容器,子容器可以引用父容器中的Bean,
- 经测试,是环绕通知改变了返回值,切面方法需要有返回值,来代替被代理方法返回结果改成如下即可:@Around("point_upda
- 一、打印直角三角形这个循环控制打印十行空格for (int x = 1; x <= 10; x++) {//因为要打印一个十行的直角三
- public class MD5Check {/*** 默认的密码字符串组合,用来将字节转换成 16 进制表示的字符,apache校验下载的
- ELK是三款软件的简称,分别是Elasticsearch、Logstash、Kibana组成,在发展的过程中,又有新成员Beats的加入,所
- 状态分类在Hibernate框架中,为了管理持久化类,Hibernate将其分为了三个状态:瞬时态(Transient Object)持久态
- 文件比较平常都是用Beyond Compare,可以说离不开的神器,特别是针对代码比较这块,确实挺好用的。不过Beyond Compare平
- 总体实现思路是启动一个生产者项目注册, 将所含服务注册到zookeeper的注册中心, 然后在启动一个消费者项目,将所需服务向zookeep
- 前言上篇Java Mybatis数据源之工厂模式文章中我们介绍了Mybatis的数据源模块的DataSource接口和它对应的实现
- 在Java中如果一个类同时继承接口A与B,并且这两个接口中具有同名方法,会怎么样?动手做实验:interface A{ void
- 什么是OKHttp一般在Java平台上,我们会使用Apache HttpClient作为Http客户端,用于发送 HTTP 请求,并对响应进
- 最近经常在机房看同学在玩一个走迷宫的游戏,比较有趣,自己也用java写一个实现随机生成迷宫的算法,其实就是一个图的深度优先遍历算法.基本思想
- 背景在开发中,如果用try catch的方式,每个方法都需要单独实现,为了方便分类异常,返回给前端,采用了@ControllerAdvice
- 现公司架构大佬在项目中使用了 mybatis-generator-gui ,这是一款开源图形化 MyBatis 代码生成工具,使用起来相当的
- 题记:由于业务的需要,需要根据模板定制pdf文档,经测试根据模板导出word成功了;但是导出pdf相对麻烦了一点。两天的研究测试java导出
- idea spring Initializr创建项目勾选项目所需要的依赖pom.xml文件会加载勾选的依赖,也可以不勾选后面通过自己常用的p
- Struts2是流行和成熟的基于MVC设计模式的Web应用程序框架。 Struts2不只是Struts1下一个版本,它是一个完全重写的Str
- 咱们废话不多说进入主题、系统主页展示:用户登录后进行系统首页:主要功能模块如下、分角色管理、超级管理员拥有最高权限、可以进行菜单灵活控制、用
- 一、LinkedHashMap的类继承关系二、源码分析1.自己对LinkedHashMap的理解从继承关系上,我们看到LinkedHashM
- 前言Kotlin一个强大之处就在于它的扩展函数,巧妙的运用这些扩展函数可以让你写出的代码更加优雅,阅读起来更加流畅,下面总结了在开发中经常用