<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.client; 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; /** * @author: hcf * @qq: 46914685 * @email: 46914685@qq.com * @date: 2020/11/26 16:25 */ public class GroupChatClient { //定義相關的屬性 private final String HOST = "127.0.0.1";//服務器ip private final int PORT = 6667;//服務器端口 private Selector selector; private SocketChannel socketChannel; private String username; /** * 初始化工作 */ public GroupChatClient() throws IOException { //獲取選擇器 selector = Selector.open(); //連接服務器 socketChannel = SocketChannel.open(new InetSocketAddress(HOST, PORT)); //設置非阻塞 socketChannel.configureBlocking(false); //將socketChannel注冊到selector上 socketChannel.register(selector, SelectionKey.OP_READ); //獲取username username = socketChannel.getLocalAddress().toString().substring(1); System.out.println(username + " is ok ..."); } /** * 向服務器發送消息 * * @param info */ public void sendInfo(String info) { info = username + " 說: " + info; try { socketChannel.write(ByteBuffer.wrap(info.getBytes())); } catch (IOException e) { e.printStackTrace(); } } /** * 讀取從服務器發送過來的消息 */ public void readInfo() { try { int count = selector.select(); if (count > 0) {//有可能的通道 Iterator<SelectionKey> iterator = selector.selectedKeys().iterator(); while (iterator.hasNext()) { SelectionKey key = iterator.next(); if (key.isReadable()) {//可讀 //得到相關通道 SocketChannel channel = (SocketChannel) key.channel(); //創建buffer容器 ByteBuffer buffer = ByteBuffer.allocate(1024); //把數據讀取到buffer容器 channel.read(buffer); //把讀取到的buffer轉換為字符串 String msg = new String(buffer.array()); System.out.println(msg.trim()); } iterator.remove();//刪除當前的selectionKey,防止重復操作 } } else { // System.out.println("沒有可用的通道..."); } } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) throws IOException { //啟動客戶端 GroupChatClient client = new GroupChatClient(); //啟動一個線程,每隔三秒,讀取從服務器發送的數據 new Thread() { public void run() { while (true) { client.readInfo(); //休眠三秒 try { Thread.currentThread().sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } } } }.start(); //發送數據給服務器 Scanner scanner = new Scanner(System.in); while (scanner.hasNextLine()) { String line = scanner.nextLine(); client.sendInfo(line); } } } ~~~
                  <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>

                              哎呀哎呀视频在线观看