JAVA NIO 实现群聊

一、群聊服务器

package com.dashu.netty.group_chat;import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.nio.charset.StandardCharsets;
import java.util.Iterator;public class GroupChatServer {/*** 初始化选择器*/private Selector selector;/*** 初始化服务器网络通道*/private ServerSocketChannel serverSocketChannel;/*** 端口*/private static final int PORT = 6666;/*** 构造方法*/public GroupChatServer() {try {//获取选择器selector = Selector.open();//获取服务器网络通道serverSocketChannel = ServerSocketChannel.open();//网络地址InetSocketAddress inetSocketAddress = new InetSocketAddress(PORT);//服务器网络通道绑定网络地址serverSocketChannel.socket().bind(inetSocketAddress);//设置服务器网络通道非阻塞serverSocketChannel.configureBlocking(false);//将服务器网络通道注册到选择器上,绑定连接请求事件serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);} catch (Exception e) {e.printStackTrace();}}/*** 监听客户端请求事件*/public void listen() {try {//无限循环while (true) {//获取请求数int count = selector.select();//count大于0,则代表有请求进来if (count > 0) {//获取请求集Iterator<SelectionKey> selectionKeyIterator = selector.selectedKeys().iterator();//遍历请求集while (selectionKeyIterator.hasNext()) {//得到请求SelectionKey selectionKey = selectionKeyIterator.next();//连接请求if (selectionKey.isAcceptable()) {//获取客户端网络通道SocketChannel socketChannel = serverSocketChannel.accept();//设置客户端网络通道非阻塞socketChannel.configureBlocking(false);//将客户端网络通道注册到选择器上socketChannel.register(selector, SelectionKey.OP_READ);System.out.println(socketChannel.getRemoteAddress() + "上线了");}//信息读取请求if (selectionKey.isReadable()) {//客户端信息读取readData(selectionKey);}//移除请求selectionKeyIterator.remove();}} else {System.out.println("等待...");}}} catch (Exception e) {e.printStackTrace();}}/*** 客户端信息读取** @param selectionKey*/private void readData(SelectionKey selectionKey) {//初始化客户端网络通道SocketChannel socketChannel = null;try {//获取客户端网络通道socketChannel = (SocketChannel) selectionKey.channel();//创建缓冲区ByteBuffer byteBuffer = ByteBuffer.allocate(1024);//读取客户端网络通道中的数据到缓冲区int count = socketChannel.read(byteBuffer);//判断缓冲区中是否有数据if (count > 0) {//将缓冲区的数据转换位字符串String message = new String(byteBuffer.array());System.out.println(message.trim());//将信息群发到其他客户端sendInfoToOtClients(message, socketChannel);}} catch (Exception e) {e.printStackTrace();}}/*** 将信息群发到其他客户端** @param message* @param socketChannel*/private void sendInfoToOtClients(String message, SocketChannel socketChannel) {//获取所有注册到选择器的客户端,并遍历for (SelectionKey selectionKey : selector.keys()) {//获取通道Channel channel = selectionKey.channel();//判断通道是否属于SocketChannel,同时不等于发送信息的客户端if (channel instanceof SocketChannel && channel != socketChannel) {//通道转换SocketChannel sc = (SocketChannel) channel;//将信息写入缓冲区ByteBuffer byteBuffer = ByteBuffer.wrap(message.getBytes(StandardCharsets.UTF_8));try {//将缓冲区的数据写入通道sc.write(byteBuffer);} catch (Exception e) {e.printStackTrace();}}}}public static void main(String[] args) {GroupChatServer groupChatServer = new GroupChatServer();System.out.println("服务器启动,开始监听客户端请求...");groupChatServer.listen();}}

二、客户端

package com.dashu.netty.group_chat;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;public class GroupChatClient {/*** 网络连接地址*/private final String HOST = "127.0.0.1";/*** 端口*/private final int PORT = 6666;/*** 初始化选择器*/private Selector selector;/*** 初始化网络通道*/private SocketChannel socketChannel;/*** 用户名*/private String username;public GroupChatClient() {try {//获取选择器selector = Selector.open();//获取服务器网络地址InetSocketAddress inetSocketAddress = new InetSocketAddress(HOST, PORT);//获取网络通道socketChannel = SocketChannel.open(inetSocketAddress);//设置网络通道非阻塞socketChannel.configureBlocking(false);//将网络通道注册到选择器socketChannel.register(selector, SelectionKey.OP_READ);//获取用户名System.out.println("请输入用户名:");Scanner scanner = new Scanner(System.in);username = scanner.nextLine();System.out.println(username + " 进入群聊...");} catch (Exception e) {e.printStackTrace();}}/*** 向服务器发送信息** @param message*/public void sendInfo(String message) {message = username + ":" + message;try {//向通道写入数据socketChannel.write(ByteBuffer.wrap(message.getBytes(StandardCharsets.UTF_8)));} catch (Exception e) {e.printStackTrace();}}/*** 读取服务器发来的信息*/public void readInfo() {try {//获取请求数int count = selector.select();if (count > 0) {//获取请求集Iterator<SelectionKey> selectionKeyIterator = selector.selectedKeys().iterator();//遍历请求集while (selectionKeyIterator.hasNext()) {//获取请求SelectionKey selectionKey = selectionKeyIterator.next();//判断位读请求if (selectionKey.isReadable()) {//获取通道SocketChannel sc = (SocketChannel) selectionKey.channel();//创建缓冲区ByteBuffer byteBuffer = ByteBuffer.allocate(1024);//读取通道的数据到缓冲区sc.read(byteBuffer);//缓冲区数据转字符串String message = new String(byteBuffer.array());//输出System.out.println(message.trim());}//移除已完成请求selectionKeyIterator.remove();}}} catch (Exception e) {e.printStackTrace();}}public static void main(String[] args) {GroupChatClient groupChatClient = new GroupChatClient();/*** 开启一个线程,每3秒读取一次服务器发来的信息*/new Thread() {@Overridepublic void run() {while (true) {groupChatClient.readInfo();try {Thread.sleep(3000);} catch (Exception e) {e.printStackTrace();}}}}.start();//信息输入Scanner scanner = new Scanner(System.in);System.out.println("请输入信息:");while (scanner.hasNextLine()) {String s = scanner.nextLine();//信息发送groupChatClient.sendInfo(s);System.out.println("请输入信息:");}}}

三、效果图

1、服务器

2、客户端01

3、客户端02

JAVA NIO 实现群聊相关推荐

  1. java nio netty 教程,4. 彤哥说netty系列之Java NIO实现群聊(自己跟自己聊上瘾了),netty实现...

    4. 彤哥说netty系列之Java NIO实现群聊(自己跟自己聊上瘾了),netty实现 你好,我是彤哥,本篇是netty系列的第四篇. 欢迎来我的公从号彤哥读源码系统地学习源码&架构的知识 ...

  2. 4. 彤哥说netty系列之Java NIO实现群聊(自己跟自己聊上瘾了)

    你好,我是彤哥,本篇是netty系列的第四篇. 欢迎来我的公从号彤哥读源码系统地学习源码&架构的知识. 简介 上一章我们一起学习了Java中的BIO/NIO/AIO的故事,本章将带着大家一起使 ...

  3. java 仿qq庅,4. 彤哥说netty系列之Java NIO实现群聊(自己跟自己聊上瘾了)

    你好,我是彤哥,本篇是netty系列的第四篇. 欢迎来我的公从号彤哥读源码系统地学习源码&架构的知识. 简介 上一章我们一起学习了Java中的BIO/NIO/AIO的故事,本章将带着大家一起使 ...

  4. 4.基于NIO的群聊系统

    [README] 1.本文总结自B站<netty-尚硅谷>,很不错: 2.文末有错误及解决方法: [1]群聊需求 1)编写一个 NIO 群聊系统,实现服务器端和客户端之间的数据简单通讯(非 ...

  5. java如何实现群聊,用Java控制台实现简单的群聊天室

    需求分析: 实现简单的群聊,需要用到Java的Socket(套接字)和ServerSocket(服务器端套接字)两个类.当客户端(Socket)发送信息到固定的服务器(ServerSocket)的地址 ...

  6. java仿qq群聊_[转载]仿QQ聊天室群聊的练习心得

    javase的学习即将告一段落,作为最后的一个项目练习,仿聊天室的程序编写让我很是头疼了一阵子.说起来还是自己java基础不牢的缘故导致的,虽然整体框架都已经很清晰了但是实际编写过程中却依然磕磕绊绊, ...

  7. TCP多人聊天程序Java实现(群聊,私聊,在线用户,踢出用户)

    本程序在程序 https://blog.csdn.net/joffy/article/details/18079331 的基础是上添加了私聊,踢出用户两个功能. 由客户端和服务器端构成程序,程序借助J ...

  8. IM消息重试机制Java实现_IM群聊消息的已读回执功能该怎么实现?

    本文引用了架构师之路公众号作者沈剑的文章,内容有改动,感谢原作者. 1.前言我们平时在使用即时通讯应用时候,每当发出一条聊天消息,都希望对方尽快看到,并尽快回复,但对方到底有没有真的看到?我却并不知道 ...

  9. java nio集群_java – Hazelcast:连接到远程集群

    14:58:26.717 [main] INFO c.m.b.p.s.s.HazelcastCacheClient – creating new Hazelcast instance 14:58:26 ...

  10. 基于Java的局域网群聊软件

    基于TCP/IP协议,包含一个客户端和一个服务器端.目前只能字符聊天. 主要功能:实现多个客户端连接服务器,服务器接收到信息就会把信息广播到所有的客户端 1.Server端:包括端口设置,向所有人发信 ...

最新文章

  1. 如何设置chrome谷歌浏览器不显示图片
  2. ajax异步后台存放购物车表,jQuery购物车插件jsorder用法(支持后台处理程序直接转换成DataTable处理)...
  3. 网易智慧企业Node.js实践(1) | Node应用架构设计和React同构
  4. bt解析 开源 java_修复开源项目 btcd RPC 实现比特币获取区块的问题
  5. Magento教程 2:Magento 社群版安装教学!
  6. 天声人語2008年05月04日-蔬菜的阴谋
  7. Vc数据库编程基础1
  8. 'WebElement' object is not iterable
  9. 腾讯看点基于 Flink 的实时数仓及多维实时数据分析实践
  10. win7系统任务栏不见了怎么办
  11. 桌面窗口管理器占用过高解决办法
  12. Songtaste,酷到不行的音乐网站
  13. jQuery高级选择器
  14. linux下系统中的文件传输
  15. 【spring】spring 的事务(transaction) 四 嵌套事务PROPAGATION_NESTED
  16. java具名参数_Spring JDBC 框架中, 绑定 SQL 参数的另一种选择:具名参数(named parameter)...
  17. 《每日一套题·提升你我能力》· 第五篇
  18. SQL高级——PLSQL数据库编程
  19. 淘宝API应用开发小试
  20. 信息系统项目管理案例分析

热门文章

  1. linux kvm装ghost镜像,kvm安装win7虚拟机
  2. oceanbase ODC和Obclient连接mysql类型的ob库
  3. java 纯真ip数据库_纯真IP数据库格式读取方法(JAVA/PHP/Python)
  4. 天勤数据结构——绪论
  5. 软件质量管理-考试复习总结
  6. 基于js alert confirm样式弹出框
  7. sip网络电话 用户一直注册超时无法注册成功
  8. C语言 队列的实现(链表实现)
  9. 用python进行小波包分解
  10. matlab得到小波参数,MATLAB|高频信号的小波分析技术要点