這篇文章將為大家詳細(xì)講解有關(guān)Java中怎么實(shí)現(xiàn)BIO阻塞式網(wǎng)絡(luò)編程,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
創(chuàng)新互聯(lián)是專業(yè)的向陽(yáng)網(wǎng)站建設(shè)公司,向陽(yáng)接單;提供成都網(wǎng)站制作、成都做網(wǎng)站,網(wǎng)頁(yè)設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行向陽(yáng)網(wǎng)站開(kāi)發(fā)網(wǎng)頁(yè)制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛(ài)的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來(lái)合作!
阻塞(blocking)IO:阻塞是指結(jié)果返回之前,線程會(huì)被掛起,函數(shù)只有在得到結(jié)果之后(或超時(shí))才會(huì)返回
非阻塞(non-blocking)IO:非阻塞和阻塞的概念相對(duì)應(yīng),指在不能立刻得到結(jié)果之前,該函數(shù)不會(huì)阻塞當(dāng)前線程,而會(huì)立刻返回
同步(synchronous)IO:應(yīng)用阻塞在發(fā)送或接受數(shù)據(jù)的狀態(tài),直至數(shù)據(jù)成功傳輸(或返回失?。?,簡(jiǎn)單來(lái)說(shuō)就是必須一件一件事做,等前一件做完了才能做下一件事
異步(asynchronous)IO:應(yīng)用發(fā)送或接受數(shù)據(jù)后立即返回,實(shí)際處理這個(gè)調(diào)用的程序在完成后,通過(guò)狀態(tài)、通知和回調(diào)來(lái)通知調(diào)用者
阻塞和非阻塞是獲取資源的方式,同步和異步是程序如何處理資源的邏輯。
首先我們來(lái)看一段最基礎(chǔ)的Java網(wǎng)絡(luò)編程代碼示例:
服務(wù)器端代碼示例:
public class BIOServerV1 { public static void main(String[] args) throws Exception { ServerSocket serverSocket = new ServerSocket(8080); System.out.println("服務(wù)器啟動(dòng)成功"); while (!serverSocket.isClosed()) { Socket request = serverSocket.accept(); // 阻塞 System.out.println("收到新連接:" + request.toString()); try { InputStream inputStream = request.getInputStream(); // 獲取數(shù)據(jù)流 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "utf-8")); String message; while ((message = bufferedReader.readLine()) != null) { if (message.length() == 0) { break; } System.out.println("消息內(nèi)容為:" + message); } System.out.println("收到數(shù)據(jù),來(lái)自:" + request.toString()); } catch (IOException e) { e.printStackTrace(); } finally { request.close(); } } serverSocket.close(); } }
客戶端代碼示例:
public class BIOClient { public static void main(String[] args) throws IOException { Socket socket = new Socket("localhost", 8080); OutputStream outputStream = socket.getOutputStream(); Scanner scanner = new Scanner(System.in); System.out.println("請(qǐng)輸入:"); String message = scanner.nextLine(); outputStream.write(message.getBytes(Charset.forName("UTF-8"))); scanner.close(); socket.close(); } }
這個(gè)版本服務(wù)器端的代碼同一時(shí)刻只能支持一個(gè)網(wǎng)絡(luò)連接,在建立連接之后服務(wù)端線程會(huì)被阻塞,只有在已建立連接的客戶端處理完數(shù)據(jù)關(guān)閉連接之后,后續(xù)的連接請(qǐng)求才能一個(gè)一個(gè)的處理,而為了能并發(fā)的處理多個(gè)請(qǐng)求我們?cè)谙乱粋€(gè)版本中加入多線程的代碼。
public class BIOServerV2 { private static ExecutorService executorService = Executors.newCachedThreadPool(); public static void main(String[] args) throws IOException { ServerSocket serverSocket = new ServerSocket(8080); System.out.println("服務(wù)器啟動(dòng)成功"); while (!serverSocket.isClosed()) { Socket request = serverSocket.accept(); System.out.println("收到新連接:" + request.toString()); // 多線程接收多個(gè)連接 executorService.submit( () -> { try { InputStream inputStream = request.getInputStream(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "utf-8")); String message; while ((message = bufferedReader.readLine()) != null) { if (message.length() == 0) { break; } System.out.println("消息內(nèi)容為:" + message); } System.out.println("收到數(shù)據(jù),來(lái)自:" + request.toString()); } catch (IOException e) { e.printStackTrace(); } finally { try { request.close(); } catch (IOException e) { e.printStackTrace(); } } }); } serverSocket.close(); } }
這個(gè)版本的代碼在加入多線程后可以并發(fā)的處理多個(gè)連接,但是它只能處理Java客戶端的連接不能處理瀏覽器端的連接,而為了能與瀏覽器端交互我們需要了解HTTP協(xié)議的內(nèi)容。
HTTP協(xié)議請(qǐng)求數(shù)據(jù)包解析
HTTP協(xié)議響應(yīng)數(shù)據(jù)包解析
HTTP協(xié)議響應(yīng)狀態(tài)碼:
在了解了HTTP協(xié)議的內(nèi)容之后我們就可以依據(jù)HTTP協(xié)議的內(nèi)容編寫(xiě)程序來(lái)處理瀏覽器請(qǐng)求。在之前多線程版本的代碼之上我們需要對(duì)數(shù)據(jù)根據(jù)HTTP協(xié)議的內(nèi)容進(jìn)行處理,代碼示例如下:
public class BIOServerV3 { private static ExecutorService executorService = Executors.newCachedThreadPool(); public static void main(String[] args) throws IOException { ServerSocket serverSocket = new ServerSocket(8080); System.out.println("服務(wù)器啟動(dòng)成功"); while (!serverSocket.isClosed()) { Socket request = serverSocket.accept(); System.out.println("收到新連接:" + request.toString()); // 多線程接收多個(gè)連接 executorService.submit( () -> { try { InputStream inputStream = request.getInputStream(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "utf-8")); String message; while ((message = bufferedReader.readLine()) != null) { if (message.length() == 0) { break; } // 拿到消息后可以解析消息拿到請(qǐng)求方法,請(qǐng)求數(shù)據(jù)等內(nèi)容 System.out.println("消息內(nèi)容為:" + message); } System.out.println("收到數(shù)據(jù),來(lái)自:" + request.toString()); // 根據(jù)HTTP協(xié)議響應(yīng)數(shù)據(jù)包返回?cái)?shù)據(jù)給瀏覽器 OutputStream outputStream = request.getOutputStream(); outputStream.write("HTTP/1.1 200 OK\r\n".getBytes()); outputStream.write("Content-Length: 11\r\n\r\n".getBytes()); outputStream.write("Hello World".getBytes()); outputStream.flush(); } catch (IOException e) { e.printStackTrace(); } finally { try { request.close(); } catch (IOException e) { e.printStackTrace(); } } }); } serverSocket.close(); } }
關(guān)于Java中怎么實(shí)現(xiàn)BIO阻塞式網(wǎng)絡(luò)編程就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。