基於TCP的網路資料傳輸測試(使用騰訊雲)
零、前言:
在騰訊雲上開啟服務,然後本地計算機去連線,以此測試TCP連線
這是java伺服器端最底層的原理
實現場景1:客戶端(本機)輸入一個字串,服務端返回相應的大寫字母
實現場景2:一個客戶端(本機)上傳檔案到伺服器,然後通過瀏覽器訪問
實現場景3:多個客戶端(本機)同時上傳檔案到伺服器(併發)
1.在伺服器上有java環境
2.伺服器上開放了測試使用的介面:本測試為:8080埠
3.如果沒有伺服器,開兩個cmd,本地也可以測試
實現場景1

tcp連線.png
實現場景2:

上傳圖片.png
一、實現場景1
1、服務端實現:
獲取socket-->通過socket獲取讀流I--> 通過socket獲取寫流O-->I讀取後轉為大寫O輸出
import java.io.*; import java.net.ServerSocket; import java.net.Socket; /** * 作者:張風捷特烈 * 時間:2018/10/8 0008:10:19 * 郵箱:[email protected] */ public class TransServer { public static void main(String[] args) { try { ServerSocket serverSocket = new ServerSocket(8080); Socket socket = serverSocket.accept(); String ip = socket.getInetAddress().getHostAddress(); System.out.println(ip + "....connected"); //讀取socket讀取流中的資料。 BufferedReader brIn = new BufferedReader(new InputStreamReader(socket.getInputStream())); //目的。socket輸出流。將大寫資料寫入到socket輸出流,併發送給客戶端。 PrintWriter pwOut = new PrintWriter(socket.getOutputStream(), true); String line = null; while ((line = brIn.readLine()) != null) { pwOut.println(line.toUpperCase()); System.out.println(line.toUpperCase()); } serverSocket.close(); socket.close(); } catch (IOException e) { e.printStackTrace(); } } }
2.執行服務端
編譯 javac TransServer.java -encoding utf-8 執行:此時會進入等待 java TransServer
3.客戶端的實現
建立服務-->獲取鍵盤錄入--> 將資料發給服務端-->
獲取服務端返回的大寫資料--> 結束,關資源-->
public class TransClient { public static void main(String[] args) { String ip = "193.112.165.148"; int port = 8080; try { Socket socket = new Socket(ip, port); //定義讀取鍵盤資料的流物件。 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); //定義目的,將資料寫入到socket輸出流。發給服務端。 PrintWriter pwOut = new PrintWriter(socket.getOutputStream(), true); //定義一個socket讀取流,讀取服務端返回的大寫資訊。 BufferedReader brIn = new BufferedReader(new InputStreamReader(socket.getInputStream())); String line = null; while ((line = br.readLine()) != null) { if ("over".equals(line)) { break; } pwOut.println(line); System.out.println("服務端:" + brIn.readLine()); } br.close(); socket.close(); } catch (IOException e) { e.printStackTrace(); } } }
tcp連線.png

tcp連線.png
二、檔案上傳
1.伺服器端
import java.io.*; import java.net.ServerSocket; import java.net.Socket; /** * 作者:張風捷特烈 * 時間:2018/10/8 0008:11:50 * 郵箱:[email protected] * 說明:伺服器端 */ public class UpLoadFileServer { public static void main(String[] args) { try { ServerSocket serverSocket = new ServerSocket(8080); while (true) { Socket socket = serverSocket.accept(); String ip = socket.getInetAddress().getHostAddress(); System.out.println(ip + "....connected"); InputStream is = socket.getInputStream(); String fileName = "F:\\ds.jpg"; FileOutputStream fos = new FileOutputStream(fileName); int len = 0; byte[] buf = new byte[1024]; while ((len = is.read(buf)) != -1) { fos.write(buf, 0, len); } OutputStream os = socket.getOutputStream(); os.write("OK".getBytes()); fos.close(); socket.close(); } } catch (IOException e) { e.printStackTrace(); } } /** * 獲取範圍隨機整數:如 rangeInt(1,9) * * @param s 前數(包括) * @param e 後數(包括) * @return 範圍隨機整數 */ public static int rangeInt(int s, int e) { int max = Math.max(s, e); int min = Math.min(s, e) - 1; return (int) (min + Math.ceil(Math.random() * (max - min))); } }
2.執行服務端
編譯 javac UpLoadFileServer.java -encoding utf-8 執行:此時會進入等待 java UpLoadFileServer
3.客戶端:
public class UpLoadFileClient { public static void main(String[] args) { String ip = "193.112.165.148"; int port = 8080; try { Socket socket = new Socket(ip, port); String path = "C:\\Users\\Administrator\\Desktop\\資料結構.jpg"; FileInputStream fis = new FileInputStream(path); OutputStream os = socket.getOutputStream(); byte[] buf = new byte[1024]; int len = 0; while ((len = fis.read(buf)) != -1) { os.write(buf, 0, len); } //告訴服務端資料已寫完 socket.shutdownOutput(); InputStream is = socket.getInputStream(); byte[] bufIn = new byte[1024]; int num = is.read(bufIn); System.out.println(new String(bufIn, 0, num)); fis.close(); is.close(); } catch (Exception e) { e.printStackTrace(); } } }

上傳圖片.png
訪問: ofollow,noindex">http://www.toly1994.com:8080/imgs/ds.jpg

結果.png
4.考慮併發:
按照上面的程式碼,每次只能有一個人上傳,後者等待,顯然是不合理的,應該多個人可以併發執行。
這裡使用多執行緒,每次使用者連線都開啟一個執行緒來執行帶程式碼。
/** * 作者:張風捷特烈 * 時間:2018/10/8 0008:11:50 * 郵箱:[email protected] * 說明:併發上傳 */ public class UpLoadFileServerCur { public static void main(String[] args) { try { ServerSocket serverSocket = new ServerSocket(8080); while (true) { new Thread(new FileThread(serverSocket.accept())).start(); } } catch (IOException e) { e.printStackTrace(); } } } class FileThread implements Runnable { private Socket mSocket; public FileThread(Socket socket) { mSocket = socket; } @Override public void run() { String ip = mSocket.getInetAddress().getHostAddress(); System.out.println(ip + "....connected"); try { InputStream is = mSocket.getInputStream(); String fileName = "F:\\ip" + ip + "-" + rangeInt(3000, 10000) + ".jpg"; FileOutputStream fos = new FileOutputStream(fileName); int len = 0; byte[] buf = new byte[1024]; while ((len = is.read(buf)) != -1) { fos.write(buf, 0, len); } OutputStream os = mSocket.getOutputStream(); os.write("上傳成功".getBytes()); fos.close(); mSocket.close(); } catch (IOException e) { e.printStackTrace(); } } /** * 獲取範圍隨機整數:如 rangeInt(1,9) * * @param s 前數(包括) * @param e 後數(包括) * @return 範圍隨機整數 */ public static int rangeInt(int s, int e) { int max = Math.max(s, e); int min = Math.min(s, e) - 1; return (int) (min + Math.ceil(Math.random() * (max - min))); } }
後記:捷文規範
1.本文成長記錄及勘誤表
專案原始碼 | 日期 | 備註 |
---|---|---|
V0.1--無 | 2018-10-2 | 基於TCP的網路資料傳輸測試(使用騰訊雲) |
V0.2--無 | - | - |
2.更多關於我
筆名 | 微信 | 愛好 | |
---|---|---|---|
張風捷特烈 | 1981462002 | zdl1994328 | 語言 |
我的github | 我的簡書 | 我的CSDN | 個人網站 |
3.宣告
1----本文由張風捷特烈原創,轉載請註明
2----歡迎廣大程式設計愛好者共同交流
3----個人能力有限,如有不正之處歡迎大家批評指證,必定虛心改正
4----看到這裡,我在此感謝你的喜歡與支援