志宇-BIO
BIO
第一次寫博客
介紹
BIO是同步阻塞性IO,傳輸數據使用的流會阻塞效率較慢,同時ServerSocket中的accept方法和socket中的getInputStream/read方法會發生阻塞,也就是接受請求和接收數據會阻塞;
如果使用BIO解決高并發問題必須要引入多線程環境;
一個客戶端要創建一個線程,當有大量的請求時但不進行數據傳輸會浪費資源;
客戶端代碼
package com.sinosoft; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; import java.util.Scanner;public class BIOClientDemo {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);Socket socket = null;OutputStream outputStream = null;InputStream inputStream = null;try {socket = new Socket("127.0.0.1", 8080);//客戶端阻塞沒有關系//向客戶端發送數據outputStream = socket.getOutputStream();//阻塞服務端接收請求//String line = scanner.nextLine();outputStream.write("request content.".getBytes("utf-8"));outputStream.flush();socket.shutdownOutput();//接收客戶端返回數據inputStream = socket.getInputStream();byte[] bytes = new byte[1024];StringBuilder response = new StringBuilder();int length;if ((length = inputStream.read(bytes)) != -1) {response.append(new String(bytes, 0, length));}System.out.println("response:" + response.toString());} catch (IOException e) {e.printStackTrace();} finally {if (inputStream != null) {try {inputStream.close();} catch (IOException e) {e.printStackTrace();}}if (outputStream != null) {try {outputStream.close();} catch (IOException e) {e.printStackTrace();}}if (socket != null) {try {socket.close();} catch (IOException e) {e.printStackTrace();}}}} }服務端代碼
package com.sinosoft;import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.ServerSocket; import java.net.Socket;public class BIOServerDemo{public static void main(String[] args) {ServerSocket server = null;try {server = new ServerSocket(8080);while (true) {//接收socket請求 這句話會阻塞Socket socket = server.accept();//每當接收到一個請求則創建一個線程new Thread(new ServerHandler(socket)).start();}} catch (IOException e) {e.printStackTrace();} finally {if (server != null) {try {server.close();} catch (IOException e) {e.printStackTrace();}}}}public static class ServerHandler implements Runnable {private Socket socket;public ServerHandler(Socket socket) {this.socket = socket;}@Overridepublic void run() {BufferedReader bufferedReader = null;BufferedWriter bufferedWriter = null;try {//要等待服務端發送數據這時會阻塞//使用流進行接收數據也會阻塞bufferedReader = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));StringBuilder requestStr = new StringBuilder();String line;while ((line = bufferedReader.readLine()) != null) {requestStr.append(line);}System.out.println("request:" + requestStr.toString());socket.shutdownInput();//發送數據bufferedWriter = new BufferedWriter(new OutputStreamWriter(this.socket.getOutputStream()));bufferedWriter.write("response content.");bufferedWriter.flush();} catch (IOException e) {e.printStackTrace();} finally {if (bufferedReader != null) {try {bufferedReader.close();} catch (IOException e) {e.printStackTrace();}}if (bufferedWriter != null) {try {bufferedWriter.close();} catch (IOException e) {e.printStackTrace();}}if (socket != null) {try {socket.close();} catch (IOException e) {e.printStackTrace();}}}}} }可以使用telnet模擬客戶端
使用telnet進行連接
點擊ctrl+],即可發送數據
總結
- 上一篇: 精髓,深入浅出剖析11个Zookeepe
- 下一篇: Linux显示文件目录分屏显示,Linu