欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java?NIO實戰(zhàn)之多人聊天室

 更新時間:2021年11月24日 11:29:10   作者:紅字V  
這篇文章主要為大家詳細介紹了Java?NIO實戰(zhàn)之多人聊天室,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Java NIO實戰(zhàn)之多人聊天室的具體代碼,供大家參考,具體內(nèi)容如下

NIO服務端

public class NioServer {

    /**
     * 啟動
     */
    public void start() throws IOException {
        /**
         * 1. 創(chuàng)建Selector
         */
        Selector selector = Selector.open();
        /**
         * 2. 通過ServerSocketChannel創(chuàng)建channel通道
         */
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        /**
         * 3. 為channel通道綁定監(jiān)聽端口
         */
        serverSocketChannel.bind(new InetSocketAddress(8000));
        /**
         * 4. **設(shè)置channel為非阻塞模式**
         */
        serverSocketChannel.configureBlocking(false);
        /**
         * 5. 將channel注冊到selector上,監(jiān)聽連接事件
         */
        serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
        System.out.println("服務器啟動成功!");

        /**
         * 6. 循環(huán)等待新接入的連接
         */
        for (;;) { // while(true) c for;;
            /**
             * TODO 獲取可用channel數(shù)量
             */
            int readyChannels = selector.select();

            /**
             * TODO 為什么要這樣?。??
             */
            if (readyChannels == 0) continue;

            /**
             * 獲取可用channel的集合
             */
            Set<SelectionKey> selectionKeys = selector.selectedKeys();

            Iterator iterator = selectionKeys.iterator();

            while (iterator.hasNext()) {
                /**
                 * selectionKey實例
                 */
                SelectionKey selectionKey = (SelectionKey) iterator.next();

                /**
                 * **移除Set中的當前selectionKey**
                 */
                iterator.remove();

                /**
                 * 7. 根據(jù)就緒狀態(tài),調(diào)用對應方法處理業(yè)務邏輯
                 */
                /**
                 * 如果是 接入事件
                 */
                if (selectionKey.isAcceptable()) {
                    acceptHandler(serverSocketChannel, selector);
                }

                /**
                 * 如果是 可讀事件
                 */
                if (selectionKey.isReadable()) {
                    readHandler(selectionKey, selector);
                }
            }
        }
    }

    /**
     * 接入事件處理器
     */
    private void acceptHandler(ServerSocketChannel serverSocketChannel,
                               Selector selector)
            throws IOException {
        /**
         * 如果要是接入事件,創(chuàng)建socketChannel
         */
        SocketChannel socketChannel = serverSocketChannel.accept();

        /**
         * 將socketChannel設(shè)置為非阻塞工作模式
         */
        socketChannel.configureBlocking(false);

        /**
         * 將channel注冊到selector上,監(jiān)聽 可讀事件
         */
        socketChannel.register(selector, SelectionKey.OP_READ);

        /**
         * 回復客戶端提示信息
         */
        socketChannel.write(Charset.forName("UTF-8")
                .encode("你與聊天室里其他人都不是朋友關(guān)系,請注意隱私安全"));
    }

    /**
     * 可讀事件處理器
     */
    private void readHandler(SelectionKey selectionKey, Selector selector)
            throws IOException {
        /**
         * 要從 selectionKey 中獲取到已經(jīng)就緒的channel
         */
        SocketChannel socketChannel = (SocketChannel) selectionKey.channel();

        /**
         * 創(chuàng)建buffer
         */
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);

        /**
         * 循環(huán)讀取客戶端請求信息
         */
        String request = "";
        while (socketChannel.read(byteBuffer) > 0) {
            /**
             * 切換buffer為讀模式
             */
            byteBuffer.flip();

            /**
             * 讀取buffer中的內(nèi)容
             */
            request += Charset.forName("UTF-8").decode(byteBuffer);
        }

        /**
         * 將channel再次注冊到selector上,監(jiān)聽他的可讀事件
         */
        socketChannel.register(selector, SelectionKey.OP_READ);

