mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-05 17:37:59 +08:00
commit
090b2e1706
@ -14,7 +14,7 @@ public class AioClientTest {
|
|||||||
public static void main(String[] args) throws IOException {
|
public static void main(String[] args) throws IOException {
|
||||||
final AsynchronousChannelGroup GROUP = AsynchronousChannelGroup.withFixedThreadPool(//
|
final AsynchronousChannelGroup GROUP = AsynchronousChannelGroup.withFixedThreadPool(//
|
||||||
RuntimeUtil.getProcessorCount(), // 默认线程池大小
|
RuntimeUtil.getProcessorCount(), // 默认线程池大小
|
||||||
ThreadFactoryBuilder.create().setNamePrefix("Huool-socket-").build()//
|
ThreadFactoryBuilder.create().setNamePrefix("Hutool-socket-").build()//
|
||||||
);
|
);
|
||||||
|
|
||||||
AioClient client = new AioClient(new InetSocketAddress("localhost", 8899), new SimpleIoAction() {
|
AioClient client = new AioClient(new InetSocketAddress("localhost", 8899), new SimpleIoAction() {
|
||||||
|
@ -17,16 +17,16 @@ public class NioServerTest {
|
|||||||
server.setChannelHandler((sc)->{
|
server.setChannelHandler((sc)->{
|
||||||
ByteBuffer readBuffer = ByteBuffer.allocate(1024);
|
ByteBuffer readBuffer = ByteBuffer.allocate(1024);
|
||||||
try{
|
try{
|
||||||
//从channel读数据到缓冲区
|
// 从channel读数据到缓冲区
|
||||||
int readBytes = sc.read(readBuffer);
|
int readBytes = sc.read(readBuffer);
|
||||||
if (readBytes > 0) {
|
if (readBytes > 0) {
|
||||||
//Flips this buffer. The limit is set to the current position and then
|
// Flips this buffer. The limit is set to the current position and then
|
||||||
// the position is set to zero,就是表示要从起始位置开始读取数据
|
// the position is set to zero,就是表示要从起始位置开始读取数据
|
||||||
readBuffer.flip();
|
readBuffer.flip();
|
||||||
//eturns the number of elements between the current position and the limit.
|
// returns the number of elements between the current position and the limit.
|
||||||
// 要读取的字节长度
|
// 要读取的字节长度
|
||||||
byte[] bytes = new byte[readBuffer.remaining()];
|
byte[] bytes = new byte[readBuffer.remaining()];
|
||||||
//将缓冲区的数据读到bytes数组
|
// 将缓冲区的数据读到bytes数组
|
||||||
readBuffer.get(bytes);
|
readBuffer.get(bytes);
|
||||||
String body = StrUtil.utf8Str(bytes);
|
String body = StrUtil.utf8Str(bytes);
|
||||||
Console.log("[{}]: {}", sc.getRemoteAddress(), body);
|
Console.log("[{}]: {}", sc.getRemoteAddress(), body);
|
||||||
@ -44,7 +44,7 @@ public class NioServerTest {
|
|||||||
|
|
||||||
public static void doWrite(SocketChannel channel, String response) throws IOException {
|
public static void doWrite(SocketChannel channel, String response) throws IOException {
|
||||||
response = "收到消息:" + response;
|
response = "收到消息:" + response;
|
||||||
//将缓冲数据写入渠道,返回给客户端
|
// 将缓冲数据写入渠道,返回给客户端
|
||||||
channel.write(BufferUtil.createUtf8(response));
|
channel.write(BufferUtil.createUtf8(response));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user