mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
nio
This commit is contained in:
parent
28c9801566
commit
7e4640804a
@ -105,7 +105,6 @@ public abstract class NioClient implements Closeable {
|
|||||||
this.executorService = Executors.newSingleThreadExecutor(r -> {
|
this.executorService = Executors.newSingleThreadExecutor(r -> {
|
||||||
final Thread thread = Executors.defaultThreadFactory().newThread(r);
|
final Thread thread = Executors.defaultThreadFactory().newThread(r);
|
||||||
thread.setName("nio-client-listen");
|
thread.setName("nio-client-listen");
|
||||||
thread.setDaemon(true);
|
|
||||||
return thread;
|
return thread;
|
||||||
});
|
});
|
||||||
this.executorService.execute(() -> {
|
this.executorService.execute(() -> {
|
||||||
@ -139,13 +138,6 @@ public abstract class NioClient implements Closeable {
|
|||||||
* @param key SelectionKey
|
* @param key SelectionKey
|
||||||
*/
|
*/
|
||||||
private void handle(SelectionKey key) throws IOException {
|
private void handle(SelectionKey key) throws IOException {
|
||||||
//连接建立完成
|
|
||||||
// if (key.isConnectable()) {
|
|
||||||
// if (this.channel.finishConnect()) {
|
|
||||||
// this.channel.register(selector, SelectionKey.OP_READ);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// 读事件就绪
|
// 读事件就绪
|
||||||
if (key.isReadable()) {
|
if (key.isReadable()) {
|
||||||
final SocketChannel socketChannel = (SocketChannel) key.channel();
|
final SocketChannel socketChannel = (SocketChannel) key.channel();
|
||||||
@ -161,22 +153,6 @@ public abstract class NioClient implements Closeable {
|
|||||||
*/
|
*/
|
||||||
protected abstract void read(SocketChannel socketChannel);
|
protected abstract void read(SocketChannel socketChannel);
|
||||||
|
|
||||||
/**
|
|
||||||
* 处理读事件<br>
|
|
||||||
* 当收到读取准备就绪的信号后,回调此方法,用户可读取从客户端传世来的消息
|
|
||||||
*
|
|
||||||
* @param buffer 服务端数据存储缓存
|
|
||||||
* @return this
|
|
||||||
*/
|
|
||||||
public NioClient read(ByteBuffer buffer) {
|
|
||||||
try {
|
|
||||||
this.channel.read(buffer);
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new IORuntimeException(e);
|
|
||||||
}
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 实现写逻辑<br>
|
* 实现写逻辑<br>
|
||||||
* 当收到写出准备就绪的信号后,回调此方法,用户可向客户端发送消息
|
* 当收到写出准备就绪的信号后,回调此方法,用户可向客户端发送消息
|
||||||
|
@ -10,7 +10,9 @@ import java.nio.ByteBuffer;
|
|||||||
import java.nio.channels.SelectionKey;
|
import java.nio.channels.SelectionKey;
|
||||||
import java.nio.channels.Selector;
|
import java.nio.channels.Selector;
|
||||||
import java.nio.channels.SocketChannel;
|
import java.nio.channels.SocketChannel;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
import java.util.Scanner;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
public class NioClientTest {
|
public class NioClientTest {
|
||||||
@ -35,7 +37,6 @@ public class NioClientTest {
|
|||||||
readBuffer.get(bytes);
|
readBuffer.get(bytes);
|
||||||
String body = new String(bytes, "UTF-8");
|
String body = new String(bytes, "UTF-8");
|
||||||
System.out.println("the read client receive message: " + body);
|
System.out.println("the read client receive message: " + body);
|
||||||
doWrite(sc, body);
|
|
||||||
}else if(readBytes < 0){
|
}else if(readBytes < 0){
|
||||||
sc.close();
|
sc.close();
|
||||||
}
|
}
|
||||||
@ -46,50 +47,21 @@ public class NioClientTest {
|
|||||||
}
|
}
|
||||||
ByteBuffer buffer = ByteBuffer.wrap("client 发生到 server".getBytes());
|
ByteBuffer buffer = ByteBuffer.wrap("client 发生到 server".getBytes());
|
||||||
client.write(buffer);
|
client.write(buffer);
|
||||||
if(!buffer.hasRemaining()) {
|
|
||||||
System.err.println("第一次发送成功...");
|
|
||||||
}
|
|
||||||
buffer = ByteBuffer.wrap("client 再次发生到 server".getBytes());
|
buffer = ByteBuffer.wrap("client 再次发生到 server".getBytes());
|
||||||
client.write(buffer);
|
client.write(buffer);
|
||||||
if(!buffer.hasRemaining()) {
|
|
||||||
System.err.println("第二次发送成功...");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//发送请求
|
/**
|
||||||
private static void doWriteRequest(SocketChannel socketChannel) throws Exception{
|
* 在控制台向服务器端发送数据
|
||||||
System.err.println("start connect...");
|
*/
|
||||||
|
System.out.println("请在下方畅所欲言");
|
||||||
//创建ByteBuffer对象,会放入数据
|
Scanner scanner = new Scanner(System.in);
|
||||||
ByteBuffer byteBuffer = ByteBuffer.allocate("Hello nio.example.Server!".getBytes().length);
|
while (scanner.hasNextLine()) {
|
||||||
byteBuffer.put("Hello nio.example.Server!".getBytes());
|
String request = scanner.nextLine();
|
||||||
byteBuffer.flip();
|
if (request != null && request.trim().length() > 0) {
|
||||||
//写数据
|
client.write(
|
||||||
socketChannel.write(byteBuffer);
|
Charset.forName("UTF-8")
|
||||||
if(!byteBuffer.hasRemaining()) {
|
.encode("测试client" + ": " + request));
|
||||||
System.err.println("Send request success...");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//读取服务端的响应
|
|
||||||
private static void doRead(SelectionKey selectionKey) throws Exception{
|
|
||||||
SocketChannel socketChannel = ((SocketChannel) selectionKey.channel());
|
|
||||||
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
|
|
||||||
int len = socketChannel.read(byteBuffer);
|
|
||||||
System.out.println("Recv:" + new String(byteBuffer.array(), 0 ,len));
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user