利用NIO建立Socket服務器

Socket的Channel在Selector上註冊某一種動作,Selector通過select操作,監視所有在該Selector註冊過的Channel的對應的動作,如果監測到某一對應的動作,則返回selectedKeys,自己手動取到各個SelectionKey進行相應的處理。當然NIO不僅可以接受Socket的Channel,還有文件操作等其他IO操作。

AD:

傳統的Java 的IO,利用Socket建立服務器,接收客戶端連接,一般都是爲每一個連接建立一個線程,如果連接數巨大,那麼服務器開銷也將巨大。。NIO的原理,可以參照圖:

Socket的Channel在Selector上註冊某一種動作,Selector通過select操作,監視所有在該Selector註冊過的Channel的對應的動作,如果監測到某一對應的動作,則返回selectedKeys,自己手動取到各個SelectionKey進行相應的處理。當然NIO不僅可以接受Socket的Channel,還有文件操作等其他IO操作。

作業的要求:

使用socket編程實現一個簡單的文件服務器。客戶端程序實現put功能(將一個文件從本地傳到文件服務器)和get功能(從文件服務器取一遠程文件存爲本地文件)。客戶端和文件服務器不在同一臺機器上。

put [-h hostname] [-p portname] local_filename remote_filename
get [-h hostname] [-p portname] remote_filename local_filename

服務器端不使用nio,直接使用io的socket代碼如下:

  1. import java.io.*; 
  2. import java.net.ServerSocket; 
  3. import java.net.Socket; 
  4.  
  5. public class ServerMain { 
  6.      
  7.     public static void main(String[] args) { 
  8.  
  9.         class SocketThread extends Thread{ 
  10.              
  11.             private Socket socket; 
  12.             private byte[] buf; 
  13.             private int len = 0
  14.             public SocketThread(Socket socket) {  
  15.                 this.socket = socket; 
  16.                 buf = new byte[1024]; 
  17.             } 
  18.  
  19.             @Override 
  20.             public void run() { 
  21.                 try {    
  22.                     DataInputStream dis = new DataInputStream(socket.getInputStream()); 
  23.                     DataOutputStream dos = new DataOutputStream(socket.getOutputStream());  
  24.                      
  25.                     //String command = dis.readUTF();  
  26.                     len = dis.read(buf); 
  27.                     String command = new String(buf,0,len); 
  28.                      
  29.                     System.out.println("command=="+command); 
  30.                      
  31.                     String[] temp =command.split(" "); 
  32.                     command = temp[0];  //命令  是put還是get 
  33.                     String filename = temp[1];  //文件名 
  34.                      
  35.                     File file = new File("C:\\",filename);//假設放在C盤 
  36.                     if(command.equals("get")){ 
  37.                         if(!file.exists()){ 
  38.                             //dos.writeUTF("notexists"); 
  39.                             dos.write("notexists".getBytes()); 
  40.                             dos.flush(); 
  41.                             System.out.println("沒有這個文件,無法提供下載!"); 
  42.                             dis.close(); 
  43.                             dos.close(); 
  44.                             socket.close(); 
  45.                             return
  46.                         } 
  47.                         //dos.writeUTF("DownloadReady "+file.length());  
  48.                         dos.write("準備下載".getBytes()); 
  49.                         dos.flush(); 
  50.                          
  51.                         System.out.println("正在接受文件下載..."); 
  52.                         DataInputStream fis = new DataInputStream(new BufferedInputStream(new FileInputStream(file)));  
  53.    
  54.                         while ((len = fis.read(buf))!= -1) {  
  55.                             dos.write(buf, 0, len); 
  56.                         } 
  57.                         dos.flush(); 
  58.                          
  59.                         fis.close();      
  60.                         System.out.println("文件傳輸完成"); 
  61.                     } 
  62.                     else {  
  63.                         //dos.writeUTF("UploadReady");  
  64.                         dos.write("UploadReady".getBytes()); 
  65.                         dos.flush(); 
  66.                          
  67.                         System.out.println("正在接受文件上傳..."); 
  68.                         DataOutputStream fileOut =  
  69.                             new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file))); 
  70.   
  71.                         while ((len = dis.read(buf))!=-1) {    
  72.                             fileOut.write(buf, 0, len); 
  73.                         } 
  74.                         System.out.println("上傳完畢!"); 
  75.                         fileOut.close();  
  76.                     } 
  77.                     dis.close(); 
  78.                     dos.close(); 
  79.                     socket.close();  
  80.                 } catch (Exception e) { 
  81.                     e.printStackTrace(); 
  82.                 } 
  83.             } 
  84.              
  85.         } 
  86.          
  87.         System.out.println("等待客戶端連接...."); 
  88.         int index = 0
  89.         try { 
  90.             ServerSocket server = new ServerSocket(9527,300); //端口號9527  允許最大連接數300 
  91.             while (true) { 
  92.                 Socket socket = server.accept(); 
  93.                 System.out.println("收到第"+(++index)+"個連接"); 
  94.                 new SocketThread(socket).start(); //對每個連接創建一個線程 
  95.             } 
  96.         } catch (Exception e) { 
  97.             e.printStackTrace(); 
  98.         } 
  99.     } 

