服务器端

package com.ronnie.nio.groupChat;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.util.Iterator; public class GroupChatServer { private Selector selector;
private ServerSocketChannel listenChannel;
private static final int PORT = 9999; /* 构造器
初始化任务
*/
public GroupChatServer(){
try {
// 得到选择器
selector = Selector.open();
// serverSocketChannel
listenChannel = ServerSocketChannel.open();
// 绑定端口
listenChannel.socket().bind(new InetSocketAddress(PORT));
// 设置非阻塞模式
listenChannel.configureBlocking(false);
// 将该listenChannel 注册到Selector
listenChannel.register(selector, SelectionKey.OP_ACCEPT); }catch (IOException e){
e.printStackTrace();
}
} /**
* 监听
*/
public void listen(){
try {
// 循环处理
while (true){
int count = selector.select(2000);
if (count > 0){ // 有事件处理
// 遍历得到的selectionKey集合
Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
while (iterator.hasNext()){
// 取出selectionKey
SelectionKey key = iterator.next(); // 监听到OP_ACCEPT
if (key.isAcceptable()){
SocketChannel sc = listenChannel.accept();
sc.configureBlocking(false);
// 将该socketChannel注册到Selector
sc.register(selector, SelectionKey.OP_READ);
// 提示
System.out.println(sc.getRemoteAddress() + " connected to the chat");
}
// 通道可读
if (key.isReadable()){
// TODO处理读
readData(key);
}
// 当前的key删除, 防止重复处理
iterator.remove();
}
} else {
System.out.println("Waiting......");
}
}
}catch (Exception e){
e.printStackTrace();
} finally {
// 发生异常处理
}
} /**
* 读取客户端消息
* @param key
*/
private void readData(SelectionKey key){
// 定义一个SocketChannel
SocketChannel channel = null;
try {
// 得到channel
channel = (SocketChannel) key.channel(); // 创建缓冲buffer
ByteBuffer buffer = ByteBuffer.allocate(1024); int count = channel.read(buffer);
// 根据count的值做处理
if (count > 0){
// 把缓冲区数据转为字符串并输出
String msg = new String(buffer.array());
// 输出该消息
System.out.println("from Client: " + msg); // 向其他的客户端转发消息(去掉自己)
sendInfoToOtherClients(msg,channel);
}
} catch (IOException e){
try {
System.out.println(channel.getRemoteAddress() + " is offline");
// 取消注册
key.cancel();
// 关闭通道
channel.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
} /**
* 转发消息给其他客户端(channel)
* @param msg
* @param self
*/
private void sendInfoToOtherClients(String msg, SocketChannel self) throws IOException {
System.out.println("Server is transferring messages......");
// 遍历, 所有注册到selector上的 SocketChannel, 并派出 自己
for (SelectionKey key : selector.keys()){ // 通过key取出对应的SocketChannel
Channel targetChannel = key.channel(); // 排除自己
if (targetChannel instanceof SocketChannel && targetChannel != self){ // 转型
SocketChannel dest = (SocketChannel) targetChannel; // 将消息存储到buffer
ByteBuffer buffer = ByteBuffer.wrap(msg.getBytes()); // 将buffer数据写入到通道
dest.write(buffer);
}
}
} public static void main(String[] args) { // 创建服务器对象
GroupChatServer groupChatServer = new GroupChatServer();
groupChatServer.listen();
}
}

客户端

package com.ronnie.nio.groupChat;

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.util.Arrays;
import java.util.Iterator;
import java.util.Scanner; public class GroupChatClient { // 定义相关属性 private final int PORT = 9999;
private Selector selector;
private SocketChannel socketChannel;
private java.lang.String username; /**
* 构造器, 完成初始化工作
*/
public GroupChatClient() throws IOException { selector = Selector.open(); // 连接服务器
socketChannel = SocketChannel.open(new InetSocketAddress("127.0.0.1", PORT)); // 设置非阻塞
socketChannel.configureBlocking(false); // 将channel 注册到selector
socketChannel.register(selector, SelectionKey.OP_READ); // 得到username
username = socketChannel.getLocalAddress().toString().substring(1); System.out.println(username + " is fine"); } /**
* 向服务器发送消息
* @param info
*/
public void sendInfo(java.lang.String info){
info = username + " said: " + info; try {
socketChannel.write(ByteBuffer.wrap(info.getBytes()));
} catch (IOException e) {
e.printStackTrace();
}
} public void readInfo(){ try{
int readChannels = selector.select(); // 有可用的通道
if (readChannels > 0){
Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
while (iterator.hasNext()){
SelectionKey key = iterator.next();
// 客户端只考虑可读
if (key.isReadable()){
// 得到相关通道
SocketChannel sc = (SocketChannel) key.channel();
// 得到一个Buffer
ByteBuffer buffer = ByteBuffer.allocate(1024);
// 读取
sc.read(buffer);
// 把读到的缓冲区数据转成字符串
String msg = Arrays.toString((buffer.array()));
System.out.println(msg.trim());
}
}
iterator.remove(); // 删除当前的selectionKey, 防止重复操作
} else {
System.out.println("No channel available");
}
} catch (Exception e) {
e.printStackTrace();
}
} public static void main(String[] args) throws IOException { // 启动客户端
GroupChatClient chaClient = new GroupChatClient(); // 启动一个线程
new Thread(){
@Override
public void run() {
while (true){
chaClient.readInfo(); try {
Thread.currentThread().sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start(); // 发送数据给服务器端
Scanner scanner = new Scanner(System.in); while (scanner.hasNext()){
String s = scanner.nextLine();
chaClient.sendInfo(s);
}
} }
  • PS: 这种代码不是天天敲是不可能很熟的, 只是找找感觉, 哪天真用到了回来看看以前的博客......

NIO 聊天室代码实现的更多相关文章

  1. Java NIO 聊天室实例

    最近写了个Java NIO聊天室聊天的程序,NIO学习起来比较困难的,我的代码能给大家起到一个抛砖引玉的作用! 服务端: package test.javanio; /** * @author * @ ...

  2. 简单的聊天室代码php+swoole

    php swoole+websocket 客户端代码 <!DOCTYPE html> <html> <head> <title></title&g ...

  3. Java 多线程Socket编程通讯--实现聊天室代码

    1.创建服务器类 import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; import ja ...

  4. 使用 NIO 搭建一个聊天室

    使用 NIO 搭建一个聊天室 前面刚讲了使用 Socket 搭建了一个 Http Server,在最后我们使用了 NIO 对 Server 进行了优化,然后有小伙伴问到怎么使用 Socket 搭建聊天 ...

  5. 【总结】学习Socket编写的聊天室小程序

    1.前言 在学习Socket之前,先来学习点网络相关的知识吧,自己学习过程中的一些总结,Socket是一门很高深的学问,本文只是Socket一些最基础的东西,大神请自觉绕路. 传输协议 TCP:Tra ...

  6. 夺命雷公狗---node下的一聊天室-首发

    今晚感觉挺蛋疼,就用了点时间,在node下开发了个聊天室来玩玩,不过之是简单的开发了套而已,并没多做什么考虑,, 但是发现了一个好处就是用node来写聊天室代码居然少得可怜,这个不佩服node都不行, ...

  7. PHP+ajax聊天室源码!支持长轮循跟定时请求两种

      var lastID = "1";//声明上次取回的消息的ID var isposted = false; var mGetTime;//设置setTimeout的返回值 // ...

  8. C 基于UDP实现一个简易的聊天室

    引言 本文是围绕Linux udp api 构建一个简易的多人聊天室.重点看思路,帮助我们加深 对udp开发中一些api了解.相对而言udp socket开发相比tcp socket开发注意的细节要少 ...

  9. Socket.io官方聊天室DEMO的学习笔记

    照着Socket.io官方的聊天室代码敲了一遍,遇到了一个奇怪的问题: 每次点击SEND按钮的时候,都会重新刷新页面. 在点击页面的一瞬间,看到了正在加载jquery的提示, 然后以为是jquery用 ...

随机推荐

  1. Eclipse开发快捷键

    ctrl+alt+r:查找资源 ctrl+o:快速outLine ctrl+e:快速切换编辑器 ctrl+./ctrl+1:下一个错误修改

  2. Spring学习(一)

    搭建环境 1.创建普通的Java工程 2.添加相应的jar包,下载链接:https://files.cnblogs.com/files/AmyZheng/lib.rar,此外,为了打印信息,我们还需要 ...

  3. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 排版:设定文本小写

    <!DOCTYPE html> <html> <head> <title>菜鸟教程(runoob.com)</title> <meta ...

  4. Java 并发锁

    Java 中的锁 阻塞锁.可重入锁.读写锁.互斥锁.悲观锁.乐观锁.公平锁.偏向锁.对象锁.线程锁.锁粗化.锁消除.轻量级锁.重量级锁.信号量.独享锁.共享锁.分段锁 一.常见的锁 synchroni ...

  5. swoole 监控文件改动

    <?php /** * 场景: * 进程监控文件改动 */ date_default_timezone_set('PRC'); echo '进程id:' . posix_getpid() . P ...

  6. ARM CORTEX-M3 内核架构理解归纳

    ARM CORTEX-M3 内核架构理解归纳 来源:网络 个人觉得对CM3架构归纳的非常不错,因此转载 基于<ARM-CORTEX M3 权威指南>做学习总结: 在我看来,Cotex-M3 ...

  7. Java-用星号打印菱形

    打印如图所示菱形9行9列(提示可以将菱形分成上下两个三角形,分析每行空格数和星号个数的关系) 代码如下: package com.homework.lhh; public class Ex20 { p ...

  8. python3爬虫

    1.爬虫的基本原理讲解 2.Urllib库的基本使用 3.Requests库的基本使用 4.正则的基本使用 5.BeautifulSoup库的使用 6.PyQuery库的使用   √ 7.Seleni ...

  9. 127、Java面向对象之对象的比较

    01.代码如下: package TIANPAN; class Book { private String title; private double price; public Book(Strin ...

  10. 微信小程序中,如何点击链接跳转到外部网页

    跳转到内部链接 这个我们应该都知道,通过wx.navigateTo,wx.redirectTo,wx.swtichTab等小程序内部的方法,可以直接跳转到小程序内部已经注册的(就是在app.json中 ...