Skip to content

UDP

UDP 简介 (Introduction to UDP)

什么是 UDP?(What is UDP?)

  • 描述 (Description): UDP 是传输层协议之一,与 TCP(Transmission Control Protocol,传输控制协议)并列。与 TCP 不同,UDP 是一种无连接、面向消息的协议,不提供可靠性、顺序保证或流量控制。它适用于那些需要快速传输但可以容忍数据丢失的应用。
  • Explanation: UDP is one of the transport layer protocols, alongside TCP (Transmission Control Protocol). Unlike TCP, UDP is a connectionless, message-oriented protocol that does not provide reliability, order guarantee, or flow control. It is suitable for applications that require fast transmission and can tolerate data loss.

UDP 的特性 (Features of UDP)

  1. 无连接 (Connectionless)
  2. 描述 (Description): UDP 是无连接的,这意味着它不需要在发送数据之前建立连接。每个数据包(称为数据报)都是独立发送的,接收方可以独立处理每个数据报。
  3. Explanation: UDP is connectionless, meaning it does not require a connection to be established before data is sent. Each packet (called a datagram) is sent independently, and the receiver can process each datagram independently.

  4. 不可靠传输 (Unreliable Transmission)

  5. 描述 (Description): UDP 不保证数据的可靠传输。它不会检测数据包丢失或乱序,也不提供重传机制。应用层需要自行处理可能发生的数据丢失或乱序问题。
  6. Explanation: UDP does not guarantee reliable data transmission. It does not detect packet loss or out-of-order delivery, nor does it provide a retransmission mechanism. The application layer must handle any potential data loss or out-of-order issues.

  7. 快速传输 (Fast Transmission)

  8. 描述 (Description): 由于 UDP 头部简单,且不需要维护连接状态,UDP 可以提供比 TCP 更快的传输速度。它非常适合需要低延迟的应用,如实时音视频通信和在线游戏。
  9. Explanation: Due to its simple header and the lack of connection state maintenance, UDP can offer faster transmission speeds than TCP. It is ideal for applications that require low latency, such as real-time audio and video communication and online gaming.

  10. 面向消息 (Message-Oriented)

  11. 描述 (Description): UDP 保留了应用层消息的边界,一个数据报对应应用层发送的一个消息。这与 TCP 的流模式不同,TCP 将数据视为一个连续的字节流。
  12. Explanation: UDP preserves the message boundaries of the application layer; one datagram corresponds to one message sent by the application layer. This is different from TCP's stream-oriented approach, where data is treated as a continuous byte stream.

  13. 支持多播和广播 (Supports Multicast and Broadcast)

  14. 描述 (Description): UDP 支持多播(将数据发送到多个接收者)和广播(将数据发送到网络中所有的接收者)。这使得 UDP 非常适合需要向多个终端发送同一数据的应用场景。
  15. Explanation: UDP supports multicast (sending data to multiple recipients) and broadcast (sending data to all recipients on the network). This makes UDP well-suited for applications that need to send the same data to multiple endpoints.

UDP 的应用场景 (Use Cases of UDP)

  1. 实时音视频传输 (Real-Time Audio and Video Transmission)
  2. 描述 (Description): 在视频会议、IP 语音(VoIP)等应用中,低延迟比可靠性更为重要。UDP 的快速传输特性使其成为这些实时应用的首选。
  3. Explanation: In applications like video conferencing and Voice over IP (VoIP), low latency is more important than reliability. UDP's fast transmission makes it the preferred choice for these real-time applications.

  4. 在线游戏 (Online Gaming)

  5. 描述 (Description): 在线游戏需要快速响应用户输入,而不必每次都等待可靠的传输。因此,许多在线游戏使用 UDP 进行通信,以减少延迟。
  6. Explanation: Online games require quick responses to user inputs without the need to wait for reliable transmission. Therefore, many online games use UDP for communication to reduce latency.

  7. DNS 查询 (DNS Queries)

  8. 描述 (Description): DNS(Domain Name System)查询通常使用 UDP 传输,因为 DNS 请求和响应数据量较小,且对速度要求较高。尽管 DNS 有时使用 TCP,但大多数情况下都依赖 UDP。
  9. Explanation: DNS (Domain Name System) queries typically use UDP because DNS requests and responses are small in size and require high speed. While DNS sometimes uses TCP, it mostly relies on UDP.

  10. 视频流 (Video Streaming)

  11. 描述 (Description): 视频流媒体服务(如 YouTube、Netflix)常常使用 UDP 来减少延迟,确保视频播放的连续性。尽管 TCP 也用于视频流(特别是在需要可靠传输时),但 UDP 在低延迟场景中更受欢迎。
  12. Explanation: Video streaming services like YouTube and Netflix often use UDP to reduce latency and ensure continuous video playback. While TCP is also used for video streaming, especially when reliable transmission is needed, UDP is preferred in low-latency scenarios.

