From ab1e5374d71e5d14521c3d2bcaa233f197f31d27 Mon Sep 17 00:00:00 2001 From: bingor Date: Thu, 27 Oct 2022 14:19:34 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BD=91=E7=BB=9C=E7=BC=96=E7=A8=8B=E5=9F=BA?= =?UTF-8?q?=E7=A1=80-Aio?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/msb/io/aio/PoolServer.java | 74 ++++++++++++++++++++ src/main/java/com/msb/io/aio/Server.java | 69 ++++++++++++++++++ 2 files changed, 143 insertions(+) create mode 100644 src/main/java/com/msb/io/aio/PoolServer.java create mode 100644 src/main/java/com/msb/io/aio/Server.java diff --git a/src/main/java/com/msb/io/aio/PoolServer.java b/src/main/java/com/msb/io/aio/PoolServer.java new file mode 100644 index 0000000..ac77f3d --- /dev/null +++ b/src/main/java/com/msb/io/aio/PoolServer.java @@ -0,0 +1,74 @@ +package com.msb.io.aio;/** + * @Author bingor + * @Date 2022/10/27 11:35 + * @Description: com.msb.io.aio + * @Version: 1.0 + */ + +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; + +/** + *@ClassName Server + *@Description TODO + *@Author bingor + *@Date 2022/10/27 11:35 + *@Version 3.0 + */ +public class PoolServer { + public static void main(String[] args) throws IOException { + ExecutorService executorService = Executors.newCachedThreadPool(); + AsynchronousChannelGroup channelGroup = AsynchronousChannelGroup.withCachedThreadPool(executorService, 1); + final AsynchronousServerSocketChannel serverChannel = AsynchronousServerSocketChannel.open(channelGroup).bind(new InetSocketAddress(8888)); + serverChannel.accept(null, new CompletionHandler() { + + @Override + public void completed(AsynchronousSocketChannel client, Object attachment) { +// public void completed(AsynchronousSocketChannel result, Object attachment) { + serverChannel.accept(null, this); + try { + System.out.println(serverChannel.getLocalAddress()); + ByteBuffer byteBuffer = ByteBuffer.allocate(1024); + client.read(byteBuffer, byteBuffer, new CompletionHandler() { + @Override + public void completed(Integer result, ByteBuffer attachment) { + attachment.flip(); + System.out.println(new String(attachment.array(), 0, result)); + client.write(ByteBuffer.wrap("hello client!".getBytes())); + } + + @Override + public void failed(Throwable exc, ByteBuffer attachment) { + exc.printStackTrace(); + } + }); + } catch (IOException exception) { + exception.printStackTrace(); + } + + } + + @Override + public void failed(Throwable exc, Object attachment) { + exc.printStackTrace(); + } + }); + + System.out.println("server 执行返回"); + try { + while (true) { + Thread.sleep(1000); + } + } catch (InterruptedException e) { + e.printStackTrace(); + } + + } +} diff --git a/src/main/java/com/msb/io/aio/Server.java b/src/main/java/com/msb/io/aio/Server.java new file mode 100644 index 0000000..c2cc176 --- /dev/null +++ b/src/main/java/com/msb/io/aio/Server.java @@ -0,0 +1,69 @@ +package com.msb.io.aio;/** + * @Author bingor + * @Date 2022/10/27 11:35 + * @Description: com.msb.io.aio + * @Version: 1.0 + */ + +import java.io.IOException; +import java.net.InetSocketAddress; +import java.nio.ByteBuffer; +import java.nio.channels.AsynchronousServerSocketChannel; +import java.nio.channels.AsynchronousSocketChannel; +import java.nio.channels.CompletionHandler; + +/** + *@ClassName Server + *@Description TODO + *@Author bingor + *@Date 2022/10/27 11:35 + *@Version 3.0 + */ +public class Server { + public static void main(String[] args) throws IOException { + final AsynchronousServerSocketChannel serverChannel = AsynchronousServerSocketChannel.open().bind(new InetSocketAddress(8888)); + serverChannel.accept(null, new CompletionHandler() { + + @Override + public void completed(AsynchronousSocketChannel client, Object attachment) { +// public void completed(AsynchronousSocketChannel result, Object attachment) { + serverChannel.accept(null, this); + try { + System.out.println(serverChannel.getLocalAddress()); + ByteBuffer byteBuffer = ByteBuffer.allocate(1024); + client.read(byteBuffer, byteBuffer, new CompletionHandler() { + @Override + public void completed(Integer result, ByteBuffer attachment) { + attachment.flip(); + System.out.println(new String(attachment.array(), 0, result)); + client.write(ByteBuffer.wrap("hello client!".getBytes())); + } + + @Override + public void failed(Throwable exc, ByteBuffer attachment) { + exc.printStackTrace(); + } + }); + } catch (IOException exception) { + exception.printStackTrace(); + } + + } + + @Override + public void failed(Throwable exc, Object attachment) { + exc.printStackTrace(); + } + }); + + System.out.println("server 执行返回"); + try { + while (true) { + Thread.sleep(1000); + } + } catch (InterruptedException e) { + e.printStackTrace(); + } + + } +}