<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>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                ~~~ package com.youge.nio.server; 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.ServerSocketChannel; import java.nio.channels.SocketChannel; import java.util.Iterator; import java.util.Set; /** * @author: hcf * @qq: 46914685 * @email: 46914685@qq.com * @date: 2020/11/26 10:46 */ public class NIOServer { public static void main(String[] args) throws IOException { //創建ServerSocketChannel->ServerSocket ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(); //獲取一個Selector對象 Selector selector = Selector.open(); //綁定一個端口6666,在服務器端上監聽 serverSocketChannel.socket().bind(new InetSocketAddress(6666)); //設置非阻塞 serverSocketChannel.configureBlocking(false); //把serverSocketChannel注冊到selector,關心事件為 OP_ACCEPT serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT); //循環等待客戶端連接 while (true) { //等待一秒鐘,如果沒有事件發生就返回 if (selector.select(1000) == 0) {//沒有事件發生 System.out.println("服務器等待了1秒中,無連接"); continue; } //如果返回的>0,就獲取到相關的selectionKey集合 //1.如果返回>0,表示已經獲取到關注的事件 //2.selector.selectedKeys返回關注事件的集合 //通過 selectedKeys反向獲取通道 Set<SelectionKey> selectionKeys = selector.selectedKeys(); //遍歷Set<SeletctionKey>,使用迭代器遍歷 Iterator<SelectionKey> keyIterator = selectionKeys.iterator(); while (keyIterator.hasNext()) { //獲取到selectionKey SelectionKey selectionKey = keyIterator.next(); //根據key中對應的通道發生事件做相應處理 if (selectionKey.isAcceptable()) {//如果是OP_ACCEPT,有新的客戶端連接 //該客戶端生成一個SocketChannel SocketChannel socketChannel = serverSocketChannel.accept(); System.out.println("客戶端連接成功,生成了一個socketChannel "+socketChannel.hashCode()); //將socketChannel設置為非阻塞 socketChannel.configureBlocking(false); //將socketChannel注冊到selector,關注事件為OP_READ,同時給socketChannel關聯一個byteBuffer socketChannel.register(selector, SelectionKey.OP_READ, ByteBuffer.allocate(1024)); } if (selectionKey.isReadable()) {//發生OP_READ事件 //通過selectionKey反向獲取socketChannel SocketChannel channel = (SocketChannel) selectionKey.channel(); //獲取到該channel關聯的buffer ByteBuffer buffer = (ByteBuffer) selectionKey.attachment(); // channel.read(buffer); System.out.println("from 客戶端:"+new String(buffer.array())); } //手動從集合中刪除當前selectionKey,防止重復操作,因為是多線程的 keyIterator.remove(); } } } } ~~~
                  <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>

                              哎呀哎呀视频在线观看