1. 程式人生 > 其它 >JavaSE:NIO程式設計例項

JavaSE:NIO程式設計例項

客戶端

 1 public static void main(String[] args) throws IOException {
 2 
 3 
 4 
 5   // 建立客戶端
 6 
 7   SocketChannel sc = SocketChannel.open();
 8 
 9 
10 
11   // 指定要連線的伺服器ip和埠
12 
13   sc.connect(new InetSocketAddress("127.0.0.1", 9000) );
14 
15 
16 
17   // 建立緩衝輸出
18 
19     ByteBuffer buffer = ByteBuffer.allocate(1024);
20 21 22 23   // 給陣列新增資料 24 25   buffer.put ("Hi你好啊".getBytes()); 26 27 28 29   // 切換 30 31   buffer.flip(); 32 33 34 35   // 輸出資料 36 37   sc.write(buffer); 38 39 40 41   // 關閉資源 42 43   sc.close(); 44 45 }

Demo服務端

 1 import java.io.IOException;
 2 
 3 import java.net.InetSocketAddress;
 4 
 5
import java.nio.channels.SelectionKey; 6 7 import java.nio.channels.Selector; 8 9 import java.nio.channels.ServerSocketChannel; 10 11 import java.util.Set; 12 13 14 15 public class Demo服務端 { 16 17   main() throws IOException { 18 19     // 建立服務端物件 20 21     ServerSocketChannel ssc1 = ServerSocketChannel.open();
22 23     ssc1.bind(new InetSocketAddress(8000)); 24 25 26 27     // 設定非阻塞 28     ssc1.configureBlocking(false); 29      30     // 建立伺服器端物件 31     ServerSocketChannel ssc2 = ServerSocketChannel.open(); 32     ssc2.bind(new InetSocketAddress(9000)); 33     ssc2.configureBlocking(false); 34 35 36 37     // 建立伺服器端物件 38     ServerSocketChannel ssc3 = ServerSocketChannel.open(); 39     ssc3.bind(new InetSocketAddress(10001)); 40     ssc3.configureBlocking(false); 41 42 43 44     // 建立選擇器物件 45 46     Selector s = Selector.open(); 47 48 49 50     // 兩個伺服器,都要交給選擇器來管理 51 52     ssc1.register(s, SelectionKey.OP_ACCEPT); 53 54     ssc2.register(s, SelectionKey.OP_ACCEPT); 55 56     ssc3.register(s, SelectionKey.OP_ACCEPT); 57 58 59 60     // 獲取集合 61     // selectedKeys(): 返回集合,集合作用存放的是被連線的服務物件的key 62     Set<SelectionKey> set = s.selectedKeys(); 63     print("集合中元素的個數:" + set.size() );  // 0 (沒有伺服器被訪問的時候,顯示0) 64      65     // select(): 這是選擇器連線客戶端的方法 66     s.select(); 67 68 69     print("集合中元素的個數:" + set.size() );  // 1 (有一個服務端被訪問的時候,顯示1) 70 71   } 72 73 }

Selector服務端

  1 import java.io.IOException;
  2 
  3 import java.net.InetSocketAddress;
  4 
  5 import java.nio.ByteBuffer;
  6 
  7 import java.nio.channels.SelectionKey;
  8 
  9 import java.nio.channels.Selector;
 10 
 11 import java.nio.channels.ServerSocketChannel;
 12 
 13 import java.nio.channels.SocketChannel;
 14 
 15 import java.util.Iterator;
 16 
 17 
 18 
 19 public class Selector服務端 {
 20 
 21   main() throws IOException {
 22 
 23     //  1. 獲取Selector選擇器
 24 
 25     Selector selector = Selector.open();
 26 
 27 
 28 
 29     //  2. 獲取通道
 30 
 31     ServerSocketChannel ssc1 = ServerSocketChannel.open();
 32 
 33     ServerSocketChannel ssc2 = ServerSocketChannel.open();
 34 
 35     ServerSocketChannel ssc3 = ServerSocketChannel.open();
 36 
 37 
 38 
 39     //  3. 設定為非阻塞
 40 
 41     ssc1.configureBlocking(false);
 42 
 43     ssc2.configureBlocking(false);
 44 
 45     ssc3.configureBlocking(false);
 46 
 47 
 48 
 49     //  4.繫結連線
 50 
 51     ssc1.bind (new InetSocketAddress(8000));
 52 
 53     ssc2.bind (new InetSocketAddress(9000));
 54 
 55     ssc3.bind (new InetSocketAddress(10000));
 56 
 57 
 58 
 59     //  5.將通道註冊到選擇器上,並註冊的操作為:"接收"操作
 60     ssc1.register(selector, SelectionKey.OP_ACCEPT);
 61     ssc2.register(selector, SelectionKey.OP_ACCEPT);
 62     ssc3.register(selector, SelectionKey.OP_ACCEPT);
 63 
 64 
 65     //  6.採用輪詢的方式,查詢獲取"準備就緒"的註冊過的操作
 66     while (selector.select() > 0 ) {
 67 
 68       //  7. 獲取當前選擇器中,所有的選擇鍵 (“已經準備就緒的操作”)
 69       Iterator<SelectionKey> selectedKeys = selector.selectedKeys().iterator();
 70       while (selectedKeys.hasNext() ){
 71 
 72 
 73 
 74         //  8.  獲取"準備就緒"的事件
 75         SelectionKey selectedKey = selectedKeys.next();
 76 
 77 
 78         //  9.  獲取ServerSocketChannel
 79         ServerSocketChannel serverSocketChannel = (ServerSocketChannel) selectedKey.channel();
 80 
 81 
 82         //  10.  接收客戶端發來的資料
 83         SocketChannel socketChannel = serverSocketChannel.accept();
 84 
 85 
 86         //  11.  讀取資料
 87         ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
 88         int length = 0;
 89         while ( (length = socketChannel.read(byteBuffer)) != -1 ){
 90 
 91           byteBuffer.flip();
 92           print (new String(byteBuffer.array(),0,length) );
 93           byteBuffer.clear();
 94         }
 95         socketChannel.close();
 96 
 97       }
 98       //  12.  移除選擇鍵
 99       selectedKeys.remove();
100     }
101     //  13.  關閉連線
102     ssc1.close();
103     ssc2.close();
104     ssc3.close();
105   }
106 
107 }