日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人工智能 > ChatGpt >内容正文

ChatGpt

基于AIO的CS聊天室

發布時間:2025/3/21 ChatGpt 51 豆豆
生活随笔 收集整理的這篇文章主要介紹了 基于AIO的CS聊天室 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

所謂AIO,即是異步IO,它的IO操作交由操作系統完成。設置監聽器(類似于一個信號處理函數),當系統IO操作完成時,會被監聽器監聽到,并執行相應的后續操作,然后返回。

監聽器一般使用CompletionHandler

服務器端代碼:?

package com.nanhao.AIOTest;import java.io.IOException; import java.net.InetSocketAddress; import java.nio.*; import java.nio.channels.AsynchronousChannelGroup; import java.nio.channels.AsynchronousServerSocketChannel; import java.nio.channels.AsynchronousSocketChannel; import java.nio.channels.CompletionHandler; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors;public class Server {static final int PORT = 3000;final static String UTF_8 = "utf-8";static List<AsynchronousSocketChannel>channelList = new ArrayList<>();public void startListen()throws IOException,Exception{//創建一個線程池ExecutorService executorService = Executors.newFixedThreadPool(20);//以指定的線程池來創建一個AsynchronousChannelGroupAsynchronousChannelGroup channelGroup = AsynchronousChannelGroup.withThreadPool(executorService);//通過channelGroup來創建一個AsynchronousServerSocketChannelAsynchronousServerSocketChannel serverSocketChannel = AsynchronousServerSocketChannel.open(channelGroup)//指定監聽本機的端口.bind(new InetSocketAddress("127.0.0.1",PORT));//使用監聽器來接收來自客戶端的鏈接請求serverSocketChannel.accept(null,new AcceptHandler(serverSocketChannel));}public static void main(String[]args)throws Exception{Server server = new Server();server.startListen();}class AcceptHandler implements CompletionHandler<AsynchronousSocketChannel,Object>{private AsynchronousServerSocketChannel serverSocketChannel;public AcceptHandler(AsynchronousServerSocketChannel serverSocketChannel) {this.serverSocketChannel = serverSocketChannel;}//定義一個ByteBuffer準備讀取數據ByteBuffer byteBuffer = ByteBuffer.allocate(1024);@Overridepublic void completed(final AsynchronousSocketChannel result, Object attachment) {Server.channelList.add(result);serverSocketChannel.accept(null,this);result.read(byteBuffer, null, new CompletionHandler<Integer, Object>() {@Overridepublic void completed(Integer result, Object attachment) {byteBuffer.flip();//將buffer中的字節轉換為字符String context = StandardCharsets.UTF_8.decode(byteBuffer).toString();//由于是聊天室,所以將所有的channel里面寫入這個消息for(AsynchronousSocketChannel ass:Server.channelList){try{ass.write(ByteBuffer.wrap(context.getBytes(Server.UTF_8))).get();}catch(Exception e){e.printStackTrace();}}byteBuffer.clear();result.read(byteBuffer,null,this);}@Overridepublic void failed(Throwable exc, Object attachment) {System.out.println("讀取數據失敗: "+exc);Server.channelList.remove(result);}});}@Overridepublic void failed(Throwable exc, Object attachment) {System.out.println("連接失敗 :"+exc);}}}

?

總結

以上是生活随笔為你收集整理的基于AIO的CS聊天室的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。