UDP 的优缺点 (Advantages and Disadvantages of UDP)

优点 (Advantages): - 低延迟 (Low Latency): 由于没有连接建立和可靠性检查,UDP 的传输速度快,延迟低。 - Low Latency: Due to the lack of connection establishment and reliability checks, UDP's transmission is fast and has low latency. - 简单性 (Simplicity): UDP 协议头部简单,处理和实现相对容易。 - Simplicity: UDP has a simple protocol header, making it easier to process and implement. - 支持多播和广播 (Supports Multicast and Broadcast): UDP 能有效地进行多播和广播。 - Supports Multicast and Broadcast: UDP can efficiently handle multicast and broadcast.

缺点 (Disadvantages): - 不可靠 (Unreliable): 没有重传机制,如果数据在传输过程中丢失,UDP 不会自动重发。 - Unreliable: There is no retransmission mechanism; if data is lost during transmission, UDP does not automatically resend it. - 无序传输 (Unordered Delivery): UDP 数据报可能乱序到达接收方,应用层需要自己处理顺序问题。 - Unordered Delivery: UDP datagrams may arrive out of order, requiring the application layer to handle the order. - 无流量控制 (No Flow Control): UDP 不提供流量控制,可能会导致接收方被数据淹没。 - No Flow Control: UDP does not provide flow control, which could overwhelm the receiver with data.

总结 (Conclusion)

UDP 是一个高效、简单、无连接的传输层协议,适用于那些需要快速传输而不在意可靠性的应用场景。尽管它没有 TCP 的可靠性和顺序保证,但其低延迟和支持多播的特性使得 UDP 在实时通信、在线游戏和视频流等应用中广泛使用。 UDP is an efficient, simple, and connectionless transport layer protocol, suitable for applications that require fast transmission without a focus on reliability. Although it lacks the reliability and order guarantees of TCP, its low latency and support for multicast make UDP widely used in real-time communication, online gaming, and video streaming.

UDP 头部结构 (UDP Header Structure)

UDP 头部概述 (Overview of UDP Header)

UDP 头部总共占用 8 字节,由以下四个字段组成,每个字段占 2 字节(16 位):

  1. 源端口号 (Source Port)
  2. 目标端口号 (Destination Port)
  3. 长度 (Length)
  4. 校验和 (Checksum)

1. 源端口号 (Source Port)

  • 描述 (Description): 源端口号字段标识发送方的端口号。它是一个可选字段,如果不需要可以设置为 0。这个端口号允许接收方知道该数据报的来源,以便在需要时进行回复。
  • Explanation: The source port field identifies the port number of the sender. It is an optional field and can be set to 0 if not needed. This port number allows the receiver to know the source of the datagram, enabling a response if necessary.

  • 长度 (Length): 2 字节 (16 位)

  • Length: 2 bytes (16 bits)

2. 目标端口号 (Destination Port)

  • 描述 (Description): 目标端口号字段标识接收方的端口号。这个端口号用于确定将数据报交付给哪个应用程序或服务。
  • Explanation: The destination port field identifies the port number of the receiver. This port number is used to determine which application or service should receive the datagram.

  • 长度 (Length): 2 字节 (16 位)

  • Length: 2 bytes (16 bits)

3. 长度 (Length)

  • 描述 (Description): 长度字段指定整个 UDP 数据报的长度,包括 UDP 头部和数据部分。由于 UDP 头部固定为 8 字节,因此该字段的最小值为 8。最大值取决于 IP 协议的限制。
  • Explanation: The length field specifies the length of the entire UDP datagram, including the UDP header and the data portion. Since the UDP header is fixed at 8 bytes, the minimum value for this field is 8. The maximum value depends on the limitations of the IP protocol.

  • 长度 (Length): 2 字节 (16 位)

  • Length: 2 bytes (16 bits)

4. 校验和 (Checksum)

  • 描述 (Description): 校验和字段用于检验 UDP 数据报在传输过程中是否发生了错误。虽然这个字段在 IPv4 中是可选的,但在 IPv6 中是必需的。校验和计算包括伪头部、UDP 头部以及数据部分,以确保数据的完整性。
  • Explanation: The checksum field is used to verify the integrity of the UDP datagram during transmission. While this field is optional in IPv4, it is mandatory in IPv6. The checksum calculation includes a pseudo-header, the UDP header, and the data portion to ensure data integrity.

  • 长度 (Length): 2 字节 (16 位)

  • Length: 2 bytes (16 bits)

UDP 头部的示意图 (Illustration of the UDP Header)


  0      7 8     15 16    23 24    31  (比特位)
  +--------+--------+--------+--------+
  |     源端口号    |    目标端口号   |
  +--------+--------+--------+--------+
  |     长度       |    校验和       |
  +--------+--------+--------+--------+

---