一、实例准备
1.1 实例要求
- 编写一个NIO群聊系统,实现服务器端和客户端之间的数据简单通讯(非阻塞)
- 实现多人群聊
- 服务器端;可以监测用户上线,离线,并实现消息转发功能
- 客户端:通过channel可以无阻塞发送消息给其他所有用户,同时可以接收其他用户发送的消息(有服务器转发得到)
- 目的:进一步理解NIO非阻塞网络编程机制
1.2 结构图
二、 实现服务端代码
package com.GroupChat;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.util.Iterator;
import java.util.Set;
public class GroupChatServer {
//定义属性
private Selector selector;
private ServerSocketChannel listenChannel;
private static final int port=6667;
//构造器 初始化相关配置
public GroupChatServer(){
try{
//获取选择器 selector
selector=Selector.open();
//获取ServerSocketChannel
listenChannel=ServerSocketChannel.open();
//绑定端口
listenChannel.socket().bind(new InetSocketAddress(6667));
//设置非阻塞模式
listenChannel.configureBlocking(false);
//将该listenChannel注册到selector
listenChannel.register(selector, SelectionKey.OP_ACCEPT);
}catch (IOException e){
e.printStackTrace();
}
}
//监听客户端连接方法
public void listen(){
try {
while (true){
// System.out.println("等待事件触发......");
int count = selector.select(2000);
if(count>0){//有事件发生
//遍历得到的selectionKey 集合
Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
while(iterator.hasNext()){
//取出selectionKey
SelectionKey key=iterator.next();
//监听到accept
if(key.isAcceptable()){
SocketChannel sc = listenChannel.accept();
sc.configureBlocking(false);
//将sc注册到selector
sc.register(selector, SelectionKey.OP_READ);
//上线提示
System.out.println(sc.getRemoteAddress()+"上线");
}
if(key.isReadable()){//通道发生read事件,即通道是刻度的事件
//处理read 方法处理
readData(key);
}
//当前的key删除,防止重复处理
iterator.remove();
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
finally {
}
}
//读取客户端消息
public void readData(SelectionKey key) {
//取到关联的channel
SocketChannel channel=null;
try {
//得到channel
channel=(SocketChannel)key.channel();
//创建buffer
ByteBuffer buffer = ByteBuffer.allocate(1024);
int count=channel.read(buffer);
if(count>0){
//把缓冲区的数据转成字符串
String msg=new String(buffer.array());
//输出消息
System.out.println("from 客户端:" + msg);
//向其他客户端转发消息(不包括自己),单独写一个方法进行处理
sendInfoToOtherClient(msg,channel);
}
}catch (IOException e){
try {
System.out.println(channel.getRemoteAddress()+" 离线了....");
key.cancel();//取消通道
channel.close();//关闭通道
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
}
//转发消息给其他客户(通道)
private void sendInfoToOtherClient(String msg,SocketChannel self){
System.out.println("服务器转发消息中....");
//遍历 所有注册到selector上的selectionKey,并排除自己
for (SelectionKey key: selector.keys()){
//通过key 取出相应的SocketChannel
Channel targetChannel = key.channel();
//排除自己
if(targetChannel instanceof SocketChannel && targetChannel!= self){
//转型
SocketChannel dest = (SocketChannel) targetChannel;
//将msg存储到buffer
ByteBuffer buffer = ByteBuffer.wrap(msg.getBytes());
//将buffer的数据写入通道
try {
dest.write(buffer);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
GroupChatServer groupChatServer = new GroupChatServer();
groupChatServer.listen();
}
}
三、客户端代码实现
package com.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.Iterator;
import java.util.Scanner;
public class GroupChatClient {
//定义相关属性
private final String HOST="127.0.0.1";//服务器的ip
private final int port=6667;//服务器的端口
private Selector selector;
private SocketChannel socketChannel;
private String username;
//构造器 完成初始化工作
public GroupChatClient(){
//
try {
//获取Selector 选择器
selector = Selector.open();
//连接服务器
socketChannel = SocketChannel.open(new InetSocketAddress(HOST, port));
//设置非阻塞
socketChannel.configureBlocking(false);
//将channel 注册到Selector
socketChannel.register(selector, SelectionKey.OP_READ);
//得到username
username=socketChannel.getLocalAddress().toString().substring(1);
} catch (IOException e) {
e.printStackTrace();
}
}
//向服务器发送消息
public void sendInfo(String info){
info = username + "说:" + 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 = new String(buffer.array());
System.out.println(msg.trim());
}
iterator.remove();//删除当前的selectionKey 防止重处理
}
}else{
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
//启动客户端
GroupChatClient chatClient = new GroupChatClient();
//启动一个线程
new Thread(){
public void run(){
while(true){
chatClient.readInfo();
try {
Thread.currentThread().sleep(1000);
}catch (Exception e){
}
}
}
}.start();
//发送数据给服务器端
Scanner scanner = new Scanner(System.in);
while(scanner.hasNextLine()){
String str = scanner.nextLine();
chatClient.sendInfo(str);
}
}
}
四、启动服服端和客户端观察结果
1、同一个类启动多个实例
结果
💥推荐阅读💥