nio enhancement

This commit is contained in:
Looly
2020-08-01 18:58:44 +08:00
parent f527c7af39
commit 04917da6e2
38 changed files with 429 additions and 427 deletions

View File

@@ -1,67 +0,0 @@
package cn.hutool.socket;
import cn.hutool.core.util.StrUtil;
import cn.hutool.socket.nio.NioClient;
import lombok.SneakyThrows;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;
public class NioClientTest {
@SneakyThrows
public static void main(String[] args) {
NioClient client = new NioClient("127.0.0.1", 8080) {
@SneakyThrows
@Override
protected void read(SocketChannel 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();
//eturns the number of elements between the current position and the limit.
// 要读取的字节长度
byte[] bytes = new byte[readBuffer.remaining()];
//将缓冲区的数据读到bytes数组
readBuffer.get(bytes);
String body = new String(bytes, "UTF-8");
System.out.println("the read client receive message: " + body);
}else if(readBytes < 0){
sc.close();
}
}
};
if (client.waitConnect()) {
client.listen();
}
ByteBuffer buffer = ByteBuffer.wrap("client 发生到 server".getBytes());
client.write(buffer);
buffer = ByteBuffer.wrap("client 再次发生到 server".getBytes());
client.write(buffer);
/**
* 在控制台向服务器端发送数据
*/
System.out.println("请在下方畅所欲言");
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextLine()) {
String request = scanner.nextLine();
if (request != null && request.trim().length() > 0) {
client.write(
Charset.forName("UTF-8")
.encode("测试client" + ": " + request));
}
}
}
}

View File

@@ -1,82 +0,0 @@
package cn.hutool.socket;
import cn.hutool.core.util.StrUtil;
import cn.hutool.socket.nio.NioServer;
import lombok.SneakyThrows;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Set;
public class NioServerTest {
public static void main(String[] args) {
NioServer server = new NioServer(8080) {
@SneakyThrows
@Override
protected void read(SocketChannel 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();
//eturns the number of elements between the current position and the limit.
// 要读取的字节长度
byte[] bytes = new byte[readBuffer.remaining()];
//将缓冲区的数据读到bytes数组
readBuffer.get(bytes);
String body = new String(bytes, "UTF-8");
System.out.println("the read server receive message: " + body);
doWrite(sc, body);
}else if(readBytes < 0){
sc.close();
}
}
@SneakyThrows
@Override
protected void write(SocketChannel 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();
//eturns the number of elements between the current position and the limit.
// 要读取的字节长度
byte[] bytes = new byte[readBuffer.remaining()];
//将缓冲区的数据读到bytes数组
readBuffer.get(bytes);
String body = new String(bytes, "UTF-8");
System.out.println("the write server receive message: " + body);
doWrite(sc, body);
}else if(readBytes < 0){
sc.close();
}
}
};
server.listen();
}
public static void doWrite(SocketChannel channel, String response) throws IOException {
response = "我们已收到消息:"+response;
if(!StrUtil.isBlank(response)){
byte [] bytes = response.getBytes();
//分配一个bytes的length长度的ByteBuffer
ByteBuffer write = ByteBuffer.allocate(bytes.length);
//将返回数据写入缓冲区
write.put(bytes);
write.flip();
//将缓冲数据写入渠道,返回给客户端
channel.write(write);
}
}
}

View File

@@ -1,13 +1,10 @@
package cn.hutool.socket;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
package cn.hutool.socket.aio;
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;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
public class AioClientTest {
public static void main(String[] args) {

View File

@@ -1,15 +1,12 @@
package cn.hutool.socket;
import java.nio.ByteBuffer;
package cn.hutool.socket.aio;
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;
import java.nio.ByteBuffer;
public class AioServerTest {

View File

@@ -0,0 +1,59 @@
package cn.hutool.socket.nio;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import lombok.SneakyThrows;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.Scanner;
public class NioClientTest {
@SneakyThrows
public static void main(String[] args) {
NioClient client = new NioClient("127.0.0.1", 8080) {
@SneakyThrows
@Override
protected void read(SocketChannel 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("the read client receive message: " + body);
} else if (readBytes < 0) {
sc.close();
}
}
};
client.listen();
ByteBuffer buffer = ByteBuffer.wrap("client 发生到 server".getBytes());
client.write(buffer);
buffer = ByteBuffer.wrap("client 再次发生到 server".getBytes());
client.write(buffer);
// 在控制台向服务器端发送数据
Console.log("请在下方畅所欲言");
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextLine()) {
String request = scanner.nextLine();
if (request != null && request.trim().length() > 0) {
client.write(
CharsetUtil.CHARSET_UTF_8
.encode("测试client" + ": " + request));
}
}
}
}

View File

@@ -0,0 +1,56 @@
package cn.hutool.socket.nio;
import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.StrUtil;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
public class NioServerTest {
public static void main(String[] args) {
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("the read server receive message: " + 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;
if (!StrUtil.isBlank(response)) {
byte[] bytes = response.getBytes();
//分配一个bytes的length长度的ByteBuffer
ByteBuffer write = ByteBuffer.allocate(bytes.length);
//将返回数据写入缓冲区
write.put(bytes);
write.flip();
//将缓冲数据写入渠道,返回给客户端
channel.write(write);
}
}
}