標籤:調用 sock rac 資料 輸入資料流 tput 結構 pre package
TCP(Transmission Control Protocol 傳輸控制通訊協定)是一種連線導向的、可靠的、基於位元組流的傳輸層通訊協定
UDP (User Datagram Protocol 使用者資料包通訊協定)是OSI(Open System Interconnection開放式系統互聯) 參考模型中一種不需連線的傳輸層協議,提供面向事務的簡單不可靠資訊傳送服務
TCP與UDP基本區別
- 1.基於串連與無串連
- 2.TCP要求系統資源較多,UDP較少;
- 3.UDP程式結構較簡單
- 4.流模式(TCP)與資料報模式(UDP);
- 5.TCP保證資料正確性,UDP可能丟包
- 6.TCP保證資料順序,UDP不保證
一、Tcp協議通訊
1、Server伺服器端:
- a、建立ServerSocket對象,同時綁定監聽連接埠
- b、通過accept()方法監聽用戶端的請求
- c、建立串連後,通過輸入資料流讀取用戶端發送的請求資訊
- d、通過輸出資料流向用戶端發送響應資訊
- e、關閉相應資源
package com.yyx.test;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.ServerSocket;import java.net.Socket;public class TcpServerSocket { public static void main(String[] args) { ServerSocket serverSocket = null; Socket socket = null; InputStream inputStream = null; OutputStream outputStream = null; FileOutputStream fileOutputStream = null; try { // 建立一個ServerSocket的對象,通過構造器指明自身的連接埠號碼 serverSocket = new ServerSocket(9090); // 服務端監聽 調用其accept()方法,返回一個Socket的對象 socket = serverSocket.accept(); inputStream = socket.getInputStream(); String pathname = "F:" + File.separator + "target.jpg"; File file = new File(pathname); fileOutputStream = new FileOutputStream(file); /* * 接收用戶端傳送檔案,並儲存到本地檔案 */ byte[] b = new byte[1024]; int len; while ((len = inputStream.read(b)) != -1) { fileOutputStream.write(b, 0, len); } String strInfo = "你發送的圖片我已接收成功!"; outputStream = socket.getOutputStream(); outputStream.write(strInfo.getBytes()); } catch (Exception e) { e.printStackTrace(); } finally { // 關閉相應的流以及Socket、ServerSocket的對象 if (outputStream != null) { try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (fileOutputStream != null) { try { fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (socket != null) { try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } if (serverSocket != null) { try { serverSocket.close(); } catch (IOException e) { e.printStackTrace(); } } } }}
2、Client用戶端:
- a、建立Socket對象,指明需要已連線的服務器的地址和連接埠號碼
- b、建立串連後,通過輸出資料流向伺服器端發送請求資訊
- c、通過輸入資料流擷取伺服器的響應資訊
- d、關閉相應資源
package com.yyx.test;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.InetAddress;import java.net.Socket;public class TcpClientSocket { public static void main(String[] args) { Socket socket = null; OutputStream outputStream = null; InputStream inputStream = null; FileInputStream fileInputStream=null; try { // 建立一個Socket的對象,通過構造器指明服務端的IP地址,以及其接收程式的連接埠號碼 socket = new Socket(InetAddress.getByName("127.0.0.1"), 9090); String pathname = "F:" + File.separator + "source.jpg"; File file = new File(pathname); fileInputStream=new FileInputStream(file); // getOutputStream():發送資料,方法返回OutputStream的對象 outputStream = socket.getOutputStream(); byte[] b = new byte[1024]; int len; while ((len = fileInputStream.read(b)) != -1) { outputStream.write(b, 0, len); } // shutdownOutput():執行此方法,顯式的告訴服務端發送完畢! socket.shutdownOutput(); inputStream = socket.getInputStream(); byte[] bAnother = new byte[1024]; int lenAnother; while ((lenAnother = inputStream.read(bAnother)) != -1) { String str = new String(bAnother, 0, lenAnother); System.out.print(str); } } catch (Exception e) { e.printStackTrace(); } finally { // 關閉相應的流和Socket對象,從後往前關閉 if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (outputStream != null) { try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (fileInputStream != null) { try { fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (socket != null) { try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } } }}
二、Udp協議通訊
用戶端
package com.yyx.test;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.InetAddress;public class UdpClientSocket { public static void main(String[] args) { DatagramSocket datagramSocket = null; try { datagramSocket = new DatagramSocket(); String str = "你好,我是要發送的資料"; byte[] b = str.getBytes(); /* * 建立一個資料報:每一個資料報不能大於64k,都記錄著資料資訊,發送端的IP、連接埠號碼,以及要發送到 的接收端的IP、連接埠號碼 */ DatagramPacket pack = new DatagramPacket(b, 0, b.length, InetAddress.getByName("127.0.0.1"), 9090); datagramSocket.send(pack); } catch (Exception e) { e.printStackTrace(); } finally { if (datagramSocket != null) { datagramSocket.close(); } } }}
服務端
package com.yyx.test;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.InetAddress;public class UdpServerSocket { public static void main(String[] args) { DatagramSocket datagramSocket = null; try { datagramSocket = new DatagramSocket(9090); byte[] b = new byte[1024]; DatagramPacket pack = new DatagramPacket(b, 0, b.length); datagramSocket.receive(pack); String str = new String(pack.getData(), 0, pack.getLength()); System.out.println(str); } catch (Exception e) { e.printStackTrace(); } finally { if (datagramSocket != null) { datagramSocket.close(); } } }}
Java網路編程Socket通訊