Skip to content

Wiki

UDP communication

About 10 min read

Existing English translations were reused. 1 current source blocks remain in Simplified Chinese, without triggering paid translation.

1.Linux UDP Communication

UDP communication is a very common communication method in Linux network programming.

Compared with TCP, UDP does not require establishing a connection in advance, and the sender can directly send data to a specified IP and port. Its characteristics are simplicity, low latency, and low overhead, but it does not guarantee that data will be delivered, nor does it guarantee order.

In robotics projects, UDP is commonly used for high-frequency, real-time data transmission scenarios where a small amount of packet loss is acceptable.

In actual projects, UDP communication is commonly used in:

  • LiDAR data transmission
  • Network camera or sensor data stream
  • Robot status broadcasting
  • LAN device discovery
  • Remote controller or gamepad data transmission
  • Simple communication between multiple robots
  • High-frequency status reporting
  • Scenarios with high real-time requirements but allowing a small amount of packet loss

The core characteristic of UDP is that it is fast but unreliable. Therefore, it is more suitable for "continuously sent data streams" rather than scenarios where "every message must be guaranteed to be delivered".

1.1.What is UDP communication?

UDP, short for User Datagram Protocol, is a connectionless transport protocol.

Unlike TCP, UDP communication does not require establishing a connection. As long as the destination IP and port are known, data can be sent directly.

The typical structure is as follows:

UDP Sender
    ↓
UDP Receiver

It can also be two-way communication:

程序 A  ←UDP→  程序 B

Common concepts include:

Concept Description
IP Address Used to locate devices in the network
Port Number Used to distinguish different programs
Datagram UDP datagram; one send corresponds to one datagram
Broadcast Send data to multiple devices within a local area network
Multicast Send data to devices that have joined the same multicast group
Packet Loss UDP does not guarantee that data will be delivered
Out-of-Order UDP does not guarantee that data will arrive in order

The biggest difference between UDP and TCP is: TCP is a reliable byte stream, while UDP is an unreliable datagram.

1.2.Common UDP libraries under Linux

There are multiple solutions for UDP communication under Linux, common ones include:

方案 类型 特点 适合场景
Linux socket API Linux 原生接口 最底层、依赖少、通用性强 想深入理解网络通信机制
boost::asio 通用 C++ 网络库 支持同步/异步 UDP,适合工程封装 推荐用于 C++ 项目
Qt Network Qt 网络模块 和 Qt 界面程序结合方便 Qt 上位机
Python socket Python 标准库 简单方便,适合测试 Python 调试脚本
ROS 2 topic ROS 2 通信机制 ROS 2 内部通信方便 ROS 2 系统内部数据传输

In essence, all these approaches are for completing the sending and receiving of UDP datagrams; they differ only in the way they are encapsulated.

1.3.Comparison of Schemes

Solution Advantages Disadvantages
Linux socket API Native, stable, few dependencies, strong versatility Code is relatively cumbersome, asynchronous handling is troublesome
boost::asio C++ is engineering-friendly, convenient for asynchronous UDP, suitable for encapsulating drivers Requires understanding concepts such as io_context, endpoint, socket, etc.
Qt Network Integrates naturally with Qt programs, suitable for host computers Not suitable for low-level drivers detached from Qt
Python socket Quick to get started, suitable for testing UDP packets Not suitable as the C++ low-level mainline
ROS 2 topic Convenient for ROS 2 internal communication Not suitable as a replacement for general UDP learning

1.4.This tutorial recommends selecting

This tutorial recommends prioritizing:

boost::asio

The reason is:

  • Suitable for C++ engineering development
  • TCP, serial ports, and timers can use the same Boost.Asio programming model
  • Supports synchronous and asynchronous UDP transmission and reception
  • Does not depend on ROS 2, making it easy to reuse in ordinary C++, OpenCV, Qt, and embedded Linux projects
  • Suitable for encapsulating functions such as networked sensors, remote control, and status broadcasting

The recommended engineering structure is:

上层项目:ROS 2 / OpenCV / Qt / 普通 C++ 程序
        ↓
自己封装的 UdpSender / UdpReceiver 类
        ↓
boost::asio
        ↓
UDP socket
        ↓
雷达 / 网络设备 / 远程控制端

1.5.How to choose between TCP and UDP

TCP and UDP are both commonly used, but they are suited to different scenarios.

Comparison Item TCP UDP
Connection Requires connection establishment No connection establishment required
Reliability Reliable transmission No guaranteed delivery
Ordering Guarantees ordering No ordering guarantee
Latency Relatively higher Relatively lower
Suitable Data Commands, configuration, files, logs High-frequency data, broadcast, real-time status
Typical Scenarios Remote control, server communication, parameter configuration Radar data, telecontrol data, status broadcast

Simple understanding:

TCP:我要确保这条消息送到
UDP:我要快速持续发送,偶尔丢一包也能接受

1.6.Relationship with ROS 2

Learning UDP does not mean using UDP to replace ROS 2's topic.

Within the ROS 2 system, communication between nodes typically prioritizes ROS 2's own communication mechanisms. UDP, on the other hand, is more suitable for connecting to network devices outside the ROS 2 system, or for implementing lightweight remote communication.

For example:

激光雷达
        ↓
UDP 数据包
        ↓
自己封装的 UdpReceiver
        ↓
ROS 2 节点发布点云 / LaserScan

Can also be used for:

远程遥控端
        ↓
UDP
        ↓
机器人主控程序

1.7.Chapter Learning Objectives

After completing this chapter, you should be able to master:

  • Basic concepts of UDP communication
  • Main differences between UDP and TCP
  • Meaning of IP address, port number, and endpoint
  • Use C++ to send and receive UDP data
  • Understand UDP packet loss, out-of-order, broadcast, and other characteristics
  • Use Boost.Asio to encapsulate a UDP communication class
  • Lay the foundation for subsequent radar data reception, remote control, and status broadcasting