NIO阻塞模式
图示简单理解
代码
Server
package com.example;
import lombok.extern.slf4j.Slf4j;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.ArrayList;
import java.util.List;
import static com.example.util.ByteBufferUtil.debugRead;
@Slf4j
public class Server {
public static void main(String[] args) throws IOException {
// 使用nio来理解 【阻塞模式】
// 0.ByteBuffer
ByteBuffer buffer = ByteBuffer.allocate(16);
// 1.Opens a server-socket channel
ServerSocketChannel ssc = ServerSocketChannel.open();
// 2.绑定监听端口
ssc.bind(new InetSocketAddress(8080));
// 3.连接集合
List<SocketChannel> channels = new ArrayList<>();
while(true){
// 4.accept 建立与客户端的连接 socketChannel 用来和客户端之间通信
log.debug("connecting...");
SocketChannel sc = ssc.accept();
log.debug("connected...{}",sc);
channels.add(sc);
for(SocketChannel channel : channels){
// 5.接受客户端发送的数据
log.debug("before read... {}",channel);
channel.read(buffer);
buffer.flip();
debugRead(buffer);
buffer.clear();
log.debug("after read ...{}", channel);
}
}
}
}
client
package com.example;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SocketChannel;
public class Client {
public static void main(String[] args) throws IOException {
SocketChannel sc = SocketChannel.open();
sc.connect(new InetSocketAddress("localhost",8080));
System.out.println("waiting...");
}
}
说明
ssc.accept()
是阻塞方法
服务启动后,没有客户端的连接,线程在执行ssc.accept()时阻塞。
22:53:50.620 [main] DEBUG com.example.Server - connecting...
-
channel.read(buffer)
是阻塞方法客户端client启动后,
sc.connect(new InetSocketAddress("localhost",8080))
客户端的socketChannel连接到服务端的socketChannel。ssc.accept()继续执行,客户端没有向buffer中写数据,read方法阻塞。
22:53:50.620 [main] DEBUG com.example.Server - connecting...
22:59:51.538 [main] DEBUG com.example.Server - connected...java.nio.channels.SocketChannel[connected local=/127.0.0.1:8080 remote=/127.0.0.1:50941]
22:59:51.539 [main] DEBUG com.example.Server - before read... java.nio.channels.SocketChannel[connected local=/127.0.0.1:8080 remote=/127.0.0.1:50941]
-
客户端发送数据
客户端向buffer中写数据,
channel.read(buffer);
服务端将channel中的数据读取到buffer中。
sc.write(Charset.defaultCharset().encode("hello!"))
-
服务端打印
打印后继续卡在
ssc.accept()
方法上,等待新的连接。
22:53:50.620 [main] DEBUG com.example.Server - connecting...
22:59:51.538 [main] DEBUG com.example.Server - connected...java.nio.channels.SocketChannel[connected local=/127.0.0.1:8080 remote=/127.0.0.1:50941]
22:59:51.539 [main] DEBUG com.example.Server - before read... java.nio.channels.SocketChannel[connected local=/127.0.0.1:8080 remote=/127.0.0.1:50941]
23:04:13.680 [main] DEBUG io.netty.util.internal.logging.InternalLoggerFactory - Using SLF4J as the default logging framework
+--------+-------------------- read -----------------------+----------------+
position: [0], limit: [6]
+-------------------------------------------------+
| 0 1 2 3 4 5 6 7 8 9 a b c d e f |
+--------+-------------------------------------------------+----------------+
|00000000| 68 65 6c 6c 6f 21 |hello! |
+--------+-------------------------------------------------+----------------+
23:04:13.693 [main] DEBUG com.example.Server - after read ...java.nio.channels.SocketChannel[connected local=/127.0.0.1:8080 remote=/127.0.0.1:50941]
23:04:13.693 [main] DEBUG com.example.Server - connecting...
-
启动第二个客户端
新的客户端端口为51608。
23:04:13.693 [main] DEBUG com.example.Server - after read ...java.nio.channels.SocketChannel[connected local=/127.0.0.1:8080 remote=/127.0.0.1:50941]
23:04:13.693 [main] DEBUG com.example.Server - connecting...
23:41:23.092 [main] DEBUG com.example.Server - connected...java.nio.channels.SocketChannel[connected local=/127.0.0.1:8080 remote=/127.0.0.1:51608]
23:41:23.092 [main] DEBUG com.example.Server - before read... java.nio.channels.SocketChannel[connected local=/127.0.0.1:8080 remote=/127.0.0.1:50941]