javaNIO-实现群聊功能
服务端
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);
//将该ServerSocketChannel注册到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 key = iterator.next();
//监听到accept
if (key.isAcceptable()){
SocketChannel sc = listenChannel.accept();
sc.configureBlocking(false);
//将该sc注册到seletor上
sc.register(selector,SelectionKey.OP_READ);
System.out.println(sc.getRemoteAddress()+" 上线");
}
//监听read(通道可读)
if (key.isReadable()){
//处理读
readData(key);
}
//当前的key删除,防止重复处理
iterator.remove();
}
}else {
System.out.println("等待中。。。");
}
}
}catch (Exception e){
e.printStackTrace();
}finally {
}
}
//读取客户端消息
private void readData(SelectionKey key){
//定义一个socketChannel
SocketChannel channel = null;
try {
//取到关联的channel
channel = (SocketChannel) key.channel();
//创建buffer
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
int count = channel.read(byteBuffer);
if (count >0){//是否读到数据
String msg = new String(byteBuffer.array());
System.out.println("客户端消息:"+msg);
//向其他客户端转发消息
sendInfoToOtherClients(msg,channel);
}
}catch (IOException e){
try {
System.out.println(channel.getRemoteAddress()+" 离线");
//取消注册
key.cancel();
//关闭通道
channel.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
//转发消息给其他客户(通道),排除自己
private void sendInfoToOtherClients(String msg,SocketChannel self) throws IOException {
System.out.println("服务器转发消息");
//遍历所有注册到selector上的Channel
for (SelectionKey key : selector.keys()) {
//通过key取出对应的SocketChannel
Channel channel = key.channel();
//排除自己
if (channel instanceof SocketChannel && channel != self){
SocketChannel dest = (SocketChannel) channel;
//将msg存储到buffer
ByteBuffer buffer = ByteBuffer.wrap(msg.getBytes());
//将buffer的数据写入通道
dest.write(buffer);
}
}
}
}
启动服务端
GroupChatServer server = new GroupChatServer();
server.listen();
客户端
public class GroupChatClient {
//定义属性
private final String HOST = "127.0.0.1";
private final int PORT = 6667;
private Selector selector;
private SocketChannel socketChannel;
private String username;
//初始化
@SneakyThrows
public GroupChatClient(){
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 + " ok");
}
//向服务器发送消息
public void sendInfo(String msg){
msg = username+":"+msg;
try {
//发送
socketChannel.write(ByteBuffer.wrap(msg.getBytes()));
}catch (IOException e){
e.printStackTrace();
}
}
//读取从服务器回复的消息
public void readInfo(){
try {
int read = selector.select();
if (read > 0){//有可用的通道
Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
while (iterator.hasNext()){
SelectionKey key = iterator.next();
if (key.isReadable()){
//得到相关通道
SocketChannel sc = (SocketChannel) key.channel();
//得到一个buffer
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
sc.read(byteBuffer);
System.out.println(new String(byteBuffer.array()));
}
iterator.remove();//删除当前SelectionKey防止重复
}
}
}catch (Exception e){
e.printStackTrace();
}
}
}
启动客户端
GroupChatClient client = new GroupChatClient();
//启动一个线程,每三秒从服务器发送的数据
new Thread(){
@Override
public void run() {
while (true){
client.readInfo();
try {
Thread.currentThread().sleep(3000);
}catch (InterruptedException e){
e.printStackTrace();
}
}
}
}.start();
//发送数据
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextLine()){
String s = scanner.nextLine();
client.sendInfo(s);
}