Java NIO 服務器與客戶端實現文件下載

寫在前面java

對於Java NIO已經學習了一段時間了,週末實踐了下,折騰了一天,總算對NIO的理論,有了一個感性的認識。下面的實踐是:服務器與客戶端都採用NIO的方式來實現文件下載。對於傳統的SOCKET BIO方式,服務器端會爲每一個鏈接上的客戶端分配一個Worker線程來進行doWork,而NIO SERVER卻沒有爲每一個Socket連接分配線程的必要了,避免了大量的線程所需的上下文切換,藉助NIO提供的Selector機制,只須要一個或者幾個線程來管理成百上千的SOCKET鏈接。那麼下面咱們就來看看吧!數組


文件下載輔助類服務器

/**
 * 這個類的基本思路是,讀取本地文件到緩衝區
 * 由於通道只能操做緩衝區
 */
class DownloadFileProcesser implements Closeable{
private ByteBuffer buffer = ByteBuffer.allocate(8 * 1024);
private FileChannel fileChannel ;
public DownloadFileProcesser() {
    try{
        FileInputStream fis = new FileInputStream("e:/tmp/Shell學習筆記.pdf");
        fileChannel = fis.getChannel();
    }catch(Exception e){
        e.printStackTrace();
    }
}
public int readFile2Buffer() throws IOException{
    int count = 0;
    buffer.clear();
    count = fileChannel.read(buffer);
    buffer.flip();
    return count;
}
public ByteBuffer getByteBuffer(){
    return buffer;
}
@Override
public void close() throws IOException {
    fileChannel.close();
}

}


服務端代碼:dom

public class ServerMain {

public static void main(String[] args) throws IOException {
    ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
    serverSocketChannel.configureBlocking(false);
    serverSocketChannel.socket().bind(new InetSocketAddress(8887));
    Selector selector = Selector.open();
    serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);

    while (true) {
        selector.select();
        Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
        while (iterator.hasNext()) {
        SelectionKey s = iterator.next();
        // 若是客戶端有鏈接請求
        if (s.isAcceptable()) {
        System.out.println("客戶端鏈接請求..");
        ServerSocketChannel ssc = (ServerSocketChannel) s.channel();
        SocketChannel sc = ssc.accept();
        sc.configureBlocking(false);
        sc.register(selector, SelectionKey.OP_READ);
        }
        // 若是客戶端有發送數據請求
        if (s.isReadable()) {
        System.out.println("接受客戶端發送過來的文本消息...");
        //這裏拿出的通道就是ACCEPT上註冊的SocketChannel通道
        SocketChannel sc = (SocketChannel) s.channel();
        //要讀取數據先要準備好BUFFER緩衝區
        ByteBuffer buffer = ByteBuffer.allocate(8 * 1024);
        //準備BYTE數組,造成輸出
        sc.read(buffer);
        byte[] clientByteInfo = new byte[buffer.position()];
        buffer.flip();
        buffer.get(clientByteInfo);
        System.out.println("服務器端收到消息:" + new String(clientByteInfo,"utf-8"));
        //CLIENT下一步的動做就是讀取服務器端的文件,所以須要註冊寫事件
        SelectionKey selectionKey = sc.register(selector, SelectionKey.OP_WRITE);
        //在這個selectionKey上綁定一個對象,以供寫操做時取出進行處理
        DownloadFileProcesser downloadFileProcesser = new DownloadFileProcesser();
        selectionKey.attach(downloadFileProcesser);
        }
    
    // 若是客戶端有下載文件數據請求
    if (s.isWritable()) {
    //這裏把p_w_upload取出進行寫入操做
    DownloadFileProcesser downloadFileProcesser = (DownloadFileProcesser)s.p_w_upload();
    int count = downloadFileProcesser.readFile2Buffer();
   
    if(count <= 0){
        System.out.println("客戶端下載完畢...");
        //關閉通道
        s.channel().close();
        downloadFileProcesser.close();
    }else{
        //須要注意的是咱們這裏並無出現常見的while寫的結構,這是爲什麼?
        //由於client其實不斷的在read操做,從而觸發了SELECTOR的不斷寫事件!
        SocketChannel sc = (SocketChannel)s.channel();
        sc.write(downloadFileProcesser.getByteBuffer());
     }
   }
        iterator.remove();
  }
 }
 }
}


客戶端代碼:socket

class Client4DownloadFile implements Runnable{
    
    //標示
    private String name;
    private FileChannel fileChannel;
    public Client4DownloadFile(String name , RandomAccessFile randomAccessFile){
    this.name = name;
    this.fileChannel = randomAccessFile.getChannel(); 
    }
    
    private ByteBuffer buffer = ByteBuffer.allocate(8 * 1024);

    @Override
public void run() {
try {
    SocketChannel sc = SocketChannel.open();
    Selector selector = Selector.open();
    sc.configureBlocking(false);
    sc.register(selector, SelectionKey.OP_CONNECT);
    sc.connect(new InetSocketAddress("127.0.0.1",8887));
   
    while(true){
        selector.select();
        Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
       
        while(iterator.hasNext()){
        SelectionKey s = iterator.next();
        if(s.isConnectable()){
        System.out.println("客戶端[" + name + "]已經鏈接上了服務器...");
        SocketChannel sc2 = (SocketChannel)s.channel();
        if(sc2.isConnectionPending() && sc2.finishConnect()){
        sc2.configureBlocking(false);
        String msg = "Thread-" + name + " send message!";
        byte[] b = msg.getBytes("utf-8");
        sc2.write(ByteBuffer.wrap(b));
        System.out.println("客戶端[" + name + "]給服務器端發送文本消息完畢...");
        sc2.register(selector, SelectionKey.OP_READ);
    }
    }
    
    if(s.isReadable()){
        SocketChannel sc3 = (SocketChannel)s.channel();
        buffer.clear();
        int count = sc3.read(buffer);
        if(count <= 0){
        s.cancel();
        System.out.println("Thread " + name + " 下載完畢...");
    }
    while(count > 0){
        buffer.flip();
        fileChannel.write(buffer);
        count = sc3.read(buffer);
        }
    }
        iterator.remove();
    }
   }
  } catch (IOException e) {
        e.printStackTrace();
  }
}
}



public class ClientMain {
    public static void main(String[] args) throws FileNotFoundException{
        for(int i = 0 ; i < 10 ; i++){
            File file = new File("e:/tmp/" + i + ".pdf");
            RandomAccessFile raf = new RandomAccessFile(file,"rw");
            Client4DownloadFile client4DownloadFile = new Client4DownloadFile("" + i, raf);
            new Thread(client4DownloadFile).start();
        }
    }
}


wKioL1Z2IYyykWSeAAEVYbJjp3U161.png


wKiom1Z2I_6ScuIpAABGN4wDSSw847.png

相關文章
相關標籤/搜索