標籤:弊端 就是 方法 服務 標識 必須 輸出資料流 sys 緩衝
網路編程概述
- 網路編程 : 通過編程實現不同電腦間資料的共用
- C/S : 用戶端/伺服器模式
- 好處:部分代碼放在用戶端,訪問伺服器只需要傳輸有效資料.
- 弊端:必須安裝用戶端,並且伺服器升級,對應的用戶端必須升級.
- B/S : 瀏覽器/伺服器模式
- 好處:不需要安裝用戶端,不需要考慮伺服器升級問題.
- 弊端:所有的資料都在伺服器,網路不好,反應慢,不友好.
網路編程三要素
- 協議 : 電腦與電腦之間傳輸的規則(協議的三要素如下)
- 文法,就是這一段內容要符合一定的規則和格式。例如,括弧要成對,結束要使用分號等。
- 語義,就是這一段內容要代表某種意義。例如數字減去數字是有意義的,數字減去文本一般來說就沒有意義。
- 順序,就是先幹啥,後幹啥。例如,可以先加上某個數值,然後再減去某個數值。
- IP地址 : 在網路中,ip地址是唯一標識
- 連接埠號碼 : 電腦中應用程式的唯一標識
用戶端的代碼步驟
- 建立Socket的對象,指定ip地址和連接埠號碼
- 利用socket擷取檔案位元組輸出資料流對象(可以使用轉換流將位元組流轉化為列印流)
- 利用位元組流向服務端發送訊息
- 關閉socket資源
服務端的代碼步驟
- 建立ServerSocket對象,設定連接埠號碼
- 利用建立的ServerSocket對象使用sccept方法擷取Socket對象
- 利用第二步擷取的socket擷取位元組輸入資料流對象(可以只用轉換流將位元組流轉化為字元高效流)
- 使用第三步的流讀取用戶端發送的資訊
- 關閉資源
在整個過程中,需要注意的是隨時重新整理緩衝區和關閉流,所以推薦使用列印流,會自動重新整理和自動換行.
寫個小例子,實現用戶端和服務端的單次通訊
package homework.demo5;import java.io.*;import java.net.Socket;/** * 建立用戶端 * * @author WZLOVE * @create 2018-07-25 20:47 */public class ClientDemo { public static void main(String[] args) throws IOException { // 建立Socket對象 Socket socket = new Socket("localhost",8888); // 利用轉換流轉換成列印流 PrintWriter pw = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()),true); // 向伺服器寫內容 pw.println("你好,我是用戶端"); // 擷取高效字元輸入資料流 BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream())); // 擷取伺服器發送的訊息 String s = br.readLine(); System.out.println("伺服器說:" + s); // 關閉流 socket.close(); }}package homework.demo5;import java.io.*;import java.net.ServerSocket;import java.net.Socket;/** * 服務端 * * @author WZLOVE * @create 2018-07-25 20:57 */public class ServerDemo { public static void main(String[] args) throws IOException { // 建立服務端網路對象,指定連接埠號碼 ServerSocket ss = new ServerSocket(8888); // 擷取Socket對象 Socket accept = ss.accept(); // 擷取高效輸入資料流 BufferedReader br = new BufferedReader(new InputStreamReader(accept.getInputStream())); // 擷取輸入的資訊 String s = br.readLine(); System.out.println("用戶端說 : " + s); // 擷取列印流 PrintWriter pw = new PrintWriter(new OutputStreamWriter(accept.getOutputStream()),true ); // 向用戶端發送訊息 pw.println("你好,我是服務端"); // 關閉資源 accept.close(); ss.close(); }}
寫個例子,實現用戶端向伺服器上傳圖片
package homework.demo7;import java.io.*;import java.net.Socket;/** * 網路通訊中的用戶端 */public class Cilent { public static void main(String[] args) throws IOException { // 建立Socket用戶端對象 Socket socket = new Socket("localhost",8888); // 準備檔案進行傳輸 BufferedInputStream bis = new BufferedInputStream(new FileInputStream("yz.jpg")); // 使用socket對象擷取位元組輸入資料流對象 BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream()); // 一邊讀檔案,一邊向伺服器發送資料 byte[] arr = new byte[1024 * 5]; int len; while((len = bis.read(arr)) != -1){ bos.write(arr,0,len); } // 資料上傳完畢,重新整理緩衝區 bos.flush(); // 關閉不用的資源 bis.close(); // 關閉輸出資料流通訊端,這個步驟是必須的 socket.shutdownOutput(); // 讀取反饋的資料 // 使用socket對象擷取位元組輸入資料流 BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream())); String line; StringBuilder sb = new StringBuilder(); while((line = br.readLine()) != null){ sb.append(line); } System.out.println(sb); // 關閉資源 socket.close(); }}package homework.demo7;import java.io.*;import java.net.ServerSocket;import java.net.Socket;/** * 服務端 */public class Server { public static void main(String[] args) throws IOException { // 建立服務端的對象 ServerSocket ss = new ServerSocket(8888); // 使用服務端對象擷取Socket對象 Socket accept = ss.accept(); // 準備接受上傳的檔案並且寫入伺服器 // 建立高效位元組輸出資料流,用來指向檔案 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("test.jpg")); // 使用socket對象擷取檔案位元組輸入資料流(轉化為高效位元組輸入資料流) BufferedInputStream bis = new BufferedInputStream(accept.getInputStream()); // 邊讀邊寫 byte[] arr = new byte[1024 * 5]; int len; while((len = bis.read(arr)) != -1){ bos.write(arr,0,len); // 進行重新整理 bos.flush(); } // 關閉不使用的資源 bos.close(); // 向用戶端反饋資訊 PrintWriter pw = new PrintWriter(new OutputStreamWriter(accept.getOutputStream()),true); // 寫資訊 pw.println("上傳成功"); // 關閉資源 accept.close(); ss.close(); }}
有關網路的相關知識大家可以去訂閱極客時間上的趣談網路通訊協定,那個講的很清楚
Java之網路編程