From 00f5909741eb8aa88fdf27358ffeb89dda007869 Mon Sep 17 00:00:00 2001
From: fuzhengwei <184172133@qq.com>
Date: Wed, 15 Apr 2020 23:22:28 +0800
Subject: [PATCH] =?UTF-8?q?=E5=85=AC=E4=BC=97=E5=8F=B7=EF=BC=9Abugstack?=
=?UTF-8?q?=E8=99=AB=E6=B4=9E=E6=A0=88=20|=20=E5=88=9D=E5=85=A5JavaIO?=
=?UTF-8?q?=E4=B9=8B=E9=97=A8BIO=E3=80=81NIO=E3=80=81AIO=E5=AE=9E=E6=88=98?=
=?UTF-8?q?=E7=BB=83=E4=B9=A0?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../itstack-demo-netty-1-00/pom.xml | 15 +++
.../demo/netty/aio/ChannelAdapter.java | 77 +++++++++++++
.../demo/netty/aio/ChannelHandler.java | 39 +++++++
.../demo/netty/aio/ChannelInitializer.java | 33 ++++++
.../demo/netty/aio/client/AioClient.java | 25 +++++
.../netty/aio/client/AioClientHandler.java | 42 +++++++
.../demo/netty/aio/server/AioServer.java | 41 +++++++
.../server/AioServerChannelInitializer.java | 22 ++++
.../netty/aio/server/AioServerHandler.java | 44 ++++++++
.../demo/netty/bio/ChannelAdapter.java | 51 +++++++++
.../demo/netty/bio/ChannelHandler.java | 38 +++++++
.../demo/netty/bio/client/BioClient.java | 25 +++++
.../netty/bio/client/BioClientHandler.java | 34 ++++++
.../demo/netty/bio/server/BioServer.java | 39 +++++++
.../netty/bio/server/BioServerHandler.java | 34 ++++++
.../demo/netty/nio/ChannelAdapter.java | 103 ++++++++++++++++++
.../demo/netty/nio/ChannelHandler.java | 41 +++++++
.../demo/netty/nio/client/NioClient.java | 32 ++++++
.../netty/nio/client/NioClientHandler.java | 40 +++++++
.../demo/netty/nio/server/NioServer.java | 38 +++++++
.../netty/nio/server/NioServerHandler.java | 39 +++++++
.../org/itstack/demo/test/AioServerTest.java | 100 +++++++++++++++++
22 files changed, 952 insertions(+)
create mode 100644 src/itstack-demo-netty/itstack-demo-netty-1-00/pom.xml
create mode 100644 src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/aio/ChannelAdapter.java
create mode 100644 src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/aio/ChannelHandler.java
create mode 100644 src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/aio/ChannelInitializer.java
create mode 100644 src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/aio/client/AioClient.java
create mode 100644 src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/aio/client/AioClientHandler.java
create mode 100644 src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/aio/server/AioServer.java
create mode 100644 src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/aio/server/AioServerChannelInitializer.java
create mode 100644 src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/aio/server/AioServerHandler.java
create mode 100644 src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/bio/ChannelAdapter.java
create mode 100644 src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/bio/ChannelHandler.java
create mode 100644 src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/bio/client/BioClient.java
create mode 100644 src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/bio/client/BioClientHandler.java
create mode 100644 src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/bio/server/BioServer.java
create mode 100644 src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/bio/server/BioServerHandler.java
create mode 100644 src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/nio/ChannelAdapter.java
create mode 100644 src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/nio/ChannelHandler.java
create mode 100644 src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/nio/client/NioClient.java
create mode 100644 src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/nio/client/NioClientHandler.java
create mode 100644 src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/nio/server/NioServer.java
create mode 100644 src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/nio/server/NioServerHandler.java
create mode 100644 src/itstack-demo-netty/itstack-demo-netty-1-00/src/test/java/org/itstack/demo/test/AioServerTest.java
diff --git a/src/itstack-demo-netty/itstack-demo-netty-1-00/pom.xml b/src/itstack-demo-netty/itstack-demo-netty-1-00/pom.xml
new file mode 100644
index 000000000..f081e04be
--- /dev/null
+++ b/src/itstack-demo-netty/itstack-demo-netty-1-00/pom.xml
@@ -0,0 +1,15 @@
+
+
+
+ itstack-demo-netty
+ org.itatack.demo
+ 1.0.0-SNAPSHOT
+
+ 4.0.0
+
+ itstack-demo-netty-1-00
+
+
+
\ No newline at end of file
diff --git a/src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/aio/ChannelAdapter.java b/src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/aio/ChannelAdapter.java
new file mode 100644
index 000000000..798836557
--- /dev/null
+++ b/src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/aio/ChannelAdapter.java
@@ -0,0 +1,77 @@
+package org.itstack.demo.netty.aio;
+
+import io.netty.channel.ChannelHandlerContext;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.channels.AsynchronousSocketChannel;
+import java.nio.channels.CompletionHandler;
+import java.nio.charset.Charset;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * 消息处理器
+ * 微信公众号:bugstack虫洞栈 | 专注原创技术专题案例,以最易学习编程的方式分享知识,让萌新、小白、大牛都能有所收获。目前已完成的专题有;Netty4.x从入门到实战、用Java实现JVM、基于JavaAgent的全链路监控等,其他更多专题还在排兵布阵中。
+ * 论坛:http://bugstack.cn
+ * Create by 付政委 on @2019
+ */
+public abstract class ChannelAdapter implements CompletionHandler {
+
+ private AsynchronousSocketChannel channel;
+ private Charset charset;
+
+ public ChannelAdapter(AsynchronousSocketChannel channel, Charset charset) {
+ this.channel = channel;
+ this.charset = charset;
+ if (channel.isOpen()) {
+ channelActive(new ChannelHandler(channel, charset));
+ }
+ }
+
+ @Override
+ public void completed(Integer result, Object attachment) {
+ try {
+ final ByteBuffer buffer = ByteBuffer.allocate(1024);
+ final long timeout = 60 * 60L;
+ channel.read(buffer, timeout, TimeUnit.SECONDS, null, new CompletionHandler() {
+ @Override
+ public void completed(Integer result, Object attachment) {
+ if (result == -1) {
+ try {
+ channelInactive(new ChannelHandler(channel, charset));
+ channel.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ return;
+ }
+ buffer.flip();
+ channelRead(new ChannelHandler(channel, charset), charset.decode(buffer));
+ buffer.clear();
+ channel.read(buffer, timeout, TimeUnit.SECONDS, null, this);
+ }
+
+ @Override
+ public void failed(Throwable exc, Object attachment) {
+ exc.printStackTrace();
+ }
+ });
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+
+ @Override
+ public void failed(Throwable exc, Object attachment) {
+ exc.getStackTrace();
+ }
+
+ public abstract void channelActive(ChannelHandler ctx);
+
+ public abstract void channelInactive(ChannelHandler ctx);
+
+ // 读取消息抽象类
+ public abstract void channelRead(ChannelHandler ctx, Object msg);
+
+}
diff --git a/src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/aio/ChannelHandler.java b/src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/aio/ChannelHandler.java
new file mode 100644
index 000000000..936862344
--- /dev/null
+++ b/src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/aio/ChannelHandler.java
@@ -0,0 +1,39 @@
+package org.itstack.demo.netty.aio;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.channels.AsynchronousSocketChannel;
+import java.nio.channels.SocketChannel;
+import java.nio.charset.Charset;
+
+/**
+ * 微信公众号:bugstack虫洞栈 | 专注原创技术专题案例,以最易学习编程的方式分享知识,让萌新、小白、大牛都能有所收获。目前已完成的专题有;Netty4.x从入门到实战、用Java实现JVM、基于JavaAgent的全链路监控等,其他更多专题还在排兵布阵中。
+ * 论坛:http://bugstack.cn
+ * Create by 付政委 on @2019
+ */
+public class ChannelHandler {
+
+ private AsynchronousSocketChannel channel;
+ private Charset charset;
+
+ public ChannelHandler(AsynchronousSocketChannel channel, Charset charset) {
+ this.channel = channel;
+ this.charset = charset;
+ }
+
+ public void writeAndFlush(Object msg) {
+ byte[] bytes = msg.toString().getBytes(charset);
+ ByteBuffer writeBuffer = ByteBuffer.allocate(bytes.length);
+ writeBuffer.put(bytes);
+ writeBuffer.flip();
+ channel.write(writeBuffer);
+ }
+
+ public AsynchronousSocketChannel channel() {
+ return channel;
+ }
+
+ public void setChannel(AsynchronousSocketChannel channel) {
+ this.channel = channel;
+ }
+}
diff --git a/src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/aio/ChannelInitializer.java b/src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/aio/ChannelInitializer.java
new file mode 100644
index 000000000..007ee0ca5
--- /dev/null
+++ b/src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/aio/ChannelInitializer.java
@@ -0,0 +1,33 @@
+package org.itstack.demo.netty.aio;
+
+import org.itstack.demo.netty.aio.server.AioServer;
+
+import java.nio.channels.AsynchronousSocketChannel;
+import java.nio.channels.CompletionHandler;
+
+/**
+ * 微信公众号:bugstack虫洞栈 | 专注原创技术专题案例,以最易学习编程的方式分享知识,让萌新、小白、大牛都能有所收获。目前已完成的专题有;Netty4.x从入门到实战、用Java实现JVM、基于JavaAgent的全链路监控等,其他更多专题还在排兵布阵中。
+ * 论坛:http://bugstack.cn
+ * Create by 付政委 on @2019
+ */
+public abstract class ChannelInitializer implements CompletionHandler {
+
+ @Override
+ public void completed(AsynchronousSocketChannel channel, AioServer attachment) {
+ try {
+ initChannel(channel);
+ } catch (Exception e) {
+ e.printStackTrace();
+ } finally {
+ attachment.serverSocketChannel().accept(attachment, this);// 再此接收客户端连接
+ }
+ }
+
+ @Override
+ public void failed(Throwable exc, AioServer attachment) {
+ exc.getStackTrace();
+ }
+
+ protected abstract void initChannel(AsynchronousSocketChannel channel) throws Exception;
+
+}
diff --git a/src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/aio/client/AioClient.java b/src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/aio/client/AioClient.java
new file mode 100644
index 000000000..e489a39ee
--- /dev/null
+++ b/src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/aio/client/AioClient.java
@@ -0,0 +1,25 @@
+package org.itstack.demo.netty.aio.client;
+
+import java.net.InetSocketAddress;
+import java.nio.ByteBuffer;
+import java.nio.channels.AsynchronousSocketChannel;
+import java.nio.charset.Charset;
+import java.util.concurrent.Future;
+
+/**
+ * 微信公众号:bugstack虫洞栈 | 专注原创技术专题案例,以最易学习编程的方式分享知识,让萌新、小白、大牛都能有所收获。目前已完成的专题有;Netty4.x从入门到实战、用Java实现JVM、基于JavaAgent的全链路监控等,其他更多专题还在排兵布阵中。
+ * 论坛:http://bugstack.cn
+ * Create by 付政委 on @2019
+ */
+public class AioClient {
+
+ public static void main(String[] args) throws Exception {
+ AsynchronousSocketChannel socketChannel = AsynchronousSocketChannel.open();
+ Future future = socketChannel.connect(new InetSocketAddress("192.168.1.116", 7397));
+ System.out.println("itstack-demo-netty aio client start done. {关注公众号:bugstack虫洞栈 | 欢迎关注&获取源码}");
+ future.get();
+ socketChannel.read(ByteBuffer.allocate(1024), null, new AioClientHandler(socketChannel, Charset.forName("GBK")));
+ Thread.sleep(100000);
+ }
+
+}
diff --git a/src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/aio/client/AioClientHandler.java b/src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/aio/client/AioClientHandler.java
new file mode 100644
index 000000000..bf03177f6
--- /dev/null
+++ b/src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/aio/client/AioClientHandler.java
@@ -0,0 +1,42 @@
+package org.itstack.demo.netty.aio.client;
+
+import org.itstack.demo.netty.aio.ChannelAdapter;
+import org.itstack.demo.netty.aio.ChannelHandler;
+
+import java.io.IOException;
+import java.nio.channels.AsynchronousSocketChannel;
+import java.nio.charset.Charset;
+import java.util.Date;
+
+/**
+ * 微信公众号:bugstack虫洞栈 | 专注原创技术专题案例,以最易学习编程的方式分享知识,让萌新、小白、大牛都能有所收获。目前已完成的专题有;Netty4.x从入门到实战、用Java实现JVM、基于JavaAgent的全链路监控等,其他更多专题还在排兵布阵中。
+ * 论坛:http://bugstack.cn
+ * Create by 付政委 on @2019
+ */
+public class AioClientHandler extends ChannelAdapter {
+
+ public AioClientHandler(AsynchronousSocketChannel channel, Charset charset) {
+ super(channel, charset);
+ }
+
+ @Override
+ public void channelActive(ChannelHandler ctx) {
+ try {
+ System.out.println("微信公众号:bugstack虫洞栈 | 链接报告信息:" + ctx.channel().getRemoteAddress());
+ //通知客户端链接建立成功
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
+ @Override
+ public void channelInactive(ChannelHandler ctx) {
+ }
+
+ @Override
+ public void channelRead(ChannelHandler ctx, Object msg) {
+ System.out.println("微信公众号:bugstack虫洞栈 | 服务端收到:" + new Date() + " " + msg + "\r\n");
+ ctx.writeAndFlush("客户端信息处理Success!\r\n");
+ }
+
+}
diff --git a/src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/aio/server/AioServer.java b/src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/aio/server/AioServer.java
new file mode 100644
index 000000000..86607da31
--- /dev/null
+++ b/src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/aio/server/AioServer.java
@@ -0,0 +1,41 @@
+package org.itstack.demo.netty.aio.server;
+
+import java.net.InetSocketAddress;
+import java.nio.channels.AsynchronousChannelGroup;
+import java.nio.channels.AsynchronousServerSocketChannel;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.Executors;
+
+/**
+ * 微信公众号:bugstack虫洞栈 | 专注原创技术专题案例,以最易学习编程的方式分享知识,让萌新、小白、大牛都能有所收获。目前已完成的专题有;Netty4.x从入门到实战、用Java实现JVM、基于JavaAgent的全链路监控等,其他更多专题还在排兵布阵中。
+ * 论坛:http://bugstack.cn
+ * Create by 付政委 on @2019
+ */
+public class AioServer extends Thread {
+
+ private AsynchronousServerSocketChannel serverSocketChannel;
+
+ @Override
+ public void run() {
+ try {
+ serverSocketChannel = AsynchronousServerSocketChannel.open(AsynchronousChannelGroup.withCachedThreadPool(Executors.newCachedThreadPool(), 10));
+ serverSocketChannel.bind(new InetSocketAddress(7397));
+ System.out.println("itstack-demo-netty aio server start done. {关注公众号:bugstack虫洞栈 | 欢迎关注&获取源码}");
+ // 等待
+ CountDownLatch latch = new CountDownLatch(1);
+ serverSocketChannel.accept(this, new AioServerChannelInitializer());
+ latch.await();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ public AsynchronousServerSocketChannel serverSocketChannel() {
+ return serverSocketChannel;
+ }
+
+ public static void main(String[] args) {
+ new AioServer().start();
+ }
+
+}
diff --git a/src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/aio/server/AioServerChannelInitializer.java b/src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/aio/server/AioServerChannelInitializer.java
new file mode 100644
index 000000000..08c7e45f2
--- /dev/null
+++ b/src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/aio/server/AioServerChannelInitializer.java
@@ -0,0 +1,22 @@
+package org.itstack.demo.netty.aio.server;
+
+import org.itstack.demo.netty.aio.ChannelInitializer;
+
+import java.nio.ByteBuffer;
+import java.nio.channels.AsynchronousSocketChannel;
+import java.nio.charset.Charset;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * 微信公众号:bugstack虫洞栈 | 专注原创技术专题案例,以最易学习编程的方式分享知识,让萌新、小白、大牛都能有所收获。目前已完成的专题有;Netty4.x从入门到实战、用Java实现JVM、基于JavaAgent的全链路监控等,其他更多专题还在排兵布阵中。
+ * 论坛:http://bugstack.cn
+ * Create by 付政委 on @2019
+ */
+public class AioServerChannelInitializer extends ChannelInitializer {
+
+ @Override
+ protected void initChannel(AsynchronousSocketChannel channel) throws Exception {
+ channel.read(ByteBuffer.allocate(1024), 10, TimeUnit.SECONDS, null, new AioServerHandler(channel, Charset.forName("GBK")));
+ }
+
+}
diff --git a/src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/aio/server/AioServerHandler.java b/src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/aio/server/AioServerHandler.java
new file mode 100644
index 000000000..5dff39405
--- /dev/null
+++ b/src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/aio/server/AioServerHandler.java
@@ -0,0 +1,44 @@
+package org.itstack.demo.netty.aio.server;
+
+
+import org.itstack.demo.netty.aio.ChannelAdapter;
+import org.itstack.demo.netty.aio.ChannelHandler;
+
+import java.io.IOException;
+import java.nio.channels.AsynchronousSocketChannel;
+import java.nio.charset.Charset;
+import java.util.Date;
+
+/**
+ * 微信公众号:bugstack虫洞栈 | 专注原创技术专题案例,以最易学习编程的方式分享知识,让萌新、小白、大牛都能有所收获。目前已完成的专题有;Netty4.x从入门到实战、用Java实现JVM、基于JavaAgent的全链路监控等,其他更多专题还在排兵布阵中。
+ * 论坛:http://bugstack.cn
+ * Create by 付政委 on @2019
+ */
+public class AioServerHandler extends ChannelAdapter {
+
+ public AioServerHandler(AsynchronousSocketChannel channel, Charset charset) {
+ super(channel, charset);
+ }
+
+ @Override
+ public void channelActive(ChannelHandler ctx) {
+ try {
+ System.out.println("微信公众号:bugstack虫洞栈 | 链接报告信息:" + ctx.channel().getRemoteAddress());
+ //通知客户端链接建立成功
+ ctx.writeAndFlush("微信公众号:bugstack虫洞栈 | 通知服务端链接建立成功" + " " + new Date() + " " + ctx.channel().getRemoteAddress() + "\r\n");
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
+ @Override
+ public void channelInactive(ChannelHandler ctx) {
+ }
+
+ @Override
+ public void channelRead(ChannelHandler ctx, Object msg) {
+ System.out.println("微信公众号:bugstack虫洞栈 | 服务端收到:" + new Date() + " " + msg + "\r\n");
+ ctx.writeAndFlush("服务端信息处理Success!\r\n");
+ }
+
+}
diff --git a/src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/bio/ChannelAdapter.java b/src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/bio/ChannelAdapter.java
new file mode 100644
index 000000000..e1fd1fc44
--- /dev/null
+++ b/src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/bio/ChannelAdapter.java
@@ -0,0 +1,51 @@
+package org.itstack.demo.netty.bio;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.net.Socket;
+import java.nio.charset.Charset;
+
+/**
+ * 微信公众号:bugstack虫洞栈 | 专注原创技术专题案例,以最易学习编程的方式分享知识,让萌新、小白、大牛都能有所收获。目前已完成的专题有;Netty4.x从入门到实战、用Java实现JVM、基于JavaAgent的全链路监控等,其他更多专题还在排兵布阵中。
+ * 论坛:http://bugstack.cn
+ * Create by 付政委 on @2019
+ */
+public abstract class ChannelAdapter extends Thread {
+
+ private Socket socket;
+ private ChannelHandler channelHandler;
+ private Charset charset;
+
+ public ChannelAdapter(Socket socket, Charset charset) {
+ this.socket = socket;
+ this.charset = charset;
+ while (!socket.isConnected()) {
+ break;
+ }
+ channelHandler = new ChannelHandler(this.socket, charset);
+ channelActive(channelHandler);
+ }
+
+ @Override
+ public void run() {
+ try {
+ BufferedReader input = new BufferedReader(new InputStreamReader(this.socket.getInputStream(), charset));
+ String str = null;
+ while ((str = input.readLine()) != null) {
+ channelRead(channelHandler, str);
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
+ // 链接通知抽象类
+ public abstract void channelActive(ChannelHandler ctx);
+
+ // 读取消息抽象类
+ public abstract void channelRead(ChannelHandler ctx, Object msg);
+
+}
+
+
diff --git a/src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/bio/ChannelHandler.java b/src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/bio/ChannelHandler.java
new file mode 100644
index 000000000..7435998a9
--- /dev/null
+++ b/src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/bio/ChannelHandler.java
@@ -0,0 +1,38 @@
+package org.itstack.demo.netty.bio;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.net.Socket;
+import java.nio.charset.Charset;
+
+/**
+ * 微信公众号:bugstack虫洞栈 | 专注原创技术专题案例,以最易学习编程的方式分享知识,让萌新、小白、大牛都能有所收获。目前已完成的专题有;Netty4.x从入门到实战、用Java实现JVM、基于JavaAgent的全链路监控等,其他更多专题还在排兵布阵中。
+ * 论坛:http://bugstack.cn
+ * Create by 付政委 on @2019
+ */
+public class ChannelHandler {
+
+ private Socket socket;
+ private Charset charset;
+
+ public ChannelHandler(Socket socket, Charset charset) {
+ this.socket = socket;
+ this.charset = charset;
+ }
+
+ public void writeAndFlush(Object msg) {
+ OutputStream out = null;
+ try {
+ out = socket.getOutputStream();
+ out.write((msg.toString()).getBytes(charset));
+ out.flush();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
+ public Socket socket() {
+ return socket;
+ }
+
+}
diff --git a/src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/bio/client/BioClient.java b/src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/bio/client/BioClient.java
new file mode 100644
index 000000000..ce39f7712
--- /dev/null
+++ b/src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/bio/client/BioClient.java
@@ -0,0 +1,25 @@
+package org.itstack.demo.netty.bio.client;
+
+import java.io.IOException;
+import java.net.Socket;
+import java.nio.charset.Charset;
+
+/**
+ * 微信公众号:bugstack虫洞栈 | 专注原创技术专题案例,以最易学习编程的方式分享知识,让萌新、小白、大牛都能有所收获。目前已完成的专题有;Netty4.x从入门到实战、用Java实现JVM、基于JavaAgent的全链路监控等,其他更多专题还在排兵布阵中。
+ * 论坛:http://bugstack.cn
+ * Create by 付政委 on @2019
+ */
+public class BioClient {
+
+ public static void main(String[] args) {
+ try {
+ Socket socket = new Socket("192.168.1.116", 7397);
+ System.out.println("itstack-demo-netty bio client start done. {关注公众号:bugstack虫洞栈 | 欢迎关注&获取源码}");
+ BioClientHandler bioClientHandler = new BioClientHandler(socket, Charset.forName("utf-8"));
+ bioClientHandler.start();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
+}
diff --git a/src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/bio/client/BioClientHandler.java b/src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/bio/client/BioClientHandler.java
new file mode 100644
index 000000000..85d294587
--- /dev/null
+++ b/src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/bio/client/BioClientHandler.java
@@ -0,0 +1,34 @@
+package org.itstack.demo.netty.bio.client;
+
+import org.itstack.demo.netty.bio.ChannelAdapter;
+import org.itstack.demo.netty.bio.ChannelHandler;
+
+import java.net.Socket;
+import java.nio.charset.Charset;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+/**
+ * 微信公众号:bugstack虫洞栈 | 专注原创技术专题案例,以最易学习编程的方式分享知识,让萌新、小白、大牛都能有所收获。目前已完成的专题有;Netty4.x从入门到实战、用Java实现JVM、基于JavaAgent的全链路监控等,其他更多专题还在排兵布阵中。
+ * 论坛:http://bugstack.cn
+ * Create by 付政委 on @2019
+ */
+public class BioClientHandler extends ChannelAdapter {
+
+ public BioClientHandler(Socket socket, Charset charset) {
+ super(socket, charset);
+ }
+
+ @Override
+ public void channelActive(ChannelHandler ctx) {
+ System.out.println("链接报告LocalAddress:" + ctx.socket().getLocalAddress());
+ ctx.writeAndFlush("hi! 我是bugstack虫洞栈 BioClient to msg for you \r\n");
+ }
+
+ @Override
+ public void channelRead(ChannelHandler ctx, Object msg) {
+ System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + " 接收到消息:" + msg);
+ ctx.writeAndFlush("hi 我已经收到你的消息Success!\r\n");
+ }
+
+}
diff --git a/src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/bio/server/BioServer.java b/src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/bio/server/BioServer.java
new file mode 100644
index 000000000..b884d95b6
--- /dev/null
+++ b/src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/bio/server/BioServer.java
@@ -0,0 +1,39 @@
+package org.itstack.demo.netty.bio.server;
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.nio.charset.Charset;
+
+/**
+ * 博客:http://itstack.org
+ * 论坛:http://bugstack.cn
+ * 公众号:bugstack虫洞栈 {获取学习源码}
+ * Create by fuzhengwei on 2019/9/30
+ */
+public class BioServer extends Thread {
+
+ private ServerSocket serverSocket = null;
+
+ public static void main(String[] args) {
+ BioServer bioServer = new BioServer();
+ bioServer.start();
+ }
+
+ @Override
+ public void run() {
+ try {
+ serverSocket = new ServerSocket();
+ serverSocket.bind(new InetSocketAddress(7397));
+ System.out.println("itstack-demo-netty bio server start done. {关注公众号:bugstack虫洞栈 | 欢迎关注&获取源码}");
+ while (true) {
+ Socket socket = serverSocket.accept();
+ BioServerHandler handler = new BioServerHandler(socket, Charset.forName("GBK"));
+ handler.start();
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+}
diff --git a/src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/bio/server/BioServerHandler.java b/src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/bio/server/BioServerHandler.java
new file mode 100644
index 000000000..6b76188bf
--- /dev/null
+++ b/src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/bio/server/BioServerHandler.java
@@ -0,0 +1,34 @@
+package org.itstack.demo.netty.bio.server;
+
+import org.itstack.demo.netty.bio.ChannelAdapter;
+import org.itstack.demo.netty.bio.ChannelHandler;
+
+import java.net.Socket;
+import java.nio.charset.Charset;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+/**
+ * 微信公众号:bugstack虫洞栈 | 专注原创技术专题案例,以最易学习编程的方式分享知识,让萌新、小白、大牛都能有所收获。目前已完成的专题有;Netty4.x从入门到实战、用Java实现JVM、基于JavaAgent的全链路监控等,其他更多专题还在排兵布阵中。
+ * 论坛:http://bugstack.cn
+ * Create by 付政委 on @2019
+ */
+public class BioServerHandler extends ChannelAdapter {
+
+ public BioServerHandler(Socket socket, Charset charset) {
+ super(socket, charset);
+ }
+
+ @Override
+ public void channelActive(ChannelHandler ctx) {
+ System.out.println("链接报告LocalAddress:" + ctx.socket().getLocalAddress());
+ ctx.writeAndFlush("hi! 我是bugstack虫洞栈 BioServer to msg for you \r\n");
+ }
+
+ @Override
+ public void channelRead(ChannelHandler ctx, Object msg) {
+ System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + " 接收到消息:" + msg);
+ ctx.writeAndFlush("hi 我已经收到你的消息Success!\r\n");
+ }
+
+}
diff --git a/src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/nio/ChannelAdapter.java b/src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/nio/ChannelAdapter.java
new file mode 100644
index 000000000..c15bc3dd2
--- /dev/null
+++ b/src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/nio/ChannelAdapter.java
@@ -0,0 +1,103 @@
+package org.itstack.demo.netty.nio;
+
+import org.itstack.demo.netty.nio.ChannelHandler;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.channels.SelectionKey;
+import java.nio.channels.Selector;
+import java.nio.channels.ServerSocketChannel;
+import java.nio.channels.SocketChannel;
+import java.nio.charset.Charset;
+import java.util.Iterator;
+import java.util.Set;
+
+/**
+ * 微信公众号:bugstack虫洞栈 | 专注原创技术专题案例,以最易学习编程的方式分享知识,让萌新、小白、大牛都能有所收获。目前已完成的专题有;Netty4.x从入门到实战、用Java实现JVM、基于JavaAgent的全链路监控等,其他更多专题还在排兵布阵中。
+ * 论坛:http://bugstack.cn
+ * Create by 付政委 on @2019
+ */
+public abstract class ChannelAdapter extends Thread {
+
+ private Selector selector;
+
+ private ChannelHandler channelHandler;
+ private Charset charset;
+
+ public ChannelAdapter(Selector selector, Charset charset) {
+ this.selector = selector;
+ this.charset = charset;
+ }
+
+ @Override
+ public void run() {
+ while (true) {
+ try {
+ selector.select(1000); //Selects a set of keys whose corresponding channels are ready for I/O
+ Set selectedKeys = selector.selectedKeys();
+ Iterator it = selectedKeys.iterator();
+ SelectionKey key = null;
+ while (it.hasNext()) {
+ key = it.next();
+ it.remove();
+ handleInput(key);
+ }
+ } catch (Exception ignore) {
+ }
+ }
+ }
+
+ private void handleInput(SelectionKey key) throws IOException {
+ if (!key.isValid()) return;
+
+ // 客户端SocketChannel
+ Class> superclass = key.channel().getClass().getSuperclass();
+ if (superclass == SocketChannel.class){
+ SocketChannel socketChannel = (SocketChannel) key.channel();
+ if (key.isConnectable()) {
+ if (socketChannel.finishConnect()) {
+ channelHandler = new ChannelHandler(socketChannel, charset);
+ channelActive(channelHandler);
+ socketChannel.register(selector, SelectionKey.OP_READ);
+ } else {
+ System.exit(1);
+ }
+ }
+ }
+
+ // 服务端ServerSocketChannel
+ if (superclass == ServerSocketChannel.class){
+ if (key.isAcceptable()) {
+ ServerSocketChannel serverSocketChannel = (ServerSocketChannel) key.channel();
+ SocketChannel socketChannel = serverSocketChannel.accept();
+ socketChannel.configureBlocking(false);
+ socketChannel.register(selector, SelectionKey.OP_READ);
+
+ channelHandler = new ChannelHandler(socketChannel, charset);
+ channelActive(channelHandler);
+ }
+ }
+
+ if (key.isReadable()) {
+ SocketChannel socketChannel = (SocketChannel) key.channel();
+ ByteBuffer readBuffer = ByteBuffer.allocate(1024);
+ int readBytes = socketChannel.read(readBuffer);
+ if (readBytes > 0) {
+ readBuffer.flip();
+ byte[] bytes = new byte[readBuffer.remaining()];
+ readBuffer.get(bytes);
+ channelRead(channelHandler, new String(bytes, charset));
+ } else if (readBytes < 0) {
+ key.cancel();
+ socketChannel.close();
+ }
+ }
+ }
+
+ // 链接通知抽象类
+ public abstract void channelActive(ChannelHandler ctx);
+
+ // 读取消息抽象类
+ public abstract void channelRead(ChannelHandler ctx, Object msg);
+
+}
diff --git a/src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/nio/ChannelHandler.java b/src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/nio/ChannelHandler.java
new file mode 100644
index 000000000..7aec0c1f3
--- /dev/null
+++ b/src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/nio/ChannelHandler.java
@@ -0,0 +1,41 @@
+package org.itstack.demo.netty.nio;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.net.Socket;
+import java.nio.ByteBuffer;
+import java.nio.channels.SocketChannel;
+import java.nio.charset.Charset;
+
+/**
+ * 微信公众号:bugstack虫洞栈 | 专注原创技术专题案例,以最易学习编程的方式分享知识,让萌新、小白、大牛都能有所收获。目前已完成的专题有;Netty4.x从入门到实战、用Java实现JVM、基于JavaAgent的全链路监控等,其他更多专题还在排兵布阵中。
+ * 论坛:http://bugstack.cn
+ * Create by 付政委 on @2019
+ */
+public class ChannelHandler {
+
+ private SocketChannel channel;
+ private Charset charset;
+
+ public ChannelHandler(SocketChannel channel, Charset charset) {
+ this.channel = channel;
+ this.charset = charset;
+ }
+
+ public void writeAndFlush(Object msg) {
+ try {
+ byte[] bytes = msg.toString().getBytes(charset);
+ ByteBuffer writeBuffer = ByteBuffer.allocate(bytes.length);
+ writeBuffer.put(bytes);
+ writeBuffer.flip();
+ channel.write(writeBuffer);
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
+ public SocketChannel channel() {
+ return channel;
+ }
+
+}
diff --git a/src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/nio/client/NioClient.java b/src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/nio/client/NioClient.java
new file mode 100644
index 000000000..79f478fcb
--- /dev/null
+++ b/src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/nio/client/NioClient.java
@@ -0,0 +1,32 @@
+package org.itstack.demo.netty.nio.client;
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.nio.channels.SelectionKey;
+import java.nio.channels.Selector;
+import java.nio.channels.SocketChannel;
+import java.nio.charset.Charset;
+
+/**
+ * 微信公众号:bugstack虫洞栈 | 专注原创技术专题案例,以最易学习编程的方式分享知识,让萌新、小白、大牛都能有所收获。目前已完成的专题有;Netty4.x从入门到实战、用Java实现JVM、基于JavaAgent的全链路监控等,其他更多专题还在排兵布阵中。
+ * 论坛:http://bugstack.cn
+ * Create by 付政委 on @2019
+ */
+public class NioClient {
+
+ public static void main(String[] args) throws IOException {
+ Selector selector = Selector.open();
+ SocketChannel socketChannel = SocketChannel.open();
+ socketChannel.configureBlocking(false);
+
+ boolean isConnect = socketChannel.connect(new InetSocketAddress("192.168.1.116", 7397));
+ if (isConnect) {
+ socketChannel.register(selector, SelectionKey.OP_READ);
+ } else {
+ socketChannel.register(selector, SelectionKey.OP_CONNECT);
+ }
+ System.out.println("itstack-demo-netty nio client start done. {关注公众号:bugstack虫洞栈 | 欢迎关注&获取源码}");
+ new NioClientHandler(selector, Charset.forName("GBK")).start();
+ }
+
+}
diff --git a/src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/nio/client/NioClientHandler.java b/src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/nio/client/NioClientHandler.java
new file mode 100644
index 000000000..e2b1537e4
--- /dev/null
+++ b/src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/nio/client/NioClientHandler.java
@@ -0,0 +1,40 @@
+package org.itstack.demo.netty.nio.client;
+
+
+import org.itstack.demo.netty.nio.ChannelAdapter;
+import org.itstack.demo.netty.nio.ChannelHandler;
+
+import java.io.IOException;
+import java.nio.channels.Selector;
+import java.nio.charset.Charset;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+/**
+ * 微信公众号:bugstack虫洞栈 | 专注原创技术专题案例,以最易学习编程的方式分享知识,让萌新、小白、大牛都能有所收获。目前已完成的专题有;Netty4.x从入门到实战、用Java实现JVM、基于JavaAgent的全链路监控等,其他更多专题还在排兵布阵中。
+ * 论坛:http://bugstack.cn
+ * Create by 付政委 on @2019
+ */
+public class NioClientHandler extends ChannelAdapter {
+
+ public NioClientHandler(Selector selector, Charset charset) {
+ super(selector, charset);
+ }
+
+ @Override
+ public void channelActive(ChannelHandler ctx) {
+ try {
+ System.out.println("链接报告LocalAddress:" + ctx.channel().getLocalAddress());
+ ctx.writeAndFlush("hi! 我是bugstack虫洞栈 NioClient to msg for you \r\n");
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
+ @Override
+ public void channelRead(ChannelHandler ctx, Object msg) {
+ System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + " 接收到消息:" + msg);
+ ctx.writeAndFlush("hi 我已经收到你的消息Success!\r\n");
+ }
+
+}
diff --git a/src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/nio/server/NioServer.java b/src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/nio/server/NioServer.java
new file mode 100644
index 000000000..239c1a843
--- /dev/null
+++ b/src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/nio/server/NioServer.java
@@ -0,0 +1,38 @@
+package org.itstack.demo.netty.nio.server;
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.nio.channels.SelectionKey;
+import java.nio.channels.Selector;
+import java.nio.channels.ServerSocketChannel;
+import java.nio.charset.Charset;
+
+/**
+ * 微信公众号:bugstack虫洞栈 | 专注原创技术专题案例,以最易学习编程的方式分享知识,让萌新、小白、大牛都能有所收获。目前已完成的专题有;Netty4.x从入门到实战、用Java实现JVM、基于JavaAgent的全链路监控等,其他更多专题还在排兵布阵中。
+ * 论坛:http://bugstack.cn
+ * Create by 付政委 on @2019
+ */
+public class NioServer {
+
+ private Selector selector;
+ private ServerSocketChannel socketChannel;
+
+ public static void main(String[] args) throws IOException {
+ new NioServer().bind(7397);
+ }
+
+ public void bind(int port) {
+ try {
+ selector = Selector.open();
+ socketChannel = ServerSocketChannel.open();
+ socketChannel.configureBlocking(false);
+ socketChannel.socket().bind(new InetSocketAddress(port), 1024);
+ socketChannel.register(selector, SelectionKey.OP_ACCEPT);
+ System.out.println("itstack-demo-netty nio server start done. {关注公众号:bugstack虫洞栈 | 欢迎关注&获取源码}");
+ new NioServerHandler(selector, Charset.forName("GBK")).start();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
+}
diff --git a/src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/nio/server/NioServerHandler.java b/src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/nio/server/NioServerHandler.java
new file mode 100644
index 000000000..3a7ea802d
--- /dev/null
+++ b/src/itstack-demo-netty/itstack-demo-netty-1-00/src/main/java/org/itstack/demo/netty/nio/server/NioServerHandler.java
@@ -0,0 +1,39 @@
+package org.itstack.demo.netty.nio.server;
+
+import org.itstack.demo.netty.nio.ChannelAdapter;
+import org.itstack.demo.netty.nio.ChannelHandler;
+
+import java.io.IOException;
+import java.nio.channels.Selector;
+import java.nio.charset.Charset;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+/**
+ * 微信公众号:bugstack虫洞栈 | 专注原创技术专题案例,以最易学习编程的方式分享知识,让萌新、小白、大牛都能有所收获。目前已完成的专题有;Netty4.x从入门到实战、用Java实现JVM、基于JavaAgent的全链路监控等,其他更多专题还在排兵布阵中。
+ * 论坛:http://bugstack.cn
+ * Create by 付政委 on @2019
+ */
+public class NioServerHandler extends ChannelAdapter {
+
+ public NioServerHandler(Selector selector, Charset charset) {
+ super(selector, charset);
+ }
+
+ @Override
+ public void channelActive(ChannelHandler ctx) {
+ try {
+ System.out.println("链接报告LocalAddress:" + ctx.channel().getLocalAddress());
+ ctx.writeAndFlush("hi! 我是bugstack虫洞栈 NioServer to msg for you \r\n");
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
+ @Override
+ public void channelRead(ChannelHandler ctx, Object msg) {
+ System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + " 接收到消息:" + msg);
+ ctx.writeAndFlush("hi 我已经收到你的消息Success!\r\n");
+ }
+
+}
diff --git a/src/itstack-demo-netty/itstack-demo-netty-1-00/src/test/java/org/itstack/demo/test/AioServerTest.java b/src/itstack-demo-netty/itstack-demo-netty-1-00/src/test/java/org/itstack/demo/test/AioServerTest.java
new file mode 100644
index 000000000..fa5a2bf9b
--- /dev/null
+++ b/src/itstack-demo-netty/itstack-demo-netty-1-00/src/test/java/org/itstack/demo/test/AioServerTest.java
@@ -0,0 +1,100 @@
+package org.itstack.demo.test;
+
+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.nio.charset.Charset;
+import java.util.concurrent.*;
+
+public class AioServerTest {
+
+ public final static int PORT = 7397;
+ private AsynchronousServerSocketChannel server;
+
+ public AioServerTest() throws IOException {
+ server = AsynchronousServerSocketChannel.open(AsynchronousChannelGroup.withCachedThreadPool(Executors.newCachedThreadPool(), 10)).bind(new InetSocketAddress(PORT));
+ }
+
+ public void startWithFuture() throws InterruptedException,
+ ExecutionException, TimeoutException {
+ while (true) {// 循环接收客户端请求
+ Future future = server.accept();
+ AsynchronousSocketChannel socket = future.get();// get() 是为了确保 accept 到一个连接
+ handleWithFuture(socket);
+ }
+ }
+
+ public void handleWithFuture(AsynchronousSocketChannel channel) throws InterruptedException, ExecutionException, TimeoutException {
+ ByteBuffer readBuf = ByteBuffer.allocate(1024);
+ readBuf.clear();
+
+ while (true) {// 一次可能读不完
+ //get 是为了确保 read 完成,超时时间可以有效避免DOS攻击,如果客户端一直不发送数据,则进行超时处理
+ Integer integer = channel.read(readBuf).get(10, TimeUnit.SECONDS);
+ System.out.println("read: " + integer);
+ if (integer == -1) {
+ break;
+ }
+ readBuf.flip();
+ System.out.println("received: " + Charset.forName("UTF-8").decode(readBuf));
+ readBuf.clear();
+ }
+ }
+
+ public void startWithCompletionHandler() throws InterruptedException, ExecutionException, TimeoutException {
+ server.accept(null, new CompletionHandler() {
+ public void completed(AsynchronousSocketChannel result, Object attachment) {
+ server.accept(null, this);// 再此接收客户端连接
+ handleWithCompletionHandler(result);
+ }
+
+ @Override
+ public void failed(Throwable exc, Object attachment) {
+ exc.printStackTrace();
+ }
+ });
+ }
+
+ public void handleWithCompletionHandler(final AsynchronousSocketChannel channel) {
+ try {
+ final ByteBuffer buffer = ByteBuffer.allocate(1024);
+ final long timeout = 10L;
+ channel.read(buffer, timeout, TimeUnit.SECONDS, null, new CompletionHandler() {
+ @Override
+ public void completed(Integer result, Object attachment) {
+ System.out.println("read:" + result);
+ if (result == -1) {
+ try {
+ channel.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ return;
+ }
+ buffer.flip();
+ System.out.println("received message:" + Charset.forName("GBK").decode(buffer));
+ buffer.clear();
+ channel.read(buffer, timeout, TimeUnit.SECONDS, null, this);
+ }
+
+ @Override
+ public void failed(Throwable exc, Object attachment) {
+ exc.printStackTrace();
+ }
+ });
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ public static void main(String args[]) throws Exception {
+// new AioServer().startWithFuture();
+ new AioServerTest().startWithCompletionHandler();
+ Thread.sleep(100000);
+ }
+}
+