<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                ~~~ package com.youge.nio.chat.server; import java.io.IOException; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.channels.*; import java.util.Iterator; /** * @author: hcf * @qq: 46914685 * @email: 46914685@qq.com * @date: 2020/11/26 15:31 */ public class GroupChatServer { //定義屬性 private static final int PORT = 6667; private Selector selector; private ServerSocketChannel listenChannel; //構造器,初始化工作 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() { //循環處理 while (true) { try { int count = selector.select(2000); if (count > 0) {//有事件要處理 //遍歷得到selectionKey集合 Iterator<SelectionKey> iterator = selector.selectedKeys().iterator(); while (iterator.hasNext()) { //取出selectionkey SelectionKey key = iterator.next(); //監聽到accept事件 if (key.isAcceptable()) { SocketChannel socketChannel = listenChannel.accept(); //設置非阻塞 socketChannel.configureBlocking(false); //將連接過來的客戶端注冊到selector上 socketChannel.register(selector, SelectionKey.OP_READ); //提示 上線 System.out.println(socketChannel.getRemoteAddress() + "上線了."); } else if (key.isReadable()) {//讀事件 //調用專門的讀方法 readDate(key); } //將當前的selectionKey刪除,防止重復操作 iterator.remove(); } } else { System.out.println("等待中...."); } } catch (IOException e) { e.printStackTrace(); } } } /** * 讀取客戶端消息 * * @param key */ private void readDate(SelectionKey key) { //定義一個SocketChannel SocketChannel channel = null; //獲取channel channel = (SocketChannel) key.channel(); //創建byteBuffer ByteBuffer buffer = ByteBuffer.allocate(1023); //讀取數據,把數據放入btyeBuffer try { int count = channel.read(buffer); if (count > 0) {//有數據 //把緩沖區的數據轉為字符串 String message = new String(buffer.array()); //輸出消息 System.out.println("from 客戶端:" + message); //向其它客戶端口轉消息(去掉自己),專門寫一個方法來處理 sendInfoToOtherClients(message,channel); } } catch (IOException e) { try { System.out.println(channel.getRemoteAddress()+"離線了"); //取消注冊 key.cancel(); //關閉通道 channel.close(); } catch (IOException ioException) { ioException.printStackTrace(); } } } /** * 轉發消息給其它客戶端 * * @param msg * @param self */ public void sendInfoToOtherClients(String msg, SocketChannel self) { System.out.println("服務器轉發消息中..."); //遍歷所有注冊到selector上的socketChannel,并排除 self for (SelectionKey key : selector.keys()) { //通過key取出對應的socketchannel // SocketChannel targetChannel = (SocketChannel) key.channel(); Channel targetChannel = key.channel(); //排除自己 if (targetChannel instanceof SocketChannel && targetChannel != self) { SocketChannel dest = (SocketChannel) targetChannel; //將msg存儲到byteBuffer ByteBuffer buffer = ByteBuffer.wrap(msg.getBytes()); try { dest.write(buffer); } catch (IOException e) { e.printStackTrace(); } } } } public static void main(String[] args) { //創建服務器對象 GroupChatServer server = new GroupChatServer(); //啟動監聽 server.listen(); } } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看