服务器端代码:
package com.atguigu.springcloud.controller;
import lombok.extern.slf4j.Slf4j;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.util.Iterator;
/**
* @Author Lxq
* @Date 2020/4/20 19:26
* @Version 1.0
*/
@Slf4j
public class GroupChatServer {
// 定义属性
private Selector selector;
private ServerSocketChannel listenChannel;
private static final int PORT = 6667;
/**
* 初始化
*/
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();
if (count > 0) { // 有事件要处理
// 遍历的到的SelectionKey
Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
while (iterator.hasNext()) {
SelectionKey key = iterator.next();
// 监听到的是连接事件
if (key.isAcceptable()) {
SocketChannel sc = listenChannel.accept();
sc.configureBlocking(false);
sc.register(selector, SelectionKey.OP_READ);
// 提示上线
log.info(sc.getRemoteAddress() + "上线....");
} else if (key.isReadable()) { // 通道发送read 事件,即是可读状态
// 处理读
readData(key);
}
// 删除key
iterator.remove();
}
} else {
log.info("等待........");
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
}
}
/**
* 读取客户端的消息
*
* @param key
*/
private void readData(SelectionKey key) {
// 定义一个socketchannel
SocketChannel channel = null;
try {
// 获取通道
channel = (SocketChannel) key.channel();
// 创建缓冲区
ByteBuffer buffer = ByteBuffer.allocate(1024);
int count = channel.read(buffer);
// 根据count值做处理
if (count > 0) {
// 将缓冲区的数据转成字符串
String msg = new String(buffer.array());
log.info("from 客户端:" + msg);
// 向其他客户端发送消息
sendInforOtherClients(msg,channel);
}
} catch (Exception e) {
try {
log.info(channel.getRemoteAddress() + "离线了");
// 取消注册
key.cancel();
// 关闭通道
channel.close();
} catch (IOException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}
}
/**
* 将消息转发个其他客户端
*
* @param msg
* @param self
*/
private void sendInforOtherClients(String msg, SocketChannel self) throws IOException {
log.info("服务器转发消息中.......");
// 遍历所有注册到selector上的socketchannel,并排除self
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写入通道
dest.write(buffer);
}
}
}
public static void main(String[] args) {
// 创建服务器
GroupChatServer groupChatServer = new GroupChatServer();
groupChatServer.listen();
}
}
客户端代码:
package com.atguigu.springcloud.controller;
import lombok.extern.slf4j.Slf4j;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectableChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Scanner;
/**
* @Author Lxq
* @Date 2020/4/20 22:09
* @Version 1.0
*/
@Slf4j
public class GroupChatClient {
// 定义相关的属性
private final String Host = "127.0.0.1";
private final Integer PORT = 6667;
private Selector selector;
private SocketChannel socketChannel;
private String username;
public GroupChatClient() throws IOException {
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);
log.info(username + "is ok........");
}
/**
* 向服务器发送消息
*
* @param info
*/
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 readChannel = selector.select();
if (readChannel > 0) {
Iterator<SelectionKey> iterator = selector.keys().iterator();
while (iterator.hasNext()) {
SelectionKey key = iterator.next();
if (key.isReadable()) {
// 获取通道
SocketChannel sc = (SocketChannel) key.channel();
ByteBuffer buffer = ByteBuffer.allocate(1024);
socketChannel.read(buffer);
String msg = new String(buffer.array());
log.info(msg.trim());
}
}
iterator.remove();
} else {
log.info("没有可以用的通道.......");
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
// 启动客户端
GroupChatClient chatClient = new GroupChatClient();
// 启动一个线程,每隔3s 读取一次客户端返回的数据
new Thread() {
@Override
public void run() {
while (true) {
chatClient.readInfo();
try {
Thread.currentThread().sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
/**
* 发送消息给服务器
*/
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
String s = scanner.next();
chatClient.sendInfo(s);
}
}
}