        /**
         * 將客戶端發(fā)送的請求信息 廣播給其他客戶端
         */
        if (request.length() > 0) {
            // 廣播給其他客戶端
            broadCast(selector, socketChannel, request);
        }
    }

    /**
     * 廣播給其他客戶端
     */
    private void broadCast(Selector selector,
                           SocketChannel sourceChannel, String request) {
        /**
         * 獲取到所有已接入的客戶端channel
         */
        Set<SelectionKey> selectionKeySet = selector.keys();

        /**
         * 循環(huán)向所有channel廣播信息
         */
        selectionKeySet.forEach(selectionKey -> {
            Channel targetChannel = selectionKey.channel();

            // 剔除發(fā)消息的客戶端
            if (targetChannel instanceof SocketChannel
                    && targetChannel != sourceChannel) {
                try {
                    // 將信息發(fā)送到targetChannel客戶端
                    ((SocketChannel) targetChannel).write(
                            Charset.forName("UTF-8").encode(request));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * 主方法
     * @param args
     */
    public static void main(String[] args) throws IOException {
        new NioServer().start();
    }

}

NIO客戶端

public class NioClient {

    /**
     * 啟動
     */
    public void start(String nickname) throws IOException {
        /**
         * 連接服務器端
         */
        SocketChannel socketChannel = SocketChannel.open(
                new InetSocketAddress("127.0.0.1", 8000));

        /**
         * 接收服務器端響應
         */
        // 新開線程,專門負責來接收服務器端的響應數(shù)據(jù)
        // selector , socketChannel , 注冊
        Selector selector = Selector.open();
        socketChannel.configureBlocking(false);
        socketChannel.register(selector, SelectionKey.OP_READ);
        new Thread(new NioClientHandler(selector)).start();

        /**
         * 向服務器端發(fā)送數(shù)據(jù)
         */
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextLine()) {
            String request = scanner.nextLine();
            if (request != null && request.length() > 0) {
                socketChannel.write(
                        Charset.forName("UTF-8")
                                .encode(nickname + " : " + request));
            }
        }

    }
    public static void main(String[] args) throws IOException {
//        new NioClient().start();
    }
}

客戶端線程,處理服務器端響應的的消息

public class NioClientHandler implements Runnable {
    private Selector selector;

    public NioClientHandler(Selector selector) {
        this.selector = selector;
    }

    @Override
    public void run() {

        try {
            for (;;) {
                int readyChannels = selector.select();

                if (readyChannels == 0) continue;

                /**
                 * 獲取可用channel的集合
                 */
                Set<SelectionKey> selectionKeys = selector.selectedKeys();

                Iterator iterator = selectionKeys.iterator();

                while (iterator.hasNext()) {
                    /**
                     * selectionKey實例
                     */
                    SelectionKey selectionKey = (SelectionKey) iterator.next();

                    /**
                     * **移除Set中的當前selectionKey**
                     */
                    iterator.remove();

                    /**
                     * 7. 根據(jù)就緒狀態(tài),調(diào)用對應方法處理業(yè)務邏輯
                     */

                    /**
                     * 如果是 可讀事件
                     */
                    if (selectionKey.isReadable()) {
                        readHandler(selectionKey, selector);
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 可讀事件處理器
     */
    private void readHandler(SelectionKey selectionKey, Selector selector)
            throws IOException {
        /**
         * 要從 selectionKey 中獲取到已經(jīng)就緒的channel
         */
        SocketChannel socketChannel = (SocketChannel) selectionKey.channel();

        /**
         * 創(chuàng)建buffer
         */
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);

        /**
         * 循環(huán)讀取服務器端響應信息
         */
        String response = "";
        while (socketChannel.read(byteBuffer) > 0) {
            /**
             * 切換buffer為讀模式
             */
            byteBuffer.flip();

            /**
             * 讀取buffer中的內(nèi)容
             */
            response += Charset.forName("UTF-8").decode(byteBuffer);
        }

        /**
         * 將channel再次注冊到selector上,監(jiān)聽他的可讀事件
         */
        socketChannel.register(selector, SelectionKey.OP_READ);

        /**
         * 將服務器端響應信息打印到本地
         */
        if (response.length() > 0) {
            System.out.println(response);
        }
    }
}

我們定義三個客戶端,模擬三個用戶在聊天室發(fā)送消息

public class AClient {

    public static void main(String[] args)
            throws IOException {
        new NioClient().start("AClient");
    }
}

public class BClient {

    public static void main(String[] args)
            throws IOException {
        new NioClient().start("BClient");
    }
}

public class CClient {

    public static void main(String[] args)
            throws IOException {
        new NioClient().start("CClient");
    }

}

NIO 聊天室到此結(jié)束

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 面向切面的Spring通過切點來選擇連接點實例詳解

    面向切面的Spring通過切點來選擇連接點實例詳解

    這篇文章主要為大家介紹了面向切面的Spring通過切點來選擇連接點實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-10-10
  • SpringBoot+WebSocket+Netty實現(xiàn)消息推送的示例代碼

    SpringBoot+WebSocket+Netty實現(xiàn)消息推送的示例代碼

    這篇文章主要介紹了SpringBoot+WebSocket+Netty實現(xiàn)消息推送的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-04-04
  • Java實現(xiàn)讀取Jar文件屬性的方法詳解

    Java實現(xiàn)讀取Jar文件屬性的方法詳解

    這篇文章主要為大家詳細介紹了如何利用Java語言實現(xiàn)讀取Jar文件屬性的功能,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
    2022-08-08
  • 使用IDEA反編譯沒有擦除泛型的原因解析

    使用IDEA反編譯沒有擦除泛型的原因解析

    Java泛型引入至今已有近20年,“偽泛型”已被認為是所有開發(fā)者的共識,沒有必要再在反編譯后體現(xiàn)出來反倒大大降低了可讀性,這篇文章主要介紹了使用IDEA反編譯沒有擦除泛型的原因解析,需要的朋友可以參考下
    2023-05-05
  • Java中的ReentrantLock使用解析

    Java中的ReentrantLock使用解析

    這篇文章主要介紹了Java中的ReentrantLock使用解析,ReentrandLock即可重入鎖,可重入鎖解決的是重入鎖定的問題,重入鎖定指的是當一個線程執(zhí)行邏輯時,需要兩次獲取鎖,而該鎖不可重入就會導致內(nèi)部嵌套無法獲取鎖導致Reentrance Lockout發(fā)生,需要的朋友可以參考下
    2023-11-11
  • 用Java實現(xiàn)24點游戲

    用Java實現(xiàn)24點游戲

    喜歡玩游戲的有福啦,文中有非常詳細的開發(fā)框架,按著框架來實現(xiàn)就好啦.而且24點游戲是經(jīng)典的紙牌益智游戲.,需要的朋友可以參考下
    2021-05-05
  • Springcloud-nacos實現(xiàn)配置和注冊中心的方法

    Springcloud-nacos實現(xiàn)配置和注冊中心的方法

    這篇文章主要介紹了Springcloud-nacos實現(xiàn)配置和注冊中心的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-07-07
  • spring的pointcut正則表達式的實現(xiàn)

    spring的pointcut正則表達式的實現(xiàn)

    本文主要介紹了spring的pointcut正則表達式的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-08-08
  • java中對List分段操作的實例

    java中對List分段操作的實例

    這篇文章主要介紹了java中對List分段操作的實例的相關(guān)資料,希望通過本文大家能夠掌握list的分段實現(xiàn)方法,需要的朋友可以參考下
    2017-09-09
  • SpringBoot發(fā)送郵件功能 驗證碼5分鐘過期

    SpringBoot發(fā)送郵件功能 驗證碼5分鐘過期

    這篇文章主要為大家詳細介紹了SpringBoot發(fā)送郵件功能,驗證碼5分鐘過期,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-03-03

最新評論