Java基础(14)UDP、TCP
=================
UDP(用户数据报协议)和TCP(传输控制协议)是互联网协议套件(TCP/IP)的两个核心协议,用于数据的传输。这两个协议在功能上是相互补充的,分别适用于不同的网络应用场景。
TCP(传输控制协议)
TCP是一种面向连接的、可靠的、基于字节流的传输层通信协议。它能确保数据包的顺序传输和数据的完整性,适用于对数据准确性要求高的应用,如Web浏览、文件传输、电子邮件等。
TCP的特点:
- 面向连接:通信双方在数据传送前,必须先建立连接。
- 数据传输可靠性:通过序号、确认应答、重传控制等机制保证数据的正确传输。
- 拥塞控制和流量控制:通过滑动窗口机制等控制数据的流量,避免网络拥塞。
TCP示例代码(Java)
服务器端示例:
import java.io.*;
import java.net.*;
public class TCPServer {
public static void main(String[] args) throws IOException {
int port = 6789;
ServerSocket welcomeSocket = new ServerSocket(port);
while (true) {
Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient =
new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
String clientSentence = inFromClient.readLine();
System.out.println("Received: " + clientSentence);
String capitalizedSentence = clientSentence.toUpperCase() + '\n';
outToClient.writeBytes(capitalizedSentence);
}
}
}
客户端示例:
import java.io.*;
import java.net.*;
public class TCPClient {
public static void main(String[] args) throws IOException {
String sentence;
String modifiedSentence;
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
Socket clientSocket = new Socket("hostname", 6789);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
sentence = inFromUser.readLine();
outToServer.writeBytes(sentence + '\n');
modifiedSentence = inFromServer.readLine();
System.out.println("FROM SERVER: " + modifiedSentence);
clientSocket.close();
}
}
UDP(用户数据报协议)
UDP是一种无连接的协议。与TCP不同,它不保证数据包的顺序或完整性,因此,它更适用于对实时性要求高的应用,如视频会议、在线游戏等。
UDP的特点:
- 无连接:发送数据之前不需要建立连接,减少了开销和发送数据之前的延迟。
- 效率高,结构简单:适用于广播和多播传输。
- 不保证可靠传输:不进行错误校验和修正,适用于对实时性要求较高的场景。
UDP示例代码(Java)
服务器端示例:
import java.io.*;
import java.net.*;
public class UDPServer {
public static void main(String[] args) throws IOException {
DatagramSocket serverSocket = new DatagramSocket(9876);
byte[] receiveData = new byte[1024];
byte[] sendData = new byte[1024];
while (true) {
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
String sentence = new String(receivePacket.getData(), 0, receivePacket.getLength());
System.out.println("RECEIVED: " + sentence);
InetAddress IPAddress = receivePacket.getAddress();
int port = receivePacket.getPort();
String capitalizedSentence = sentence.toUpperCase();
sendData = capitalizedSentence.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);
serverSocket.send(sendPacket);
}
}
}
客户端示例:
import java.io.*;
import java.net.*;
public class UDPClient {
public static void main(String[] args) throws IOException {
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
DatagramSocket clientSocket = new DatagramSocket();
InetAddress IPAddress = InetAddress.getByName("hostname");
byte[] sendData = new byte[1024];
byte[] receiveData = new byte[1024];
String sentence = inFromUser.readLine();
sendData = sentence.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
clientSocket.send(sendPacket);
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
clientSocket.receive(receivePacket);
String modifiedSentence = new String(receivePacket.getData(), 0, receivePacket.getLength());
System.out.println("FROM SERVER:" + modifiedSentence);
clientSocket.close();
}
}
总结
TCP和UDP各有优缺点,适用于不同的网络应用场景。TCP提供了一种可靠的、面向连接的通信方式,适用于对数据顺序和完整性要求高的应用。UDP则提供了一种无连接的、尽最大努力交付的通信方式,适用于对实时性要求较高的场景。在实际应用中,选择哪种协议取决于应用的具体需求和网络环境的特点。
原文链接: https://juejin.cn/post/7364418751481085986
文章收集整理于网络,请勿商用,仅供个人学习使用,如有侵权,请联系作者删除,如若转载,请注明出处:http://www.cxyroad.com/17819.html