使用NIO建立的Socket服務器,代碼如下:

  1. import java.io.BufferedInputStream; 
  2. import java.io.BufferedOutputStream; 
  3. import java.io.DataInputStream; 
  4. import java.io.DataOutputStream; 
  5. import java.io.File; 
  6. import java.io.FileInputStream; 
  7. import java.io.FileOutputStream; 
  8. import java.io.IOException; 
  9. import java.net.InetSocketAddress; 
  10. import java.nio.ByteBuffer;  
  11. import java.nio.CharBuffer; 
  12. import java.nio.channels.SelectionKey; 
  13. import java.nio.channels.Selector; 
  14. import java.nio.channels.ServerSocketChannel; 
  15. import java.nio.channels.SocketChannel; 
  16. import java.nio.charset.Charset; 
  17. import java.nio.charset.CharsetDecoder; 
  18. import java.nio.charset.CharsetEncoder; 
  19. import java.util.Iterator; 
  20.  
  21. public class NewSocketServer { 
  22.   
  23.     private static final int port = 9527
  24.     private Selector selector; 
  25.     private ByteBuffer clientBuffer = ByteBuffer.allocate(1024);   
  26.     private CharsetDecoder decoder = Charset.forName("GB2312").newDecoder(); 
  27.     private CharsetEncoder encoder = Charset.forName("GB2312").newEncoder();  
  28.     //編碼解碼格式設置成GBK也行.UTF-8不行,中文亂碼  (前提都是客戶端沒有設置任何編碼解碼格式) 
  29.      
  30.     public void setListener() throws Exception{ 
  31.          
  32.         selector = Selector.open(); //打開選擇器    
  33.          
  34.         ServerSocketChannel server = ServerSocketChannel.open();  //定義一個 ServerSocketChannel通道 
  35.         server.socket().bind(new InetSocketAddress(port));  //ServerSocketChannel綁定端口   
  36.         server.configureBlocking(false);   //配置通道使用非阻塞模式 
  37.         server.register(selector, SelectionKey.OP_ACCEPT); //該通道在selector上註冊  接受連接的動作 
  38.          
  39.         while(true
  40.         {     
  41.             selector.select();   //select() 會阻塞,直到在該selector上註冊的channel有對應的消息讀入 
  42.             Iterator iter = selector.selectedKeys().iterator();    
  43.             while (iter.hasNext()) {     
  44.                 SelectionKey key = (SelectionKey) iter.next();    
  45.                 iter.remove();  // 刪除此消息  
  46.                 process(key);   // 當前線程內處理。(爲了高效,一般會在另一個線程中處理此消息) 
  47.             }    
  48.         }    
  49.     } 
  50.      
  51.      private void process(SelectionKey key) throws IOException {    
  52.             if (key.isAcceptable()) { // 接收請求    
  53.                 ServerSocketChannel server = (ServerSocketChannel) key.channel();    
  54.                 SocketChannel channel = server.accept();//類似於io的socket,ServerSocketChannel的accept函數返回 SocketChannel 
  55.                 channel.configureBlocking(false);   //設置非阻塞模式    
  56.                 SelectionKey sKey = channel.register(selector, SelectionKey.OP_READ);  
  57.                 sKey.attach("read_command"); //這兒接收到連接請求之後可以爲每個連接設置一個ID 
  58.             }  
  59.             else if (key.isReadable()) { // 讀信息     
  60.                 SocketChannel channel = (SocketChannel) key.channel();    
  61.                 String name = (String) key.attachment();  
  62.                 if(name.equals("read_command")){ 
  63.                     int count = channel.read(clientBuffer);  
  64.                     if (count > 0) {    
  65.                         clientBuffer.flip();    
  66.                         CharBuffer charBuffer = decoder.decode(clientBuffer);    
  67.                         String command = charBuffer.toString();    
  68.                          
  69.                         //command形如:get abc.png 或者  put aaa.png 
  70.                         System.out.println("command===="+command);  //得到客戶端傳來的命令  
  71.                          
  72.                         String[] temp =command.split(" "); 
  73.                         command = temp[0];  //命令  是put還是get 
  74.                         String filename = temp[1];  //文件名 
  75.                          
  76.                         SelectionKey sKey = channel.register(selector,SelectionKey.OP_WRITE);    
  77.                         if(command.equals("put"))sKey.attach("UploadReady#"+filename);  //要保護該通道的文件名 
  78.                         else if(command.equals("get")){  
  79.                             if(!new File("C:\\",filename).exists()){ //假設文件都是在C盤根目錄 
  80.                                 System.out.println("沒有這個文件,無法提供下載!"); 
  81.                                 sKey.attach("notexists");  
  82.                             } 
  83.                             else sKey.attach("DownloadReady#"+filename); //要保護該通道的文件名 
  84.                         } 
  85.                     } else {    
  86.                         channel.close();    
  87.                     }    
  88.                 } 
  89.                 else if(name.startsWith("read_file")){//這兒可以新開一個線程     文件操作也可以用NIO  
  90.                     DataOutputStream fileOut =  
  91.                         new DataOutputStream( 
  92.                                 new BufferedOutputStream( 
  93.                                         new FileOutputStream( 
  94.                                                 new File("C:\\",name.split("#")[1])))); 
  95.   
  96.                     int passlen = channel.read(clientBuffer);   
  97.                     while (passlen>=0) {    
  98.                         clientBuffer.flip();   
  99.                         fileOut.write(clientBuffer.array(), 0, passlen);  
  100.                         passlen = channel.read(clientBuffer); 
  101.                     } 
  102.                     System.out.println("上傳完畢!"); 
  103.                     fileOut.close();  
  104.                     channel.close(); 
  105.                 } 
  106.                 clientBuffer.clear();    
  107.             }  
  108.             else if (key.isWritable()) { // 寫事件    
  109.                 SocketChannel channel = (SocketChannel) key.channel();    
  110.                 String flag = (String) key.attachment();     
  111.                 if(flag.startsWith("downloading")){//這兒可以新開一個線程   文件操作也可以用NIO 
  112.                     DataInputStream fis = new DataInputStream( 
  113.                             new BufferedInputStream( 
  114.                                     new FileInputStream( 
  115.                                             new File("C:\\",flag.split("#")[1]))));  
  116.                       
  117.                     byte[] buf = new byte[1024]; 
  118.                     int len =0;  
  119.                     while ((len = fis.read(buf))!= -1) {  
  120.                         channel.write(ByteBuffer.wrap(buf, 0, len));   
  121.                     }   
  122.                     fis.close();      
  123.                     System.out.println("文件傳輸完成"); 
  124.                     channel.close(); 
  125.                 } 
  126.                 else if(flag.equals("notexists")){  
  127.                     //channel.write(encoder.encode(CharBuffer.wrap(flag)));    
  128.                     channel.write(ByteBuffer.wrap(flag.getBytes())); //不用編碼也行    客戶端直接接收    中文也不是亂碼 
  129.                     channel.close(); 
  130.                 } 
  131.                 else if(flag.startsWith("UploadReady")){  
  132.                     channel.write(encoder.encode(CharBuffer.wrap("UploadReady")));  
  133.                      
  134.                     //這兒如果不重新註冊該通道的讀操作    selector選擇到該通道的將繼續永遠是寫操作,也就無法跳轉到上面的接受上傳的處理 
  135.                     SelectionKey sKey =channel.register(selector, SelectionKey.OP_READ);//register是覆蓋的????!!! 
  136.                     sKey.attach("read_file#"+flag.split("#")[1]); 
  137.                     //key.attach("read_file#"+flag.split("#")[1]); //select不到讀操作 
  138.                 } 
  139.                 else if(flag.startsWith("DownloadReady")){  
  140.                     channel.write(ByteBuffer.wrap("準備下載".getBytes()));  
  141.                     //channel.write(encoder.encode(CharBuffer.wrap("準備下載")));    
  142.                     key.attach("downloading#"+flag.split("#")[1]); 
  143.                 }  
  144.             }   
  145.         }    
  146.      
  147.     public static void main(String[] args) { 
  148.          
  149.         try { 
  150.              System.out.println("等待來至" + port + "端口的客戶端連接.....");  
  151.             new NewSocketServer().setListener(); 
  152.         } catch (Exception e) { 
  153.             e.printStackTrace(); 
  154.         } 
  155.  
  156.     } 

客戶端代碼如下:

  1. import java.io.*; 
  2. import java.net.InetAddress; 
  3. import java.net.Socket; 
  4. import java.util.Scanner; 
  5.  
  6. public class ClientMain { 
  7.  
  8.     private   int ServerPort = 9527
  9.     private   String ServerAddress = "192.168.1.154"
  10.     private   String GetOrPut = "get";    
  11.     private   String local_filename = "";  
  12.     private   String remote_filename  = "";  
  13.     private   byte[] buf; 
  14.     private   int len; 
  15.     class SocketThread extends Thread{ 
  16.          
  17.         @Override 
  18.         public void run() { 
  19.              try { 
  20.                  
  21.                 File file = new File("C:\\",local_filename); //假設文件放在C盤 
  22.                 if(!file.exists()&&GetOrPut.equals("put")){  
  23.                     System.out.println("本地沒有這個文件,無法上傳!");  
  24.                     return
  25.                 }  
  26.                  
  27.                 InetAddress loalhost = InetAddress.getLocalHost(); 
  28.                 Socket socket = new Socket(ServerAddress,ServerPort,loalhost,44); 
  29.                                             //服務器IP地址  端口號   本機IP 本機端口號 
  30.                 DataInputStream dis = new DataInputStream(socket.getInputStream()); 
  31.                 DataOutputStream dos = new DataOutputStream(socket.getOutputStream()); 
  32.                   
  33.                 //dos.writeUTF(GetOrPut+" "+remote_filename);//服務器端如果是io的socket,writeUTF和writeUTF對接 
  34.                 dos.write((GetOrPut+" "+remote_filename).getBytes()); 
  35.                 dos.flush();  
  36.                
  37.                 //String tempString = dis.writeUTF();  
  38.                 buf = new byte[1024]; 
  39.                 len = dis.read(buf); 
  40.                 String tempString = new String(buf,0,len);//服務器反饋的信息 
  41.                  
  42.                 //System.out.println(tempString);  
  43.                 if(tempString.equals("notexists")){ 
  44.                     System.out.println("服務器沒有這個文件,無法下載!");  
  45.                     dos.close(); 
  46.                     dis.close(); 
  47.                     socket.close(); 
  48.                     return
  49.                 } 
  50.                  
  51.                 if(tempString.startsWith("準備下載")){   
  52.                     DataOutputStream fileOut =  
  53.                         new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file))); 
  54.   
  55.                     while ((len = dis.read(buf))!=-1) {    
  56.                         fileOut.write(buf, 0, len); 
  57.                     } 
  58.                     System.out.println("下載完畢!"); 
  59.                     fileOut.close(); 
  60.                     dos.close(); 
  61.                     dis.close(); 
  62.                     socket.close(); 
  63.                 } 
  64.                 else if(tempString.equals("UploadReady")){   
  65.                     System.out.println("正在上傳文件......."); 
  66.                     DataInputStream fis = new DataInputStream(new BufferedInputStream(new FileInputStream(file)));  
  67.                        
  68.                     while ((len = fis.read(buf))!= -1) {   
  69.                         dos.write(buf, 0, len); 
  70.                     } 
  71.                     dos.flush(); 
  72.                     System.out.println("上傳完畢!"); 
  73.                     fis.close(); 
  74.                     dis.close(); 
  75.                     dos.close(); 
  76.                     socket.close(); 
  77.                 } 
  78.                  
  79.             } catch (Exception e) { 
  80.                 e.printStackTrace(); 
  81.             } 
  82.         } 
  83.          
  84.     } 
  85.      
  86.     public boolean checkCommand(String command) 
  87.     {  
  88.         if(!command.startsWith("put")&&!command.startsWith("get")){ 
  89.             System.out.println("輸入命令錯誤"); 
  90.             return false
  91.         } 
  92.          
  93.         int index = -1
  94.         String temp = ""
  95.         String[] tempStrings = null
  96.          
  97.         if((index=command.indexOf("-h"))>0){ 
  98.             temp = command.substring(index+3); 
  99.             temp = temp.substring(0, temp.indexOf(' ')); 
  100.             ServerAddress = temp; 
  101.         } 
  102.         if((index=command.indexOf("-p"))>0){ 
  103.             temp = command.substring(index+3); 
  104.             temp = temp.substring(0, temp.indexOf(' ')); 
  105.             ServerPort = Integer.valueOf(temp); 
  106.         } 
  107.          
  108.         tempStrings = command.split(" "); 
  109.         if(command.startsWith("put")){ 
  110.             GetOrPut = "put"
  111.             local_filename = tempStrings[tempStrings.length-2]; 
  112.             remote_filename = tempStrings[tempStrings.length-1]; 
  113.         } 
  114.         else if(command.startsWith("get")){ 
  115.             GetOrPut = "get"
  116.             local_filename = tempStrings[tempStrings.length-1]; 
  117.             remote_filename = tempStrings[tempStrings.length-2]; 
  118.         } 
  119.          
  120.         return true
  121.     } 
  122.      
  123.     public static void main(String[] args) { 
  124.         ClientMain thisC= new ClientMain();  
  125.         Scanner sc = new Scanner(System.in); 
  126.         String commandString = ""
  127.         do { 
  128.             System.out.println("請輸入命令:");  
  129.             commandString = sc.nextLine(); 
  130.         } while (!thisC.checkCommand(commandString));  
  131.          
  132.         ClientMain.SocketThread a = thisC.new SocketThread(); 
  133.         a.start(); 
  134.     } 

原文鏈接:http://tonl.iteye.com/blog/1202817

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章