JAVA NIO入門實例

jopen 10年前發布 | 20K 次閱讀 Java開發 Java NIO

NIO因為其高效性,成為了服務端的首選,大大提高了服務端的響應效率。以下是一個簡單的DEMO。服務端:

    package com.liuc.io;

import java.io.IOException;  
import java.net.InetSocketAddress;  
import java.nio.ByteBuffer;  
import java.nio.channels.SelectionKey;  
import java.nio.channels.Selector;  
import java.nio.channels.ServerSocketChannel;  
import java.nio.channels.SocketChannel;  
import java.nio.charset.Charset;  
import java.util.Iterator;  
import java.util.Set;  

import org.apache.log4j.Logger;  

public class NIOServer extends Thread {  
    private Logger log=Logger.getLogger(NIOServer.class);  
    private boolean stop=false;  
    private String ip="localhost";  
    private int port=9999;  
    private Selector selector;  
    private ByteBuffer buffer=ByteBuffer.allocate(1024);  
    private Charset gbkCharset = Charset.forName( "GB2312" );    
    public NIOServer() {  
        try {  
            selector=Selector.open();  
            ServerSocketChannel ssc=ServerSocketChannel.open();  
            InetSocketAddress address=new InetSocketAddress(ip, port);  
            //ServerSocket綁定IP  
            ssc.socket().bind(address);  
            //設置為非阻塞模式  
            ssc.configureBlocking(false);  
            //向選擇器注冊  
            ssc.register(selector,SelectionKey.OP_ACCEPT);    
            System.out.println("Listen to port:"+port);  
        } catch (IOException e) {  
            log.error(e);  
        }  

    }  

    @Override  
    public void run() {  
        System.out.println("deal with request");  
        while(!stop){  
                String content="HelloWorld";  
                try {  
                    int numKeys = selector.select();  
                    Set<SelectionKey> selectionKeys=selector.selectedKeys();  
                    Iterator<SelectionKey> iterator=selectionKeys.iterator();  
                    while (iterator.hasNext()) {  
                         SelectionKey key = iterator.next();  
                         iterator.remove();  
                         dealRequest(key,content);  
                    }  
                } catch (IOException e) {  
                    log.equals(e);  
                }  

        }  
    }  

    private void dealRequest(SelectionKey key,String contentToSend) {  
        try {  

             StringBuffer clientContent=new StringBuffer();  
              if(key.isAcceptable()){//測試此鍵的通道是否已準備好接受新的套接字連接。  
                  ServerSocketChannel ssc=(ServerSocketChannel) key.channel();  
                  SocketChannel sc=ssc.accept();  
                  sc.configureBlocking(false);  
                  sc.register(selector, SelectionKey. OP_READ);  
              }else if(key.isReadable()) {//測試此鍵的通道是否已準備好進行讀取。   
                  SocketChannel sc=(SocketChannel) key.channel();  
                  while(true){//讀取全部數據  
                      buffer.clear();  
                      int r=sc.read(buffer);  
                      buffer.flip();  
                      byte[] temp=new byte[r];  
                      if (r<=0) {  
                        break;  
                      }  
                      clientContent.append(gbkCharset.decode(buffer).toString());  

                  }  
                  //獲取全部的數據  
                  String content=clientContent.toString();  
                  String[] info=content.split(",");//用戶名密碼驗證  
                  System.out.println("客戶端的驗證請求為:"+content);  

                      sc.register(selector, SelectionKey.OP_WRITE);  

              }else if(key.isWritable()){//測試此鍵的通道是否已準備好進行寫入  
                System.out.println("要發送的消息為"+contentToSend);  
                buffer.clear();  
                buffer.put(contentToSend.getBytes());  
                buffer.flip();  
                SocketChannel sc = (SocketChannel)key.channel();  
                sc.write(buffer);  
                buffer.flip();  
                //發送完信息后關閉連接  
                key.cancel();  
                sc.close();  
            }  

        } catch (IOException e) {  
            System.out.println(e);  
            //客戶端斷開連接,所以從Selector中取消注冊   
            key.cancel();  
            if(key.channel() != null)  
                try {  
                    key.channel().close();  
                    log.debug("the client socket is closed!");  
                    //System.out.println("the client socket is closed!");  
                } catch (IOException e1) {  
                    e1.printStackTrace();  
                }  
        }  
    }  


    public static void main(String[] args){  
        new NIOServer().start();  
    }  

}  </pre><span style="font-size:medium;">客戶端:</span> <pre class="brush:java; toolbar: true; auto-links: false;">    package com.liuc.io;  

import java.io.IOException;  
import java.io.InputStream;  
import java.io.OutputStream;  
import java.net.Socket;  
import java.net.UnknownHostException;  

public class NIOClient {  

    /** 
     * @param args 
     */  
    public static void main(String[] args) {  
        Socket socket=null;  
        OutputStream outputStream=null;  
        InputStream inputStream=null;  
        try {  
            socket=new Socket("localhost", 9999);  
            outputStream=socket.getOutputStream();  
            String sendContent="HelloWorld";  
            outputStream.write(sendContent.getBytes());  
            outputStream.flush();  


            inputStream=socket.getInputStream();  
            byte[] bytes=new byte[1024];  
            int n=inputStream.read(bytes);  
            System.out.println(new String(bytes));  


        } catch (UnknownHostException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }finally{  
            try {  
                    if(inputStream!=null){  
                        inputStream.close();  
                    }  
                    if (outputStream!=null) {  
                        outputStream.close();  
                    }  
                    if (socket!=null) {  
                        socket.close();  
                    }  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  

        }  


    }  

}  </pre>來自:http://blog.csdn.net/shanhuhau/article/details/8728594
 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!