IO與NIO的主要區別
Java NIO系統的核心在於:通道和緩衝區。
通道的可以理解爲:打開到IO設備(文件、套接字)的連接。可以想象成Channel負責傳輸,Buffer負責存儲。
緩衝區是一個用於特定基本數據類型的容器,所有緩衝區都是Buffer抽象類的子類。
Buffer主要用於與NIO通道交互,數據的讀寫都必須通過緩衝區。
Buffer中有一些參數如下圖所示:
緩衝區的數據操作:
Channel接口的主要實現類:
FileChannel:用於讀寫、映射和操作文件的通道。
DatagramChannel:通過UDP讀寫網絡中的數據通道。
SocketChannel:通過tcp讀寫網絡中的數據。
ServerSocletChannel:可以監聽新來的TCP連接,對每一個新進來的連接都會創建一個SocketChannel。
獲取通道:
關於通道的數據傳輸,我上一篇已經寫得比較清楚了,大家可以翻翻上一篇博客。
下面主要寫一下NIO的非阻塞網絡通信
阻塞與非阻塞:
選擇器:
SelectionKey:
差不多就這些,下面來直接看一個客戶端與服務端通信的demo:
// 客戶端
@Test
public void client() throws IOException {
// 1. 獲取通道
SocketChannel sChannel = SocketChannel.open(new InetSocketAddress("127.0.0.1", 9898));
// 2. 切換非阻塞模式
sChannel.configureBlocking(false);
// 3. 分配指定大小的緩衝區
ByteBuffer buf = ByteBuffer.allocate(1024);
// 4. 發送數據給服務端
Scanner scan = new Scanner(System.in);
while (scan.hasNext()) {
String str = scan.next();
buf.put((new Date().toString() + "\n" + str).getBytes());
buf.flip();
sChannel.write(buf);
buf.clear();
}
// 5. 關閉通道
sChannel.close();
}
// 服務端
@Test
public void server() throws IOException {
// 1. 獲取通道
ServerSocketChannel ssChannel = ServerSocketChannel.open();
// 2. 切換非阻塞模式
ssChannel.configureBlocking(false);
// 3. 綁定連接
ssChannel.bind(new InetSocketAddress(9898));
// 4. 獲取選擇器
Selector selector = Selector.open();
// 5. 將通道註冊到選擇器上, 並且指定“監聽接收事件”
ssChannel.register(selector, SelectionKey.OP_ACCEPT);
// 6. 輪詢式的獲取選擇器上已經“準備就緒”的事件
while (selector.select() > 0) {
// 7. 獲取當前選擇器中所有註冊的“選擇鍵(已就緒的監聽事件)”
Iterator<SelectionKey> it = selector.selectedKeys().iterator();
while (it.hasNext()) {
// 8. 獲取準備“就緒”的是事件
SelectionKey sk = it.next();
// 9. 判斷具體是什麼事件準備就緒
if (sk.isAcceptable()) {
// 10. 若“接收就緒”,獲取客戶端連接
SocketChannel sChannel = ssChannel.accept();
// 11. 切換非阻塞模式
sChannel.configureBlocking(false);
// 12. 將該通道註冊到選擇器上
sChannel.register(selector, SelectionKey.OP_READ);
System.out.println("接受就緒");
} else if (sk.isReadable()) {
// 13. 獲取當前選擇器上“讀就緒”狀態的通道
SocketChannel sChannel = (SocketChannel) sk.channel();
// 14. 讀取數據
ByteBuffer buf = ByteBuffer.allocate(1024);
System.out.println("讀取數據");
int len = 0;
while ((len = sChannel.read(buf)) > 0) {
System.out.println(new String(buf.array(), 0, len));
buf.clear();
}
}
// 15. 取消選擇鍵 SelectionKey
it.remove();
}
}
}