add ChannelUtil

This commit is contained in:
Looly
2022-05-23 11:32:50 +08:00
parent 2953b27dbc
commit 5698f1ecd4
4 changed files with 77 additions and 29 deletions

View File

@@ -1,15 +1,23 @@
package cn.hutool.socket.aio;
import cn.hutool.core.lang.Console;
import cn.hutool.core.thread.ThreadFactoryBuilder;
import cn.hutool.core.util.StrUtil;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousChannelGroup;
public class AioClientTest {
public static void main(String[] args) {
public static void main(String[] args) throws IOException {
final AsynchronousChannelGroup GROUP = AsynchronousChannelGroup.withFixedThreadPool(//
Runtime.getRuntime().availableProcessors(), // 默认线程池大小
ThreadFactoryBuilder.create().setNamePrefix("Huool-socket-").build()//
);
AioClient client = new AioClient(new InetSocketAddress("localhost", 8899), new SimpleIoAction() {
@Override
public void doAction(AioSession session, ByteBuffer data) {
if(data.hasRemaining()) {
@@ -19,10 +27,10 @@ public class AioClientTest {
Console.log("OK");
}
});
client.write(ByteBuffer.wrap("Hello".getBytes()));
client.read();
client.close();
}
}