Channel類似于傳統(tǒng)的流對(duì)象,但與傳統(tǒng)的流對(duì)象有兩個(gè)主要區(qū)別:
1、Channel可以直接將指定文件的部分或全部直接映射成Buffer。
2、程序不能直接訪問Channel中的數(shù)據(jù),包括讀、寫入都不行,Channel只能與Buffer進(jìn)行交互。也就是說,如果要從Channel中取得數(shù)據(jù),必須先用Buffer從Channel中取出一些數(shù)據(jù),然后讓程序從Buffer中取出這些數(shù)據(jù);如果要將程序中的數(shù)據(jù)寫入Channel,一樣先讓程序?qū)⒄l(shuí)放入Buffer中,程序再將Buffer里的數(shù)據(jù)寫入Channel中。
?
創(chuàng)建serversocket對(duì)象
ServerSocketChannel ssc = ServerSocketChannel.open(); ServerSocket serverSocket = ssc.socket(); // Listen on port 1234 serverSocket.bind(new InetSocketAddress(1234));
?
阻塞和非阻塞
?
傳統(tǒng)的serversocket阻塞模式:
public class ServerSocketApp { public static void main(String[] args) throws Exception { ServerSocket ss = new ServerSocket(8989); ss.accept(); System.out.println(1); } }
? ? 運(yùn)行這個(gè)程序 ?為什么沒有輸出1 ????
? ? 因?yàn)? ServerSocket ?是阻塞模式 的 ,什么是阻塞,就是在沒有任何連接之前, accept方法一直在那里阻塞著 ,直到有connection來(lái)繼續(xù)往下執(zhí)行,所以在運(yùn)行程序的時(shí)候,并沒輸出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;// 線程池中的工作線程的數(shù)量 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);// 每個(gè)處理器處理POOL_MULTIPLE個(gè)線程 ssc = ServerSocketChannel.open();// 靜態(tài)函數(shù)返回一個(gè)ServerSocketChannel實(shí)例 ssc.socket().setReuseAddress(true);// 使得關(guān)閉服務(wù)器后再次啟動(dòng)服務(wù)器程序可以順利綁定相同的端口 ssc.socket().bind(new InetSocketAddress(port));// 綁定端口 es.execute(new Runnable() { public void run() { try { SocketChannel s = ssc.accept();// s.socket()返回socket對(duì)象,利用socket對(duì)象就可以進(jìn)行流的讀取和寫入了。 System.out.println(s.socket().getInetAddress()); s.close(); } catch (IOException e) { e.printStackTrace(); } } }); // 創(chuàng)建客戶端連接到服務(wù)器端。 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();// 關(guān)閉線程池。 } public static void main(String[] args) throws IOException { new NioTest(); } }
?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
