亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java基于NIO實(shí)現(xiàn)群聊功能

 更新時間:2021年11月23日 17:15:19   作者:大樹下躲雨  
這篇文章主要為大家詳細(xì)介紹了Java基于NIO實(shí)現(xiàn)群聊功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Java基于NIO實(shí)現(xiàn)群聊功能的具體代碼,供大家參考,具體內(nèi)容如下

一、群聊服務(wù)器

package com.dashu.netty.group_chat;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.nio.charset.StandardCharsets;
import java.util.Iterator;


public class GroupChatServer {


    /**
     * 初始化選擇器
     */
    private Selector selector;

    /**
     * 初始化服務(wù)器網(wǎng)絡(luò)通道
     */
    private ServerSocketChannel serverSocketChannel;

    /**
     * 端口
     */
    private static final int PORT = 6666;


    /**
     * 構(gòu)造方法
     */
    public GroupChatServer() {

        try {

            //獲取選擇器
            selector = Selector.open();

            //獲取服務(wù)器網(wǎng)絡(luò)通道
            serverSocketChannel = ServerSocketChannel.open();

            //網(wǎng)絡(luò)地址
            InetSocketAddress inetSocketAddress = new InetSocketAddress(PORT);

            //服務(wù)器網(wǎng)絡(luò)通道綁定網(wǎng)絡(luò)地址
            serverSocketChannel.socket().bind(inetSocketAddress);

            //設(shè)置服務(wù)器網(wǎng)絡(luò)通道非阻塞
            serverSocketChannel.configureBlocking(false);

            //將服務(wù)器網(wǎng)絡(luò)通道注冊到選擇器上,綁定連接請求事件
            serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);

        } catch (Exception e) {

            e.printStackTrace();

        }

    }


    /**
     * 監(jiān)聽客戶端請求事件
     */
    public void listen() {


        try {

            //無限循環(huán)
            while (true) {

                //獲取請求數(shù)
                int count = selector.select();

                //count大于0,則代表有請求進(jìn)來
                if (count > 0) {


                    //獲取請求集
                    Iterator<SelectionKey> selectionKeyIterator = selector.selectedKeys().iterator();

                    //遍歷請求集
                    while (selectionKeyIterator.hasNext()) {

                        //得到請求
                        SelectionKey selectionKey = selectionKeyIterator.next();

                        //連接請求
                        if (selectionKey.isAcceptable()) {

                            //獲取客戶端網(wǎng)絡(luò)通道
                            SocketChannel socketChannel = serverSocketChannel.accept();

                            //設(shè)置客戶端網(wǎng)絡(luò)通道非阻塞
                            socketChannel.configureBlocking(false);

                            //將客戶端網(wǎng)絡(luò)通道注冊到選擇器上
                            socketChannel.register(selector, SelectionKey.OP_READ);

                            System.out.println(socketChannel.getRemoteAddress() + "上線了");

                        }

                        //信息讀取請求
                        if (selectionKey.isReadable()) {

                            //客戶端信息讀取
                            readData(selectionKey);

                        }

                        //移除請求
                        selectionKeyIterator.remove();

                    }


                } else {

                    System.out.println("等待...");

                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    /**
     * 客戶端信息讀取
     *
     * @param selectionKey
     */
    private void readData(SelectionKey selectionKey) {

        //初始化客戶端網(wǎng)絡(luò)通道
        SocketChannel socketChannel = null;

        try {

            //獲取客戶端網(wǎng)絡(luò)通道
            socketChannel = (SocketChannel) selectionKey.channel();

            //創(chuàng)建緩沖區(qū)
            ByteBuffer byteBuffer = ByteBuffer.allocate(1024);

            //讀取客戶端網(wǎng)絡(luò)通道中的數(shù)據(jù)到緩沖區(qū)
            int count = socketChannel.read(byteBuffer);

            //判斷緩沖區(qū)中是否有數(shù)據(jù)
            if (count > 0) {

                //將緩沖區(qū)的數(shù)據(jù)轉(zhuǎn)換位字符串
                String message = new String(byteBuffer.array());

                System.out.println(message.trim());

                //將信息群發(fā)到其他客戶端
                sendInfoToOtClients(message, socketChannel);

            }

        } catch (Exception e) {

            e.printStackTrace();

        }


    }

    /**
     * 將信息群發(fā)到其他客戶端
     *
     * @param message
     * @param socketChannel
     */
    private void sendInfoToOtClients(String message, SocketChannel socketChannel) {

        //獲取所有注冊到選擇器的客戶端,并遍歷
        for (SelectionKey selectionKey : selector.keys()) {

            //獲取通道
            Channel channel = selectionKey.channel();

            //判斷通道是否屬于SocketChannel,同時不等于發(fā)送信息的客戶端
            if (channel instanceof SocketChannel && channel != socketChannel) {

                //通道轉(zhuǎn)換
                SocketChannel sc = (SocketChannel) channel;

                //將信息寫入緩沖區(qū)
                ByteBuffer byteBuffer = ByteBuffer.wrap(message.getBytes(StandardCharsets.UTF_8));

                try {

                    //將緩沖區(qū)的數(shù)據(jù)寫入通道
                    sc.write(byteBuffer);

                } catch (Exception e) {

                    e.printStackTrace();

                }

            }

        }

    }




    public static void main(String[] args) {

        GroupChatServer groupChatServer = new GroupChatServer();

        System.out.println("服務(wù)器啟動,開始監(jiān)聽客戶端請求...");
        groupChatServer.listen();

    }


}

二、客戶端

package com.dashu.netty.group_chat;

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.nio.charset.StandardCharsets;
import java.util.Iterator;
import java.util.Scanner;

public class GroupChatClient {


    /**
     * 網(wǎng)絡(luò)連接地址
     */
    private final String HOST = "127.0.0.1";

    /**
     * 端口
     */
    private final int PORT = 6666;

    /**
     * 初始化選擇器
     */
    private Selector selector;

    /**
     * 初始化網(wǎng)絡(luò)通道
     */
    private SocketChannel socketChannel;


    /**
     * 用戶名
     */
    private String username;


    public GroupChatClient() {
        try {

            //獲取選擇器
            selector = Selector.open();


            //獲取服務(wù)器網(wǎng)絡(luò)地址
            InetSocketAddress inetSocketAddress = new InetSocketAddress(HOST, PORT);

            //獲取網(wǎng)絡(luò)通道
            socketChannel = SocketChannel.open(inetSocketAddress);

            //設(shè)置網(wǎng)絡(luò)通道非阻塞
            socketChannel.configureBlocking(false);


            //將網(wǎng)絡(luò)通道注冊到選擇器
            socketChannel.register(selector, SelectionKey.OP_READ);


            //獲取用戶名
            System.out.println("請輸入用戶名:");

            Scanner scanner = new Scanner(System.in);

            username = scanner.nextLine();

            System.out.println(username + " 進(jìn)入群聊...");

        } catch (Exception e) {

            e.printStackTrace();

        }
    }


    /**
     * 向服務(wù)器發(fā)送信息
     *
     * @param message
     */
    public void sendInfo(String message) {

        message = username + ":" + message;

        try {

            //向通道寫入數(shù)據(jù)
            socketChannel.write(ByteBuffer.wrap(message.getBytes(StandardCharsets.UTF_8)));

        } catch (Exception e) {

            e.printStackTrace();

        }

    }


    /**
     * 讀取服務(wù)器發(fā)來的信息
     */
    public void readInfo() {
        try {

            //獲取請求數(shù)
            int count = selector.select();

            if (count > 0) {

                //獲取請求集
                Iterator<SelectionKey> selectionKeyIterator = selector.selectedKeys().iterator();

                //遍歷請求集
                while (selectionKeyIterator.hasNext()) {

                    //獲取請求
                    SelectionKey selectionKey = selectionKeyIterator.next();

                    //判斷位讀請求
                    if (selectionKey.isReadable()) {

                        //獲取通道
                        SocketChannel sc = (SocketChannel) selectionKey.channel();

                        //創(chuàng)建緩沖區(qū)
                        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);


                        //讀取通道的數(shù)據(jù)到緩沖區(qū)
                        sc.read(byteBuffer);

                        //緩沖區(qū)數(shù)據(jù)轉(zhuǎn)字符串
                        String message = new String(byteBuffer.array());

                        //輸出
                        System.out.println(message.trim());

                    }

                    //移除已完成請求
                    selectionKeyIterator.remove();

                }

            }

        } catch (Exception e) {

            e.printStackTrace();

        }


    }


    public static void main(String[] args) {

        GroupChatClient groupChatClient = new GroupChatClient();

        /**
         * 開啟一個線程,每3秒讀取一次服務(wù)器發(fā)來的信息
         */
        new Thread() {
            @Override
            public void run() {
                while (true) {
                    groupChatClient.readInfo();
                    try {
                        Thread.sleep(3000);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();


        //信息輸入
        Scanner scanner = new Scanner(System.in);

        System.out.println("請輸入信息:");
        while (scanner.hasNextLine()) {

            String s = scanner.nextLine();

            //信息發(fā)送
            groupChatClient.sendInfo(s);

            System.out.println("請輸入信息:");

        }
    }


}

三、效果圖

1、服務(wù)器

2、客戶端01

3、客戶端02

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

相關(guān)文章

  • Java設(shè)計模式之代理模式解析

    Java設(shè)計模式之代理模式解析

    這篇文章主要介紹了Java設(shè)計模式之代理模式解析,代理模式為一個對象提供一個替身,以控制對這個對象的訪問,即通過代理對象訪問目標(biāo)對象,需要的朋友可以參考下
    2023-12-12
  • SpringMVC實(shí)現(xiàn)Validation校驗(yàn)過程詳解

    SpringMVC實(shí)現(xiàn)Validation校驗(yàn)過程詳解

    這篇文章主要介紹了SpringMVC實(shí)現(xiàn)Validation校驗(yàn)過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-11-11
  • Springboot集成kafka高級應(yīng)用實(shí)戰(zhàn)分享

    Springboot集成kafka高級應(yīng)用實(shí)戰(zhàn)分享

    這篇文章主要介紹了Springboot集成kafka高級應(yīng)用實(shí)戰(zhàn)分享,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-08-08
  • Java身份證號碼校驗(yàn)工具類詳解

    Java身份證號碼校驗(yàn)工具類詳解

    這篇文章主要為大家詳細(xì)介紹了Java身份證號碼校驗(yàn)工具類,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-10-10
  • java取兩個字符串的最大交集

    java取兩個字符串的最大交集

    這篇文章主要介紹了java取兩個字符串的最大交集的方法,涉及Java對字符串操作的技巧,具有一定的參考借鑒價值,需要的朋友可以參考下
    2014-10-10
  • SpringMVC視圖作用詳解

    SpringMVC視圖作用詳解

    這篇文章主要介紹了springMVC中的視圖與視圖解析器,springMVC視圖的種類很多,默認(rèn)有轉(zhuǎn)發(fā)視圖和重定向視圖,本文就每一種視圖給大家詳細(xì)介紹,需要的朋友可以參考下
    2022-11-11
  • JAVA線程sleep()和wait()詳解及實(shí)例

    JAVA線程sleep()和wait()詳解及實(shí)例

    這篇文章主要介紹了JAVA線程sleep()和wait()詳解及實(shí)例的相關(guān)資料,探討一下sleep()和wait()方法的區(qū)別和實(shí)現(xiàn)機(jī)制,需要的朋友可以參考下
    2017-05-05
  • Spring?Boot面試必問之啟動流程知識點(diǎn)詳解

    Spring?Boot面試必問之啟動流程知識點(diǎn)詳解

    SpringBoot是Spring開源組織下的子項(xiàng)目,是Spring組件一站式解決方案,主要是簡化了使用Spring的難度,簡省了繁重的配置,提供了各種啟動器,開發(fā)者能快速上手,這篇文章主要給大家介紹了關(guān)于Spring?Boot面試必問之啟動流程知識點(diǎn)的相關(guān)資料,需要的朋友可以參考下
    2022-06-06
  • 最新評論