您好,登錄后才能下訂單哦!
利用Java怎么樣實現網絡通信功能?相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。
方式一:同步阻塞方式(BIO):
服務器端(Server):
package com.ietree.basicskill.socket.mode1; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; /** * 服務端 */ public class Server { // 端口號 final static int PORT = 8765; public static void main(String[] args) { ServerSocket server = null; try { server = new ServerSocket(PORT); System.out.println("Server start......"); // 進行阻塞 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(); } } server = null; } } }
采用多線程來處理接收到的請求(ServerHandler):
package com.ietree.basicskill.socket.mode1; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; public class ServerHandler implements Runnable { private Socket socket; public ServerHandler(Socket socket) { this.socket = socket; } @Override public void run() { BufferedReader in = null; PrintWriter out = null; try { in = new BufferedReader(new InputStreamReader(this.socket.getInputStream())); out = new PrintWriter(this.socket.getOutputStream(), true); String body = null; while (true) { body = in.readLine(); if(body == null){ break; } System.out.println("Server: " + body); out.println("服務器端回送響應的數據。"); } } catch (Exception e) { e.printStackTrace(); } finally { if(in != null){ try { in.close(); } catch (IOException e) { e.printStackTrace(); } } if(out != null){ try { out.close(); } catch (Exception e) { e.printStackTrace(); } } if(socket != null){ try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } socket = null; } } }
客戶端(Client):
package com.ietree.basicskill.socket.mode1; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; /** * 客戶端 */ public class Client { final static String ADDRESS = "127.0.0.1"; final static int PORT = 8765; public static void main(String[] args) { Socket socket = null; BufferedReader in = null; PrintWriter out = null; try { socket = new Socket(ADDRESS, PORT); in = new BufferedReader(new InputStreamReader(socket.getInputStream())); out = new PrintWriter(socket.getOutputStream(), true); // 向服務器端發送數據 out.println("接收到客戶端的請求數據......"); String response = in.readLine(); System.out.println("Client: " + response); } catch (Exception e) { e.printStackTrace(); } finally { if(in != null){ try { in.close(); } catch (IOException e) { e.printStackTrace(); } } if(out != null){ try { out.close(); } catch (Exception e) { e.printStackTrace(); } } if(socket != null){ try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } socket = null; } } }
程序輸出:
Server:
Server start...... Server: 接收到客戶端的請求數據......
Client:
Client: 服務器端回送響應的數據。
同步非阻塞(NIO)
異步非阻塞(AIO)
看完上述內容,你們掌握利用Java怎么樣實現網絡通信功能的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。