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(); + } + + } +}