一、網絡通信
TCP是一種可靠的傳輸協議,它保證了數據的可靠傳輸。在網絡通信中,TCP協議被廣泛應用。例如,電子郵件、FTP文件傳輸、HTTP網頁瀏覽等都是基於TCP/IP協議的。
以下是一個基於TCP協議的簡單聊天室服務器和客戶端代碼示例:
// 服務器端代碼
import java.io.*;
import java.net.*;
public class ChatServer {
public static void main(String[] args) {
try {
ServerSocket ss = new ServerSocket(8080); // 創建服務器套接字
System.out.println("服務器已經啟動...");
while (true) {
Socket s = ss.accept(); // 接收客戶端套接字
System.out.println("客戶端已經連接...");
InputStream is = s.getInputStream(); // 獲取輸入流
OutputStream os = s.getOutputStream(); // 獲取輸出流
BufferedReader br = new BufferedReader(new InputStreamReader(is)); // 讀取客戶端發送的信息
PrintWriter pw = new PrintWriter(new OutputStreamWriter(os), true); // 將服務端消息回傳給客戶端
pw.println("歡迎來到聊天室!");
while (true) {
String str = br.readLine();
if (str.equals("end")) {
break;
}
System.out.println("客戶端說:" + str);
pw.println("服務器說:" + str);
}
s.close(); // 關閉客戶端套接字
System.out.println("客戶端已經關閉...");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 客戶端代碼
import java.io.*;
import java.net.*;
public class ChatClient {
public static void main(String[] args) {
try {
Socket s = new Socket("127.0.0.1", 8080); // 創建客戶端套接字並連接服務器
InputStream is = s.getInputStream(); // 獲取輸入流
OutputStream os = s.getOutputStream(); // 獲取輸出流
BufferedReader br = new BufferedReader(new InputStreamReader(is)); // 讀取服務端發送的信息
PrintWriter pw = new PrintWriter(new OutputStreamWriter(os), true); // 將客戶端消息發送給服務端
while (true) {
String str = br.readLine();
if (str == null) {
break;
}
System.out.println(str);
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
pw.println(reader.readLine());
}
s.close(); // 關閉客戶端套接字
} catch (IOException e) {
e.printStackTrace();
}
}
}
二、文件傳輸
TCP協議可以保證數據的可靠傳輸,因此在文件傳輸的場景下,TCP協議也是很常用的。例如,在FTP文件傳輸、P2P文件共享等應用中,都廣泛使用了TCP協議。
以下是一個基於TCP協議的文件傳輸服務器和客戶端代碼示例:
// 服務器端代碼
import java.io.*;
import java.net.*;
public class FileServer {
public static void main(String[] args) {
try {
ServerSocket ss = new ServerSocket(8080); // 創建服務器套接字
System.out.println("服務器已經啟動...");
while (true) {
Socket s = ss.accept(); // 接收客戶端套接字
System.out.println("客戶端已經連接...");
InputStream is = s.getInputStream(); // 獲取輸入流
OutputStream os = s.getOutputStream(); // 獲取輸出流
BufferedInputStream bis = new BufferedInputStream(is); // 創建輸入緩衝流
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("file.txt")); // 創建輸出緩衝流
byte[] buffer = new byte[1024];
int len;
while ((len = bis.read(buffer, 0, buffer.length)) != -1) {
bos.write(buffer, 0, len);
}
bos.flush();
bos.close();
bis.close();
s.close(); // 關閉客戶端套接字
System.out.println("客戶端已經關閉...");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 客戶端代碼
import java.io.*;
import java.net.*;
public class FileClient {
public static void main(String[] args) {
try {
Socket s = new Socket("127.0.0.1", 8080); // 創建客戶端套接字並連接服務器
InputStream is = s.getInputStream(); // 獲取輸入流
OutputStream os = s.getOutputStream(); // 獲取輸出流
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("file.txt")); // 創建輸入緩衝流
BufferedOutputStream bos = new BufferedOutputStream(os); // 創建輸出緩衝流
byte[] buffer = new byte[1024];
int len;
while ((len = bis.read(buffer, 0, buffer.length)) != -1) {
bos.write(buffer, 0, len);
}
bos.flush();
bos.close();
bis.close();
s.close(); // 關閉客戶端套接字
} catch (IOException e) {
e.printStackTrace();
}
}
}
三、遠程控制
在遠程控制場景下,TCP協議也是非常重要的。例如,在遠程桌面控制、遠程維護等應用中,都需要使用TCP協議。
以下是一個基於TCP協議的遠程控制服務器和客戶端代碼示例:
// 服務器端代碼
import java.awt.*;
import java.io.*;
import java.net.*;
public class RemoteServer {
public static void main(String[] args) {
try {
ServerSocket ss = new ServerSocket(8080); // 創建服務器套接字
System.out.println("服務器已經啟動...");
while (true) {
Socket s = ss.accept(); // 接收客戶端套接字
System.out.println("客戶端已經連接...");
OutputStream os = s.getOutputStream();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Robot robot = new Robot();
int width = (int) screenSize.getWidth();
int height = (int) screenSize.getHeight();
Rectangle screenRect = new Rectangle(0, 0, width, height);
BufferedImage image = robot.createScreenCapture(screenRect);
ImageIO.write(image, "jpg", os);
os.flush();
os.close();
s.close(); // 關閉客戶端套接字
System.out.println("客戶端已經關閉...");
}
} catch (IOException | AWTException e) {
e.printStackTrace();
}
}
}
// 客戶端代碼
import java.awt.*;
import java.io.*;
import java.net.*;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class RemoteClient {
public static void main(String[] args) {
try {
Socket s = new Socket("127.0.0.1", 8080); // 創建客戶端套接字並連接服務器
InputStream is = s.getInputStream(); // 獲取輸入流
byte[] buffer = new byte[1024];
BufferedImage image = ImageIO.read(is);
ImageIcon icon = new ImageIcon(image);
JFrame frame = new JFrame("遠程監控");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new JLabel(icon));
frame.pack();
frame.setVisible(true);
is.close();
s.close(); // 關閉客戶端套接字
} catch (IOException e) {
e.printStackTrace();
}
}
}
四、音視頻傳輸
TCP協議在音視頻傳輸場景下並不常用,因為它的可靠性會對音視頻傳輸的實時性產生影響。在音視頻傳輸場景下,一般會選擇UDP協議,以獲得更好的實時性。
以下是一個基於TCP協議的音視頻傳輸服務器和客戶端代碼示例:
// 服務器端代碼
import java.io.*;
import java.net.*;
public class AVServer {
public static void main(String[] args) {
try {
ServerSocket ss = new ServerSocket(8080); // 創建服務器套接字
System.out.println("服務器已經啟動...");
while (true) {
Socket s = ss.accept(); // 接收客戶端套接字
System.out.println("客戶端已經連接...");
InputStream is = s.getInputStream(); // 獲取輸入流
OutputStream os = s.getOutputStream(); // 獲取輸出流
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer, 0, buffer.length)) != -1) {
os.write(buffer, 0, len);
os.flush();
}
os.close();
is.close();
s.close(); // 關閉客戶端套接字
System.out.println("客戶端已經關閉...");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 客戶端代碼
import java.io.*;
import java.net.*;
public class AVClient {
public static void main(String[] args) {
try {
Socket s = new Socket("127.0.0.1", 8080); // 創建客戶端套接字並連接服務器
InputStream is = new FileInputStream("av.mp4"); // 獲取輸入流
OutputStream os = s.getOutputStream(); // 獲取輸出流
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer, 0, buffer.length)) != -1) {
os.write(buffer, 0, len); // 寫入數據
os.flush();
}
os.close();
is.close();
s.close(); // 關閉客戶端套接字
} catch (IOException e) {
e.printStackTrace();
}
}
}
原創文章,作者:JOVZJ,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/370284.html
微信掃一掃
支付寶掃一掃