Hutool Hutool
(opens new window)
🏡首页
📖指南
  • 核心(Hutool-core)
  • 配置文件(Hutool-setting)
  • 日志(Hutool-log)
  • 缓存(Hutool-cache)
  • JSON(Hutool-json)
  • 加密解密(Hutool-crypto)
  • DFA查找(Hutool-dfa)
  • 数据库(Hutool-db)
  • HTTP客户端(Hutool-http)
  • 定时任务(Hutool-cron)
  • 扩展(Hutool-extra)
  • 布隆过滤(Hutool-bloomFilter)
  • 切面(Hutool-aop)
  • 脚本(Hutool-script)
  • Office文档操作(Hutool-poi)
  • 系统调用(Hutool-system)
  • 图形验证码(Hutool-captcha)
  • 网络Socket(Hutool-socket)
  • JWT(Hutool-jwt)
💡javaDoc (opens new window)
⏳更新记录 (opens new window)
  • 🍎gitee (opens new window)
  • 🍏github (opens new window)
(opens new window)
🏡首页
📖指南
  • 核心(Hutool-core)
  • 配置文件(Hutool-setting)
  • 日志(Hutool-log)
  • 缓存(Hutool-cache)
  • JSON(Hutool-json)
  • 加密解密(Hutool-crypto)
  • DFA查找(Hutool-dfa)
  • 数据库(Hutool-db)
  • HTTP客户端(Hutool-http)
  • 定时任务(Hutool-cron)
  • 扩展(Hutool-extra)
  • 布隆过滤(Hutool-bloomFilter)
  • 切面(Hutool-aop)
  • 脚本(Hutool-script)
  • Office文档操作(Hutool-poi)
  • 系统调用(Hutool-system)
  • 图形验证码(Hutool-captcha)
  • 网络Socket(Hutool-socket)
  • JWT(Hutool-jwt)
💡javaDoc (opens new window)
⏳更新记录 (opens new window)
  • 🍎gitee (opens new window)
  • 🍏github (opens new window)
  • 快速入门

  • 核心(Hutool-core)

  • 配置文件(Hutool-setting)

  • 日志(Hutool-log)

  • 缓存(Hutool-cache)

  • JSON(Hutool-json)

  • 加密解密(Hutool-crypto)

  • DFA查找(Hutool-dfa)

  • 数据库(Hutool-db)

  • HTTP客户端(Hutool-http)

  • 定时任务(Hutool-cron)

  • 扩展(Hutool-extra)

  • 布隆过滤(Hutool-bloomFilter)

  • 切面(Hutool-aop)

  • 脚本(Hutool-script)

  • Office文档操作(Hutool-poi)

  • 系统调用(Hutool-system)

  • 图形验证码(Hutool-captcha)

  • 网络Socket(Hutool-socket)

    • 概述
    • AIO封装-AioServer和AioClient
    • NIO封装-NioServer和NioClient
      • 由来
      • 使用
        • 服务端
        • 客户端
  • JWT(Hutool-jwt)

  • 指南
  • 网络Socket(Hutool-socket)
Hutool
2023-03-28
目录

NIO封装-NioServer和NioClient

特别赞助 by:

# 由来

Hutool对NIO其进行了简单的封装。

# 使用

# 服务端

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();
public static void doWrite(SocketChannel channel, String response) throws IOException {
	response = "收到消息:" + response;
	//将缓冲数据写入渠道,返回给客户端
	channel.write(BufferUtil.createUtf8(response));
}

# 客户端

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));
	}
}
上次更新: 2025/05/06, 10:48:51
AIO封装-AioServer和AioClient
概述

← AIO封装-AioServer和AioClient 概述→

Theme by Vdoing | Copyright © 2023-2025 Hutool | MulanPSL-2.0
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
×