Technology Encyclopedia Home >What is the basic structure of a UDP message?

What is the basic structure of a UDP message?

A UDP (User Datagram Protocol) message consists of two main parts: the header and the data payload.

Header

The UDP header is 8 bytes (64 bits) long and contains the following fields:

  1. Source Port (16 bits): Identifies the sending port.
  2. Destination Port (16 bits): Identifies the receiving port.
  3. Length (16 bits): Specifies the total length of the UDP datagram, including the header and the data.
  4. Checksum (16 bits): Used for error checking of the header and data. It can be set to zero if checksum is not used.

Data Payload

The data payload is the actual data being transmitted. The length of this part can vary, but it must fit within the maximum size allowed by the network.

Example

Suppose you want to send a simple message "Hello" using UDP. Here’s how it would look:

  • Header:

    • Source Port: 12345 (example)
    • Destination Port: 54321 (example)
    • Length: 16 bits for the header (8 bytes) + 5 bytes for "Hello" = 13 bytes (in this case, the length field would be set to 13 + 8 = 21 bytes, but typically the header is fixed at 8 bytes, so the length would be 13 bytes)
    • Checksum: Calculated based on the header and data (example value: 0x1234)
  • Data Payload: "Hello" (5 bytes)

In practice, the length field would be set to 13 bytes (8 bytes header + 5 bytes data), and the checksum would be calculated accordingly.

Tencent Cloud Services

For implementing UDP-based applications, Tencent Cloud offers services like Cloud Virtual Machine (CVM) where you can deploy your own UDP servers and clients. Additionally, Tencent Cloud Load Balancer can be configured to handle UDP traffic, providing scalability and reliability for your applications.