Skip to content

Wiki

TCP communication

About 10 min read

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

1.Linux TCP Communication

TCP communication is one of the most common communication methods in Linux network programming.

In robotics and embedded Linux projects, TCP is commonly used for reliable communication between host computers, between robots and servers, and between robots and debugging software.

Compared with serial ports and CAN, TCP is more oriented toward network communication. It typically runs over Ethernet, Wi-Fi or the local loopback network, and is suitable for transmitting control commands, configuration parameters, logs, status information, file data, etc.

In actual projects, TCP communication is commonly used in:

  • Communication between the Qt host computer and the robot main controller
  • Communication between the robot and the remote server
  • Communication between the industrial computer and network devices
  • Reliable data exchange between multiple programs
  • Remote sending of control commands
  • Uploading logs, configuration files, and map files
  • Communication with the SDKs of certain radars, cameras, and industrial devices

TCP is characterized by reliable, in-order transmission and is not prone to data loss, making it suitable for scenarios with high data integrity requirements.

1.1.What is TCP communication?

TCP, short for Transmission Control Protocol, is a connection-oriented reliable transport protocol.

Before using TCP communication, the client and server need to establish a connection first. Once the connection is established, both parties can send and receive data just like reading from and writing to files.

The typical structure is as follows:

TCP Server
    ↑↓
TCP Client

Common concepts include:

Concept Description
IP address Used to locate a device on the network, for example 192.168.1.10
Port number Used to distinguish different services on the same device, for example 8080
Server The server side, responsible for listening on a port and waiting for connections
Client The client side, actively connects to the server
Socket An abstract interface for network communication
Connection A connection must be established before TCP communication
Sticky packets TCP is a byte stream protocol, so you need to design your own application-layer protocol

It should be noted that TCP does not correspond to one receive per message. TCP is essentially a byte stream protocol, so in actual engineering it is usually necessary to design your own data protocol, such as frame header, length, command word, data area, checksum, etc.

1.2.Common TCP libraries on Linux

There are multiple approaches for TCP communication under Linux, common ones include:

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

These approaches are essentially all for completing TCP connection establishment, sending, and receiving; they differ only in their level of encapsulation.

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, asynchronous communication is convenient, and it is suitable for encapsulating drivers Requires understanding concepts such as io_context, sockets, and callbacks
Qt Network Integrates naturally with Qt programs; signals and slots are convenient to use Not suitable for low-level drivers detached from Qt
Poco Net Complete encapsulation, strong engineering capability Slightly heavy for beginners
Python socket Quick to get started, suitable for testing Not suitable as the main low-level line for C++
ROS 2 communication Convenient for internal integration with ROS 2 Not suitable as a replacement for general TCP learning

1.4.This tutorial recommends selecting

This tutorial recommends prioritizing:

boost::asio

The reason is:

  • Suitable for C++ engineering development
  • Simultaneously supports TCP, UDP, serial ports, timers, and other functions
  • You can write synchronous code or asynchronous code
  • Does not depend on ROS 2, making it easy to reuse in ordinary C++, OpenCV, Qt, and embedded Linux projects
  • Subsequently, it can form a unified I/O programming style with serial ports, UDP, and timers

The recommended engineering structure is:

上层项目:ROS 2 / OpenCV / Qt / 普通 C++ 程序
        ↓
自己封装的 TcpClient / TcpServer 类
        ↓
boost::asio
        ↓
TCP socket
        ↓
远程设备 / 服务器 / 调试软件

1.5.Relationship with ROS 2

Learning TCP communication does not mean using TCP to replace the topics, services, and actions of ROS 2.

Within the ROS 2 system, communication between nodes typically prioritizes ROS 2's own communication mechanism. TCP is more suitable for use between devices, programs, or services outside the ROS 2 system.

For example:

ROS 2 节点
        ↓
自己封装的 TcpClient
        ↓
boost::asio
        ↓
远程服务器 / Qt 调试软件 / 网络设备

That is to say, ROS 2 is responsible for internal communication within the robot system, while TCP is responsible for interacting with external systems, network devices, and remote services.

1.6.Chapter Learning Objectives

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

  • Basic concepts of TCP communication
  • Meaning of IP address, port number, server, and client
  • Basic relationship between Linux socket and Boost.Asio
  • Using C++ to create a TCP client and server
  • Understanding TCP byte streams and the packet sticking problem
  • Designing a simple TCP application-layer protocol
  • Encapsulating TCP communication into a reusable C++ class
  • Laying the foundation for subsequent network device communication, remote control, and Qt host computer