mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-08-18 20:38:02 +08:00
fix code
This commit is contained in:
@@ -7,11 +7,11 @@ import java.net.InetSocketAddress;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
public class AioClientTest {
|
||||
public static void main(String[] args) {
|
||||
AioClient client = new AioClient(new InetSocketAddress("localhost", 8899), new SimpleIoAction() {
|
||||
public static void main(final String[] args) {
|
||||
final AioClient client = new AioClient(new InetSocketAddress("localhost", 8899), new SimpleIoAction() {
|
||||
|
||||
@Override
|
||||
public void doAction(AioSession session, ByteBuffer data) {
|
||||
public void doAction(final AioSession session, final ByteBuffer data) {
|
||||
if(data.hasRemaining()) {
|
||||
Console.log(StrUtil.utf8Str(data));
|
||||
session.read();
|
||||
|
||||
@@ -10,24 +10,23 @@ import java.nio.ByteBuffer;
|
||||
|
||||
public class AioServerTest {
|
||||
|
||||
public static void main(String[] args) {
|
||||
public static void main(final String[] args) {
|
||||
|
||||
@SuppressWarnings("resource")
|
||||
AioServer aioServer = new AioServer(8899);
|
||||
@SuppressWarnings("resource") final AioServer aioServer = new AioServer(8899);
|
||||
aioServer.setIoAction(new SimpleIoAction() {
|
||||
|
||||
@Override
|
||||
public void accept(AioSession session) {
|
||||
public void accept(final AioSession session) {
|
||||
StaticLog.debug("【客户端】:{} 连接。", session.getRemoteAddress());
|
||||
session.write(BufferUtil.createUtf8("=== Welcome to Hutool socket server. ==="));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doAction(AioSession session, ByteBuffer data) {
|
||||
public void doAction(final AioSession session, final ByteBuffer data) {
|
||||
Console.log(data);
|
||||
|
||||
if(false == data.hasRemaining()) {
|
||||
StringBuilder response = StrUtil.builder()//
|
||||
final 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")//
|
||||
|
||||
@@ -11,22 +11,22 @@ import java.util.Scanner;
|
||||
public class NioClientTest {
|
||||
|
||||
@SneakyThrows
|
||||
public static void main(String[] args) {
|
||||
NioClient client = new NioClient("127.0.0.1", 8080);
|
||||
public static void main(final String[] args) {
|
||||
final NioClient client = new NioClient("127.0.0.1", 8080);
|
||||
client.setChannelHandler((sc)->{
|
||||
ByteBuffer readBuffer = ByteBuffer.allocate(1024);
|
||||
final ByteBuffer readBuffer = ByteBuffer.allocate(1024);
|
||||
//从channel读数据到缓冲区
|
||||
int readBytes = sc.read(readBuffer);
|
||||
final 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()];
|
||||
final byte[] bytes = new byte[readBuffer.remaining()];
|
||||
//将缓冲区的数据读到bytes数组
|
||||
readBuffer.get(bytes);
|
||||
String body = StrUtil.utf8Str(bytes);
|
||||
final String body = StrUtil.utf8Str(bytes);
|
||||
Console.log("[{}]: {}", sc.getRemoteAddress(), body);
|
||||
} else if (readBytes < 0) {
|
||||
sc.close();
|
||||
@@ -39,9 +39,9 @@ public class NioClientTest {
|
||||
|
||||
// 在控制台向服务器端发送数据
|
||||
Console.log("请输入发送的消息:");
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
final Scanner scanner = new Scanner(System.in);
|
||||
while (scanner.hasNextLine()) {
|
||||
String request = scanner.nextLine();
|
||||
final String request = scanner.nextLine();
|
||||
if (request != null && request.trim().length() > 0) {
|
||||
client.write(BufferUtil.createUtf8(request));
|
||||
}
|
||||
|
||||
@@ -12,37 +12,37 @@ import java.nio.channels.SocketChannel;
|
||||
|
||||
public class NioServerTest {
|
||||
|
||||
public static void main(String[] args) {
|
||||
NioServer server = new NioServer(8080);
|
||||
public static void main(final String[] args) {
|
||||
final NioServer server = new NioServer(8080);
|
||||
server.setChannelHandler((sc)->{
|
||||
ByteBuffer readBuffer = ByteBuffer.allocate(1024);
|
||||
final ByteBuffer readBuffer = ByteBuffer.allocate(1024);
|
||||
try{
|
||||
//从channel读数据到缓冲区
|
||||
int readBytes = sc.read(readBuffer);
|
||||
final 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()];
|
||||
final byte[] bytes = new byte[readBuffer.remaining()];
|
||||
//将缓冲区的数据读到bytes数组
|
||||
readBuffer.get(bytes);
|
||||
String body = StrUtil.utf8Str(bytes);
|
||||
final String body = StrUtil.utf8Str(bytes);
|
||||
Console.log("[{}]: {}", sc.getRemoteAddress(), body);
|
||||
|
||||
doWrite(sc, body);
|
||||
} else if (readBytes < 0) {
|
||||
IoUtil.close(sc);
|
||||
}
|
||||
} catch (IOException e){
|
||||
} catch (final IOException e){
|
||||
throw new IORuntimeException(e);
|
||||
}
|
||||
});
|
||||
server.listen();
|
||||
}
|
||||
|
||||
public static void doWrite(SocketChannel channel, String response) throws IOException {
|
||||
public static void doWrite(final SocketChannel channel, String response) throws IOException {
|
||||
response = "收到消息:" + response;
|
||||
//将缓冲数据写入渠道,返回给客户端
|
||||
channel.write(BufferUtil.createUtf8(response));
|
||||
|
||||
Reference in New Issue
Block a user