Channel類似于傳統的流對象,但與傳統的流對象有兩個主要區別:
1、Channel可以直接將指定文件的部分或全部直接映射成Buffer。
2、程序不能直接訪問Channel中的數據,包括讀、寫入都不行,Channel只能與Buffer進行交互。也就是說,如果要從Channel中取得數據,必須先用Buffer從Channel中取出一些數據,然后讓程序從Buffer中取出這些數據;如果要將程序中的數據寫入Channel,一樣先讓程序將誰放入Buffer中,程序再將Buffer里的數據寫入Channel中。
?
創建serversocket對象
ServerSocketChannel ssc = ServerSocketChannel.open();
ServerSocket serverSocket = ssc.socket(); // Listen on port 1234
serverSocket.bind(new InetSocketAddress(1234));
?
阻塞和非阻塞
?
傳統的serversocket阻塞模式:
public class ServerSocketApp {
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(8989);
ss.accept();
System.out.println(1);
}
}
? ? 運行這個程序 ?為什么沒有輸出1 ????
? ? 因為 ServerSocket ?是阻塞模式 的 ,什么是阻塞,就是在沒有任何連接之前, accept方法一直在那里阻塞著 ,直到有connection來繼續往下執行,所以在運行程序的時候,并沒輸出1,若要輸出 ?telnet一下就可以了
?
nio中的 非阻塞:
public static void main(String[] args) throws Exception {
ServerSocketChannel ssc = ServerSocketChannel.open();
ServerSocket ss = ssc.socket();
ss.bind(new InetSocketAddress(8989));
// set no blocking
ssc.configureBlocking(false);
ssc.accept();
System.out.println(1);
}
?
例子:?
package channel;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.UnknownHostException;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class NioTest {
private ExecutorService es = null;
private static int port = 8083;
private ServerSocketChannel ssc = null;
public static final int POOL_MULTIPLE = 4;// 線程池中的工作線程的數量
public NioTest() throws IOException {
// Runtime.getRuntime().availableProcessors()) Returns the number of
// processors available to the Java virtual machine.
es = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * POOL_MULTIPLE);// 每個處理器處理POOL_MULTIPLE個線程
ssc = ServerSocketChannel.open();// 靜態函數返回一個ServerSocketChannel實例
ssc.socket().setReuseAddress(true);// 使得關閉服務器后再次啟動服務器程序可以順利綁定相同的端口
ssc.socket().bind(new InetSocketAddress(port));// 綁定端口
es.execute(new Runnable() {
public void run() {
try {
SocketChannel s = ssc.accept();// s.socket()返回socket對象,利用socket對象就可以進行流的讀取和寫入了。
System.out.println(s.socket().getInetAddress());
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
});
// 創建客戶端連接到服務器端。
es.execute(new Runnable() {
public void run() {
try {
// Socket s = new Socket("localhost", port);
// s.close();
SocketChannel s = SocketChannel.open();
InetAddress i = InetAddress.getLocalHost();
System.out.println("client InetAddress : " + i);
s.connect(new InetSocketAddress(i, port));
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
es.shutdown();// 關閉線程池。
}
public static void main(String[] args) throws IOException {
new NioTest();
}
}
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

