mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
clean history
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
package cn.hutool.socket;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.socket.aio.AioClient;
|
||||
import cn.hutool.socket.aio.AioSession;
|
||||
import cn.hutool.socket.aio.SimpleIoAction;
|
||||
|
||||
public class AioClientTest {
|
||||
public static void main(String[] args) {
|
||||
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();
|
||||
}
|
||||
}
|
@@ -0,0 +1,45 @@
|
||||
package cn.hutool.socket;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.io.BufferUtil;
|
||||
import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.log.StaticLog;
|
||||
import cn.hutool.socket.aio.AioServer;
|
||||
import cn.hutool.socket.aio.AioSession;
|
||||
import cn.hutool.socket.aio.SimpleIoAction;
|
||||
|
||||
public class AioServerTest {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user