From 4378a91bedf10d5f60ba29da47a69d1ca2b36f16 Mon Sep 17 00:00:00 2001 From: Looly Date: Tue, 15 Sep 2020 15:26:32 +0800 Subject: [PATCH] fix content --- docs/SUMMARY.md | 4 + ...243\205-AioServer\345\222\214AioClient.md" | 57 +++++++++++++ ...243\205-NioServer\345\222\214NioClient.md" | 81 +++++++++++++++++++ "docs/socket/\346\246\202\350\277\260.md" | 27 +++++++ index.html | 2 +- 5 files changed, 170 insertions(+), 1 deletion(-) create mode 100644 "docs/socket/AIO\345\260\201\350\243\205-AioServer\345\222\214AioClient.md" create mode 100644 "docs/socket/NIO\345\260\201\350\243\205-NioServer\345\222\214NioClient.md" create mode 100644 "docs/socket/\346\246\202\350\277\260.md" diff --git a/docs/SUMMARY.md b/docs/SUMMARY.md index 25f233bd..f26a4a41 100755 --- a/docs/SUMMARY.md +++ b/docs/SUMMARY.md @@ -217,5 +217,9 @@ * [系统属性调用-SystemUtil](system/系统属性调用-SystemUtil.md) * 图形验证码(Hutool-captcha) * [概述](captcha/概述.md) +* 网络Socket(Hutool-socket) + * [概述](socket/概述.md) + * [NIO封装-NioServer和NioClient](socket/NIO封装-NioServer和NioClient.md) + * [AIO封装-AioServer和AioClient](socket/AIO封装-AioServer和AioClient.md) * [Hutool的朋友们](Hutool的朋友们.md) * [捐赠使用公开](捐赠使用公开.md) \ No newline at end of file diff --git "a/docs/socket/AIO\345\260\201\350\243\205-AioServer\345\222\214AioClient.md" "b/docs/socket/AIO\345\260\201\350\243\205-AioServer\345\222\214AioClient.md" new file mode 100644 index 00000000..265a0e23 --- /dev/null +++ "b/docs/socket/AIO\345\260\201\350\243\205-AioServer\345\222\214AioClient.md" @@ -0,0 +1,57 @@ +## 由来 + +在JDK7+后,提供了异步Socket库——AIO,Hutool对其进行了简单的封装。 + +## 使用 + +### 服务端 + +```java +AioServer aioServer = new AioServer(8899); +aioServer.setIoAction(new SimpleIoAction() { + + @Override + public void accept(AioSession session) { + StaticLog.debug("【客户端】:{} 连接。", session.getRemoteAddress()); + session.write(BufferUtil.createUtf8("=== Welcome to Hutool socket server. ===")); + } + + @Override + public void doAction(AioSession session, ByteBuffer data) { + Console.log(data); + + if(false == data.hasRemaining()) { + StringBuilder response = StrUtil.builder()// + .append("HTTP/1.1 200 OK\r\n")// + .append("Date: ").append(DateUtil.formatHttpDate(DateUtil.date())).append("\r\n")// + .append("Content-Type: text/html; charset=UTF-8\r\n")// + .append("\r\n") + .append("Hello Hutool socket");// + session.writeAndClose(BufferUtil.createUtf8(response)); + }else { + session.read(); + } + } +}).start(true); +``` + +### 客户端 + +```java +AioClient client = new AioClient(new InetSocketAddress("localhost", 8899), new SimpleIoAction() { + + @Override + public void doAction(AioSession session, ByteBuffer data) { + if(data.hasRemaining()) { + Console.log(StrUtil.utf8Str(data)); + session.read(); + } + Console.log("OK"); + } +}); + +client.write(ByteBuffer.wrap("Hello".getBytes())); +client.read(); + +client.close(); +``` \ No newline at end of file diff --git "a/docs/socket/NIO\345\260\201\350\243\205-NioServer\345\222\214NioClient.md" "b/docs/socket/NIO\345\260\201\350\243\205-NioServer\345\222\214NioClient.md" new file mode 100644 index 00000000..34a1ec9d --- /dev/null +++ "b/docs/socket/NIO\345\260\201\350\243\205-NioServer\345\222\214NioClient.md" @@ -0,0 +1,81 @@ +## 由来 + +Hutool对NIO其进行了简单的封装。 + +## 使用 + +### 服务端 + +```java +NioServer server = new NioServer(8080); +server.setChannelHandler((sc)->{ + ByteBuffer readBuffer = ByteBuffer.allocate(1024); + try{ + //从channel读数据到缓冲区 + int readBytes = sc.read(readBuffer); + if (readBytes > 0) { + //Flips this buffer. The limit is set to the current position and then + // the position is set to zero,就是表示要从起始位置开始读取数据 + readBuffer.flip(); + //eturns the number of elements between the current position and the limit. + // 要读取的字节长度 + byte[] bytes = new byte[readBuffer.remaining()]; + //将缓冲区的数据读到bytes数组 + readBuffer.get(bytes); + String body = StrUtil.utf8Str(bytes); + Console.log("[{}]: {}", sc.getRemoteAddress(), body); + doWrite(sc, body); + } else if (readBytes < 0) { + IoUtil.close(sc); + } + } catch (IOException e){ + throw new IORuntimeException(e); + } +}); +server.listen(); +``` + +```java +public static void doWrite(SocketChannel channel, String response) throws IOException { + response = "收到消息:" + response; + //将缓冲数据写入渠道,返回给客户端 + channel.write(BufferUtil.createUtf8(response)); +} +``` + +### 客户端 + +```java +NioClient client = new NioClient("127.0.0.1", 8080); +client.setChannelHandler((sc)->{ + ByteBuffer readBuffer = ByteBuffer.allocate(1024); + //从channel读数据到缓冲区 + int readBytes = sc.read(readBuffer); + if (readBytes > 0) { + //Flips this buffer. The limit is set to the current position and then + // the position is set to zero,就是表示要从起始位置开始读取数据 + readBuffer.flip(); + //returns the number of elements between the current position and the limit. + // 要读取的字节长度 + byte[] bytes = new byte[readBuffer.remaining()]; + //将缓冲区的数据读到bytes数组 + readBuffer.get(bytes); + String body = StrUtil.utf8Str(bytes); + Console.log("[{}]: {}", sc.getRemoteAddress(), body); + } else if (readBytes < 0) { + sc.close(); + } +}); +client.listen(); +client.write(BufferUtil.createUtf8("你好。\n")); +client.write(BufferUtil.createUtf8("你好2。")); +// 在控制台向服务器端发送数据 +Console.log("请输入发送的消息:"); +Scanner scanner = new Scanner(System.in); +while (scanner.hasNextLine()) { + String request = scanner.nextLine(); + if (request != null && request.trim().length() > 0) { + client.write(BufferUtil.createUtf8(request)); + } +} +``` \ No newline at end of file diff --git "a/docs/socket/\346\246\202\350\277\260.md" "b/docs/socket/\346\246\202\350\277\260.md" new file mode 100644 index 00000000..2bdd40b2 --- /dev/null +++ "b/docs/socket/\346\246\202\350\277\260.md" @@ -0,0 +1,27 @@ +## 模块介绍 + +### Socket介绍 + +介绍来自:https://www.liaoxuefeng.com/wiki/1252599548343744/1305207629676577 + +在开发网络应用程序的时候,我们又会遇到Socket这个概念。Socket是一个抽象概念,一个应用程序通过一个Socket来建立一个远程连接,而Socket内部通过TCP/IP协议把数据传输到网络: + +### Hutool封装 + +JDK中提供了Socket功能,包括: + +- BIO +- NIO +- AIO + +Hutool只针对NIO和AIO做了简单的封装,用于简化Socket异步开发。 + +> 现阶段,Hutool的socket封装依旧不是一个完整框架或者高效的工具类,不能提供完整的高性能IO功能,因此推荐更加专业的Socket库。例如: + +> [t-io](https://www.t-io.org/) + +> [Voovan](http://www.voovan.org/) + +> [Netty](https://netty.io/) + +> [Mina](http://mina.apache.org/) \ No newline at end of file diff --git a/index.html b/index.html index 2f39dc03..beff8b61 100755 --- a/index.html +++ b/index.html @@ -60,7 +60,7 @@
-
+