使用NIO实现聊天室

在这里插入图片描述

服务端

package example;

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 = 80;

    /**
     * 构造器
     * 初始化任务
     */
    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();
                //System.out.println("**系统通知**:当前线程被唤醒!");
                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");
                            //
                            sc.write(ByteBuffer.wrap(("您好," + sc.getRemoteAddress() + ",欢迎来到聊天室~~~").getBytes()));
                        }
                        // 通道可读
                        if (key.isReadable()){
                            // TODO处理读
                            readData(key);
                        }
                        // 当前的key删除, 防止重复处理
                        iterator.remove();
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     *  读取客户端消息
     * @param key
     */
    private void readData(SelectionKey key){
        // 定义一个SocketChannel
        SocketChannel channel = null;
        String msg = null;
        try {
            // 得到channel
            channel = (SocketChannel) key.channel();
            // 创建缓冲buffer
            ByteBuffer buffer = ByteBuffer.allocate(1024);
            int count = channel.read(buffer);
            // 根据count的值做处理
            if (count > 0){
                // 把缓冲区数据转为字符串并输出
                msg = new String(buffer.array());
                // 输出该消息
                System.out.println("from Client: " + msg);
            }
        } catch (IOException e){
            try {
                msg = "**系统通知:**" + channel.getRemoteAddress() + " is offline";
                // 输出该消息
                System.out.println(msg);
                // 取消注册
                key.cancel();
                // 关闭通道
                channel.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        } finally {
            try {
                sendInfoToAllClients(msg);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     *  广播消息给所有客户端(channel)
     * @param msg
     */
    private void sendInfoToAllClients(String msg) throws IOException {
        // 遍历, 所有注册到selector上的SocketChannel
        for (SelectionKey key : selector.keys()){
            // 通过key取出对应的SocketChannel
            Channel targetChannel = key.channel();
            if (targetChannel instanceof SocketChannel && targetChannel.isOpen()){
                // 转型
                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 example;

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 int PORT = 80;
    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(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();
            //System.out.println("**系统通知**:当前线程被唤醒!");
            // 有可用的通道
            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())));
                        //String msg = getString(buffer);
                        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();
                }
            }
        }.start();
        // 发送数据给服务器端
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){
            String s = scanner.nextLine();
            chaClient.sendInfo(s);
        }
    }

}

参考资料

[01] NIO SelectionKey.cancel只对下一次select()生效?
[02] Java NIO开发需要注意的陷阱
[03] NIO网络编程实战之简单多人聊天室
[04] NIO 聊天室代码实现
[05] Java NIO示例:多人网络聊天室
[06] Java NIO实战之聊天室

微信扫一扫关注公众号
image.png
点击链接加入群聊

https://jq.qq.com/?_wv=1027&k=5eVEhfN
软件测试学习交流QQ群号:511619105

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值