TCP(Transmission Control Protocol)和UDP(User Datagram Protocol)是两种不同的传输层协议,用于在计算机网络中进行数据传输。以下是它们的主要区别:
连接性:
数据传输方式:
可靠性:
效率:
以下是Java中使用TCP和UDP进行简单通信的示例。
// 服务器端
import java.io.*;
import java.net.*;
public class TCPServer {
public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(12345);
System.out.println("Waiting for client...");
Socket clientSocket = serverSocket.accept();
System.out.println("Client connected.");
BufferedReader reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String message = reader.readLine();
System.out.println("Received from client: " + message);
PrintWriter writer = new PrintWriter(clientSocket.getOutputStream(), true);
writer.println("Hello from server!");
reader.close();
writer.close();
clientSocket.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 客户端
import java.io.*;
import java.net.*;
public class TCPClient {
public static void main(String[] args) {
try {
Socket socket = new Socket("localhost", 12345);
PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);
writer.println("Hello from client!");
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String response = reader.readLine();
System.out.println("Received from server: " + response);
writer.close();
reader.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 服务器端
import java.io.*;
import java.net.*;
public class UDPServer {
public static void main(String[] args) {
try {
DatagramSocket serverSocket = new DatagramSocket(9876);
System.out.println("Waiting for client...");
byte[] receiveData = new byte[1024];
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
String message = new String(receivePacket.getData(), 0, receivePacket.getLength());
System.out.println("Received from client: " + message);
InetAddress clientAddress = receivePacket.getAddress();
int clientPort = receivePacket.getPort();
byte[] sendData = "Hello from server!".getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, clientAddress, clientPort);
serverSocket.send(sendPacket);
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 客户端
import java.io.*;
import java.net.*;
public class UDPClient {
public static void main(String[] args) {
try {
DatagramSocket clientSocket = new DatagramSocket();
InetAddress serverAddress = InetAddress.getByName("localhost");
int serverPort = 9876;
String message = "Hello from client!";
byte[] sendData = message.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, serverAddress, serverPort);
clientSocket.send(sendPacket);
byte[] receiveData = new byte[1024];
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
clientSocket.receive(receivePacket);
String response = new String(receivePacket.getData(), 0, receivePacket.getLength());
System.out.println("Received from server: " + response);
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
上述示例分别演示了使用TCP和UDP进行简单通信的服务器端和客户端代码。在TCP示例中,使用Socket
和ServerSocket
进行连接,而在UDP示例中,使用DatagramSocket
和DatagramPacket
进行通信。