Java Socket IO(BIO、NIO)

總結下Java socket IO。首先是各類IO的定義,這個定義彷佛也是衆說紛紜。我按照stackoverflow上面的解釋:java

IO有兩種分法:按照阻塞或者按照同步。按照阻塞,有阻塞IO和非阻塞IO。按照同步就是同步IO或者異步IO。咱們能夠認爲阻塞IO和同步IO相等,而非阻塞IO和異步IO不一樣。git

阻塞IO或者同步IO是指:IO的請求發出去以後,請求者一直在等待回覆,當IO的數據回來到來以後,請求者就開始接受數據。阻塞的意思就是:IO請求發出去以後請求線程就中止在那裏,一直等待數據到來。github

非阻塞則是:IO請求發出去以後,會馬上收到回覆,這個回覆多是IO能夠當即進行,或者是沒法進行IO的錯誤。因此非阻塞IO請求者必須一直調用那個API,一直到API返回能夠進行IO的信號。編程

 

異步IO:IO請求發出以後,後臺會創建一個進程,處理IO,當IO都處理完以後,把處理完的數據交給IO的請求者。多線程

本文用Java socket實現阻塞和非阻塞IO(這裏大部份內容都學習自千與的專欄,這是位大牛,竟然hadoop源碼分析寫了19篇博客)。而後本文章的全部代碼我都放在Github上了。異步

阻塞IO

阻塞IO比較簡單,就是用普通的socket去寫,由於沒有什麼太複雜的處理。創建一個socket,而後,獲取它的inputstream和outputstream,而後進行讀寫操做。socket

Server端主要代碼,handleSocket就是從socket裏面讀取數據,而後向client寫數據:async

	ServerSocket serverSocket = new ServerSocket(port); while (true) { socket = serverSocket.accept(); handleSocket(socket); }

Client只是簡單的發送一句消息:oop

	socket = new Socket(host, port); out = socket.getOutputStream(); out.write(data); out.flush(); in = socket.getInputStream(); byte[] buffer = new byte[128]; int receivedBytes; if((receivedBytes = in.read(buffer))!=-1){ System.out.println("Client: received msg from server: " + new String(buffer, 0, receivedBytes)); }

同時client5000個,使用了大概5s。而後還可使用多線程Server,就是在每個client到的時候分配一個線程來處理這個IO,理論上能夠增長效率,可是彷佛是由於每次處理時間過短,效果不明顯。主要代碼:源碼分析

	boolean flag = false; int count = 0; ServerSocket serverSocket = new ServerSocket(port); Date start = null; while (true) { socket = serverSocket.accept(); if (!flag) { start = new Date(); flag = true; } pool.execute(new RequestHandler(socket)); if(++count== threadCount){ flag = false; Date end = new Date(); System.out.println(threadCount+" client requests spends: " + (end.getTime() -start.getTime())); } }

其中的pool是ExecutorService,提供線程池,而後RequestHandler是一個Runnable類,用來處理一個socket鏈接。簡單講來,就是把前面server處理socket的代碼放到了這個handler裏面。

NIO,非阻塞IO

我認定Java的NIO包實現的是同步非阻塞IO,也有人說不是,至少我這裏這麼認爲。在server端,首先打開一個channel,而後向其中註冊一個selector,這個selector會在channel中輪詢註冊的事件,而後根據事件的類型做出處理。通常事件的類型有Accept、read、write。

在Client端也能夠是同樣的註冊,而後經過和server一樣的處理方式處理事件(可是沒有accept事件,由於只有server才能accept的)。可是個人例子裏面只是簡單的發送了一條消息。

Server的主要代碼:

	Selector selector = Selector.open(); ServerSocketChannel serverSocketChannel = ServerSocketChannel .open(); serverSocketChannel.configureBlocking(false); serverSocketChannel.socket().bind(address); serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT); log.info("Server: socket server started!"); while (true) { int nKeys = selector.select(); if (nKeys > 0) { Set selectedKeys = selector.selectedKeys(); Iterator it = selectedKeys.iterator(); while (it.hasNext()) { SelectionKey key = (SelectionKey) it.next(); if (key.isAcceptable()) { log.info("Server: Selection key is acceptable"); handler.handleAccept(key); } else if (key.isReadable()) { log.info("Server: Selection key is readable"); handler.handleRead(key); } else if (key.isWritable()) { log.info("Server: Selection key is writable"); handler.handleWrite(key); } it.remove(); } } }

其中的handler就是處理每種類型的事件的類,舉個Read的例子:

public void handleRead(SelectionKey key) throws IOException { ByteBuffer byteBuffer = ByteBuffer.allocate(512); SocketChannel socketChannel = (SocketChannel) key .channel(); while (true) { int readBytes = socketChannel.read(byteBuffer); if (readBytes > 0) { log.info("Server: readBytes = " + readBytes); log.info("Server: data = " + new String(byteBuffer.array(), 0, readBytes)); byteBuffer.flip(); socketChannel.write(byteBuffer); break; } } socketChannel.close(); }

Client就比較簡單了,就是用channel直接發送了一句消息:

public void send(String data) { try { SocketChannel socketChannel = SocketChannel.open(inetSocketAddress); socketChannel.configureBlocking(false); ByteBuffer byteBuffer = ByteBuffer.allocate(512); socketChannel.write(ByteBuffer.wrap(data.getBytes())); while (true) { byteBuffer.clear(); int readBytes = socketChannel.read(byteBuffer); if (readBytes > 0) { byteBuffer.flip(); log.info("Client: readBytes = " + readBytes); log.info("Client: data = " + byteBuffer.toString()); socketChannel.close(); break; } } } catch (IOException e) { e.printStackTrace(); } }

最後,這些Java的socket編程在實際工程中已經不多使用了,因爲Unix的poll等功能的出現,select相比較之下,有點弱了。接下來能夠考慮研究下。

NIO包的API介紹:

http://wufan0023.iteye.com/blog/198722

http://wufan0023.iteye.com/blog/198710

    分享到:

This entry was posted in Java topic and tagged Javasocket by 柳浪聞鶯. Bookmark the permalink.

相關文章
相關標籤/搜索