This commit is contained in:
hyl 2020-07-27 10:32:24 +08:00
parent 28c9801566
commit 7e4640804a
2 changed files with 14 additions and 66 deletions

View File

@ -105,7 +105,6 @@ public abstract class NioClient implements Closeable {
this.executorService = Executors.newSingleThreadExecutor(r -> {
final Thread thread = Executors.defaultThreadFactory().newThread(r);
thread.setName("nio-client-listen");
thread.setDaemon(true);
return thread;
});
this.executorService.execute(() -> {
@ -139,13 +138,6 @@ public abstract class NioClient implements Closeable {
* @param key SelectionKey
*/
private void handle(SelectionKey key) throws IOException {
//连接建立完成
// if (key.isConnectable()) {
// if (this.channel.finishConnect()) {
// this.channel.register(selector, SelectionKey.OP_READ);
// }
// }
// 读事件就绪
if (key.isReadable()) {
final SocketChannel socketChannel = (SocketChannel) key.channel();
@ -161,22 +153,6 @@ public abstract class NioClient implements Closeable {
*/
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>
* 当收到写出准备就绪的信号后回调此方法用户可向客户端发送消息

View File

@ -10,7 +10,9 @@ 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 {
@ -35,7 +37,6 @@ public class NioClientTest {
readBuffer.get(bytes);
String body = new String(bytes, "UTF-8");
System.out.println("the read client receive message: " + body);
doWrite(sc, body);
}else if(readBytes < 0){
sc.close();
}
@ -46,50 +47,21 @@ public class NioClientTest {
}
ByteBuffer buffer = ByteBuffer.wrap("client 发生到 server".getBytes());
client.write(buffer);
if(!buffer.hasRemaining()) {
System.err.println("第一次发送成功...");
}
buffer = ByteBuffer.wrap("client 再次发生到 server".getBytes());
client.write(buffer);
if(!buffer.hasRemaining()) {
System.err.println("第二次发送成功...");
}
}
//发送请求
private static void doWriteRequest(SocketChannel socketChannel) throws Exception{
System.err.println("start connect...");
//创建ByteBuffer对象会放入数据
ByteBuffer byteBuffer = ByteBuffer.allocate("Hello nio.example.Server!".getBytes().length);
byteBuffer.put("Hello nio.example.Server!".getBytes());
byteBuffer.flip();
//写数据
socketChannel.write(byteBuffer);
if(!byteBuffer.hasRemaining()) {
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);
/**
* 在控制台向服务器端发送数据
*/
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));
}
}
}
}