我正在参加「掘金·启航计划」
服务端
package com.atguigu.nio.qunliao;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.util.Set;
/**
* @Description: ···
* @author: Freedom
* @QQ: 1556507698
* @date:2022/6/23 10:20
*/
public class QServer {
//定义属性
private ServerSocketChannel listenChannel;
private Selector selector;
private static final int PORT = 6667;
//构造器 初始化操作
public QServer() {
try {
//得到选择器
selector = Selector.open();
//初始化通道
listenChannel = ServerSocketChannel.open();
//绑定端口
listenChannel.bind(new InetSocketAddress(PORT));
//设置非阻塞模式
listenChannel.configureBlocking(false);
//将通道注册到复用器上 监听新的连接事件
listenChannel.register(selector, SelectionKey.OP_ACCEPT);
} catch (IOException e) {
e.printStackTrace();
}
}
//监听事件
public void listen() {
System.out.println("监听线程: " + Thread.currentThread().getName());
try {
while (true) {
int conunt = selector.select();
if (conunt > 0) {
//遍历得到key
Set<SelectionKey> selectionKeys = selector.selectedKeys();
selectionKeys.stream().forEach(key -> {
//监听到连接事件
if (key.isAcceptable()) {
try {
SocketChannel socketChannel = listenChannel.accept();
socketChannel.configureBlocking(false);
//将该通道初测到Selector上关注Read事件
socketChannel.register(selector, SelectionKey.OP_READ);
//给出提示某个人上线
System.out.println(socketChannel.getRemoteAddress() + "上线");
} catch (IOException e) {
e.printStackTrace();
}
}
//如果通道发生可读事件 通道可读
if (key.isReadable()) {
readData(key);
}
//删除key 防止重复读取
selectionKeys.clear();
});
}else {
System.out.println("等待");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
//读取客户端消息
private void readData(SelectionKey selectionKey) {
//定义一个通到
SocketChannel channel = null;
try {
//取得关联的Channel
channel = (SocketChannel) selectionKey.channel();
//创建缓冲区域
ByteBuffer buffer = ByteBuffer.allocate(1024);
//将数据读取到buffer中
int count = channel.read(buffer);
//根据count值做处理
if (count > 0) {
//将缓冲区数据转成字符串
String msg = new String(buffer.array());
//在服务器端输出消息
System.out.println("客户端 :" + msg);
//向其他客户端(排除自己)转发消息 抽取方法
snedInfo(msg, channel);
}
} catch (IOException e) {
try {
System.out.println(channel.getRemoteAddress() + "离线");
//取消注册
selectionKey.cancel();
//关闭通道
channel.close();
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
}
//转发消息到其他通道
private void snedInfo(String msg, SocketChannel self) {
System.out.println("服务器转发消息");
//遍历所有注册到复用器上的通道 并排除自己
selector.keys().stream().forEach(key -> {
//通过key取出通道 SocketChannel
Channel targetChannel = key.channel();
//排除自己
if (targetChannel instanceof SocketChannel && targetChannel != self) {
//转换类型
SocketChannel dest = (SocketChannel) targetChannel;
//将msg存储到buffer
ByteBuffer buffer = ByteBuffer.wrap(msg.getBytes());
try {
//将buffer数据写入到通道
dest.write(buffer);
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
public static void main(String[] args) {
QServer qServer = new QServer();
qServer.listen();
}
}
客户端
package com.atguigu.nio.qunliao;
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.Scanner;
import java.util.Set;
/**
* @Description: ···
* @author: Freedom
* @QQ: 1556507698
* @date:2022/6/23 11:11
*/
public class QClient {
//定义服务器ID
private final String HOST = "127.0.0.1";
//定义服务器端口
private final int PORT = 6667;
//通道
private SocketChannel socketChannel;
//复用器
private Selector selector;
//username
private String userName;
//构造器
public QClient() 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);
System.out.println(userName + "is ok ....");
}
//向服务器发送消息
private void sendMsg(String info){
info = userName + "说:"+info;
try{
//发送消息
socketChannel.write(ByteBuffer.wrap(info.getBytes()));
}catch (IOException e){
}
}
//读取从服务器端回复的消息
public void ReadServerMsg(){
try {
int readChannels = selector.select();
//如果有可读的通道
if (readChannels > 0){
Set<SelectionKey> selectionKeys = selector.selectedKeys();
selectionKeys.stream().forEach(key -> {
if (key.isReadable()){
//得到相关通道
SocketChannel sc = (SocketChannel) key.channel();
//得到一个Buffer
ByteBuffer allocate = ByteBuffer.allocate(1024);
//读取
try {
sc.read(allocate);
//把读取到的缓冲区的数据转成字符串
String msg = new String(allocate.array());
//输出消息
System.out.println(msg.trim());
} catch (IOException e) {
e.printStackTrace();
}
}else {
System.out.println("没有可用通道");
}
});
selectionKeys.clear();
}
}catch (IOException e){
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception{
//启动客户端
QClient qClient = new QClient();
//启动一个线程 每隔三秒读取从服务器端发送数据
new Thread(() -> {
while (true){
//循环读取
qClient.ReadServerMsg();
try {
Thread.currentThread().sleep(3000);
}catch (InterruptedException e){
e.printStackTrace();
}
}
}).start();
//客户端发送数据给服务器端
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextLine()){
//循环写入
String s = scanner.nextLine();
qClient.sendMsg(s);
}
}
}