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

歡迎訪問 生活随笔!

生活随笔

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

ChatGpt

AIO 初体验

發布時間:2024/4/13 ChatGpt 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 AIO 初体验 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

服務端代碼:

package com.gupaoedu.vip.netty.io.aio; import java.io.IOException; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.channels.AsynchronousChannelGroup; import java.nio.channels.AsynchronousServerSocketChannel; import java.nio.channels.AsynchronousSocketChannel; import java.nio.channels.CompletionHandler; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; /** * AIO 服務端 */ public class AIOServer {private final int port;public static void main(String args[]) {int port = 8000;new AIOServer(port);}public AIOServer(int port) {this.port = port;listen();}private void listen() {try {ExecutorService executorService = Executors.newCachedThreadPool();AsynchronousChannelGroup threadGroup = AsynchronousChannelGroup.withCachedThreadPool(executorService, 1);final AsynchronousServerSocketChannel server = AsynchronousServerSocketChannel.open(threadGroup);server.bind(new InetSocketAddress(port));System.out.println("服務已啟動,監聽端口" + port);server.accept(null, new CompletionHandler<AsynchronousSocketChannel, Object>(){final ByteBuffer buffer = ByteBuffer.allocateDirect(1024);public void completed(AsynchronousSocketChannel result, Object attachment){System.out.println("IO 操作成功,開始獲取數據");try {buffer.clear();result.read(buffer).get();buffer.flip();result.write(buffer);buffer.flip();} catch (Exception e) {System.out.println(e.toString());} finally {try {result.close();server.accept(null, this);} catch (Exception e) {System.out.println(e.toString());}}System.out.println("操作完成");}@Overridepublic void failed(Throwable exc, Object attachment) {System.out.println("IO 操作是失敗: " + exc);}});try {Thread.sleep(Integer.MAX_VALUE);} catch (InterruptedException ex) {System.out.println(ex);}} catch (IOException e) {System.out.println(e);}} }

客戶端代碼:

package com.leon.vip.netty.io.aio; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.channels.AsynchronousSocketChannel; import java.nio.channels.CompletionHandler; /** * AIO 客戶端 */ public class AIOClient {private final AsynchronousSocketChannel client;public AIOClient() throws Exception{client = AsynchronousSocketChannel.open();}public void connect(String host,int port)throws Exception{client.connect(new InetSocketAddress(host,port),null,new CompletionHandler<Void,Void>() {@Overridepublic void completed(Void result, Void attachment) {try {client.write(ByteBuffer.wrap("這是一條測試數據".getBytes())).get();System.out.println("已發送至服務器");} catch (Exception ex) {ex.printStackTrace();}}@Overridepublic void failed(Throwable exc, Void attachment) {exc.printStackTrace();}});final ByteBuffer bb = ByteBuffer.allocate(1024);client.read(bb, null, new CompletionHandler<Integer,Object>(){@Overridepublic void completed(Integer result, Object attachment) {System.out.println("IO 操作完成" + result);System.out.println("獲取反饋結果" + new String(bb.array()));}@Overridepublic void failed(Throwable exc, Object attachment) {exc.printStackTrace();}});try {Thread.sleep(Integer.MAX_VALUE);} catch (InterruptedException ex) {System.out.println(ex);}}public static void main(String args[])throws Exception{new AIOClient().connect("localhost",8000);} }

?

?

?

總結

以上是生活随笔為你收集整理的AIO 初体验的全部內容,希望文章能夠幫你解決所遇到的問題。

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