Java網絡編程之TCP協議數據傳輸

一、TCP協議概述

TCP協議是相對於UDP比較穩定的傳輸協議,這裏存在三次握手,來確保連接狀態,同時有明確的客戶端和服務端之分。
TCP服務中需要服務器端先啓動,需要監聽指定端口,等待客戶端的連接。
客戶端主動連接服務器,和服務器連接之後,纔可以進行數據交互,服務器不能主動連接客戶端。

對於TCP操作而言,Java中提供了兩個Socket

1. 服務端Socket 
	java.net.ServerSocket;
	創建對應的ServerScoket開啓服務器,等待客戶端連接
2. 客戶端Socket
	java.net.Socket
	創建客戶端Scoket,並且連接服務器,同時將Socket發送給服務器綁定註冊。

二、Socket 客戶端

給客戶端提供數據傳輸的符合TCP/IP要求的Socket對象

1、構造方法 Constructor
Socket(String host, int port);
	host是服務器IP地址,port對應服務器程序的端口號
	通過指定的服務器IP地址和端口號,獲取TCP連接對象
2、成員方法 Method
InputStream getInputStream();
	獲取Socket對象輸入字節流,可以從服務器獲取對應的數據
	InputStream是一個資源,需要在程序退出時關閉
	
OutputStream getOutputStream();
	獲取Sokcet對象輸出字節流,可以發送數據到服務器
	OutputStream是一個資源,需要在程序退出時關閉

void close();
	關閉客戶端Socket

void shutdownOutput();
	禁止當前Socket發送數據

TCP/IP協議對應的Socket是基於IO流實現的。

三、ServerSocket 服務器端

在服務端開啓Socket服務器

1、構造方法 Constructor
ServerSocket(int port);
	開啓ServerSocket服務器,並且明確當前的服務端口
2、成員方法 Method
Socket accept();
	監聽並且連接,得到一個Socket對象,同時該方法是一個阻塞方法,會處於一個始終
	的監聽狀態。
	返回的是Socket,也就是客戶端Socket對象,獲取到當前Socket對象,相對於獲取到
	客戶端連接,同時使用的Socket和客戶端一致。

四、TCP協議代碼演示

1、服務器端流程
1. 創建ServerSocket服務器,同時監聽指定端口
2. 通過accept方法獲取Socket連接,得到客戶端Socket對象
3. 通過Socket對象,獲取InputStream,讀取客戶端發送數據
4. 通過Socket對象,獲取OutputStream,發送數據給客戶端
5. 關閉服務
public class TcpServer {
	public static void main(String[] args) throws IOException {
		System.out.println("服務器已啓動");
		// 1. 創建ServerSocket服務器,同時監聽指定端口
		ServerSocket serverSocket = new ServerSocket(6666);
	
		// 2. 通過accept方法獲取Socket連接,得到客戶端Socket對象
		Socket socket = serverSocket.accept();
		
		// 3. 通過Socket對象,獲取InputStream,讀取客戶端發送數據
		InputStream inputStream = socket.getInputStream();
		
		// IO流操作
		byte[] buf = new byte[1024];
		int length = inputStream.read(buf);
		System.out.println(new String(buf, 0, length));
        
		// 4. 通過Socket對象,獲取OutputStream,發送數據給客戶端
		OutputStream outputStream = socket.getOutputStream();
		String str = "這是來自服務器端的數據";
		
		outputStream.write(str.getBytes());
		
		// 5. 關閉Socket服務 同時關閉當前Socket使用的輸入字節流和輸出字節流	
		socket.close();	
	}
}
2、客戶端流程
1. 創建Socket服務,同時明確連接服務器的IP地址和對應端口號
2. 通過Socket對象,獲取對應的OutputStream對象,發送數據給服務器
3. 通過Socket對象,獲取對應的InputStream對象,接收服務器發送數據
4. 關閉服務
public class TcpClient {
	public static void main(String[] args) 
		throws UnknownHostException, IOException {
		System.out.println("客戶端已啓動");
		
		// 1. 創建Socket服務,同時明確連接服務器的IP地址和對應端口號
		Socket socket = new Socket("192.168.246.13", 6666);
		
		// 2. 通過Socket對象,獲取對應的OutputStream對象,發送數據給服務器
		OutputStream outputStream = socket.getOutputStream();
		
		outputStream.write("服務器,你好!".getBytes());
		
		// 3. 通過Socket對象,獲取對應的InputStream對象,接收服務器發送數據
		InputStream inputStream = socket.getInputStream();
		
		byte[] buf = new byte[1024];
		int length = inputStream.read(buf);
		System.out.println(new String(buf, 0, length));
		
		// 4. 關閉服務
		socket.close();
	}
}

五、實現文件上傳操作

1、過程分析

文件上傳流程圖

2、客戶端程序
1. 創建對應文件的輸入字節流操作,這裏可以使用緩衝
2. 啓動Socket,
3. 獲取Socket輸出OutputStream對象,發送數據給服務器
4. 邊讀邊發
5. 當文件讀取結束,發送完畢,關閉客戶端
public class TcpClient {
	public static void main(String[] args) throws IOException {
		// 1. 創建對應文件的輸入字節流操作,這裏可以使用緩衝
		BufferedInputStream bis = new BufferedInputStream(
				new FileInputStream(new File("D:/1.mp4")));
		
		// 2. 啓動Socket
		Socket socket = new Socket(InetAddress.getLocalHost().getHostAddress(), 6666);
		
		// 3. 獲取Socket輸出OutputStream對象,發送數據給服務器
		OutputStream outputStream = socket.getOutputStream();
		
		int length = -1;
		byte[] buf = new byte[1024 * 8];
		
		
		// 4. 讀取數據,發送數據
		while ((length = bis.read(buf)) != -1) {
			outputStream.write(buf, 0, length);
		}
		
		// 5. 關閉資源
		socket.close();
		bis.close();
	}
}
3、服務端程序
1. 開啓服務端服務,創建ServerSocket對象
2. 明確保存文件的位置,創建對應文件夾的輸出緩衝字節流
3. 讀取數據,寫入文件
4. 關閉服務器
public class TcpServer {
	public static void main(String[] args) throws IOException {
		// 1. 開啓服務端服務,創建ServerSocket對象
		ServerSocket serverSocket = new ServerSocket(6666);
		
		Socket socket = serverSocket.accept();
		
		// 2. 明確保存文件的位置,創建對應文件夾的輸出緩衝字節流
		BufferedOutputStream bos = new BufferedOutputStream(
				new FileOutputStream(
						new File("D:/temp.mp4")));
		
		// 3. 獲取Socket對應的輸入流
		InputStream inputStream = socket.getInputStream();
		
		// 4. 邊讀邊寫
		int length = -1;
		byte[] buf = new byte[1024 * 8];
		
		while ((length = inputStream.read(buf)) != -1) {
			bos.write(buf, 0, length);
		}
		
		// 5. 關閉資源
		bos.close();
		socket.close();
	}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章