免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
Bio、Nio、Aio的用法系列之NIO客戶端(三)

NIO客戶端的實(shí)現(xiàn)

上一篇文章我們提到了NIO,大家應(yīng)該對(duì)NIO有了一定的了解,接下來我們繼續(xù)學(xué)習(xí)NIO的客戶端實(shí)現(xiàn)

1、代碼展示

首先我們還是先啟動(dòng)一個(gè)線程

public class NioServer {
public static void main(String [] args){
   //啟動(dòng)一個(gè)線程
   new Thread(new NioServerHandle()).start();
}

}

處理類

public class NioServerHandle implements Runnable{private ServerSocketChannel serverSocketChannel;
private Selector selector;
boolean stop  = false;
//初始化注冊(cè)
public NioServerHandle(){
   try {
       //獲取ServerSocketChannel對(duì)象
       serverSocketChannel = ServerSocketChannel.open();
       //綁定ip
       serverSocketChannel.socket().bind(new InetSocketAddress('127.0.0.1',8989));
       //設(shè)置為非阻塞
       serverSocketChannel.configureBlocking(false);
       //獲取Selector
       selector = Selector.open();
       //將管道注冊(cè)到多路復(fù)用器selector上
       serverSocketChannel.register(selector,SelectionKey.OP_ACCEPT);
   } catch (IOException e) {
       e.printStackTrace();
       System.exit(1);
   }
}
public void stop(){
   this.stop = true;
}
@Override
public void run() {
   //輪詢key
   while (!stop){
       try {
           //設(shè)置超時(shí)時(shí)間
           selector.select(1000);
           //獲取所有key
           Set selectionKeys = selector.selectedKeys();
           //遍歷
           Iterator
it = selectionKeys.iterator();
           SelectionKey selectionKey = null;
           while (it.hasNext()){
               selectionKey = it.next();
               //獲取到就緒數(shù)組進(jìn)行操作,并移除
               it.remove();
               try {
                   handle(selectionKey);
               }catch (Exception e){
                   selectionKey.cancel();
                   selectionKey.channel().close();
               }
           }
       } catch (IOException e) {
           e.printStackTrace();
       }
   }
}
/**
* 處理
* @param key
* @throws Exception
*/

public void handle(SelectionKey key) throws Exception{
   if (key.isValid()){
       if (key.isAcceptable()){
           //獲取ServerSocketChannel
           ServerSocketChannel ssc = (ServerSocketChannel)key.channel();
           //接受請(qǐng)求
           SocketChannel sc = ssc.accept();
           //設(shè)置為非阻塞
           sc.configureBlocking(false);
           //注冊(cè)到多路復(fù)用器
           sc.register(selector,SelectionKey.OP_READ);
       }
       //read data
       if (key.isReadable()){
           //得到SocketChannel
           SocketChannel sc = (SocketChannel) key.channel();
           //設(shè)置字節(jié)緩沖區(qū)
           ByteBuffer readBuffer = ByteBuffer.allocate(1024);
           //將通道的數(shù)據(jù)讀取碼流
           int readByte = sc.read(readBuffer);
           //對(duì)于大于0的情況進(jìn)行編解碼
           if (readByte > 0){
               //將當(dāng)前的緩沖區(qū)的limit設(shè)置為0,讓后面進(jìn)行讀取
               readBuffer.flip();
               //根據(jù)緩沖區(qū)的可讀大小設(shè)置字節(jié)數(shù)組
               byte [] bytes = new byte[readBuffer.remaining()];
               //get將讀取的數(shù)據(jù)放入字節(jié)數(shù)組
               readBuffer.get(bytes);
               //將字節(jié)數(shù)組按照UTF-8的格式輸出到body
               String body = new String(bytes,'UTF-8');
               System.out.print('The Time server recevive order:'+body);
               String currentTime = 'query'.equals(body)?new java.util.Date(System.currentTimeMillis()).toString():'No';
               //進(jìn)行輸出操作
               doWrite(sc,currentTime);
           }else if (readByte <>0 ){
               key.cancel();
               sc.close();
           }else {
           }
       }
   }
}
/**
* 將數(shù)據(jù)返回給客戶端
* @param sc
* @param currentTime
* @throws Exception
*/

public void doWrite(SocketChannel sc,String currentTime) throws Exception{
   if (currentTime != null && currentTime.trim().length() > 0){
       //轉(zhuǎn)換為字節(jié)數(shù)組,放到緩沖區(qū)
       byte [] bytes = currentTime.getBytes();
       ByteBuffer byteBuffer = ByteBuffer.allocate(bytes.length);
       byteBuffer.put(bytes);
       byteBuffer.flip();
       sc.write(byteBuffer);
   }
}}


本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
基于NIO實(shí)現(xiàn)非阻塞Socket編程
Java NIO (異步IO)Socket通信例子
Android NIO非阻塞包
全面解讀Java NIO工作原理(2)AA
Java NIO API詳解
理解Java NIO
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服