第 13.3 節

TCP communication

0瀏覽次數0訪問次數--跳出率--平均停留

Linux TCP Communication

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

In robot 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 to serial ports and CAN, TCP is more network-oriented. It usually runs on Ethernet, Wi-Fi, or local loopback networks, suitable for transmitting control commands, configuration parameters, logs, status information, file data, etc.

In actual projects, TCP communication is common in:

  • Communication between Qt host computer and robot main controller
  • Communication between robot and remote server
  • Industrial computer and network device communication
  • Reliable data exchange between multiple programs
  • Remotely send control commands
  • Upload logs, configuration files, and map files.
  • Communicating with the SDKs of certain radars, cameras, and industrial equipment

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

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. Once the connection is established, both parties can send and receive data just like reading and writing files.

A typical structure is as follows:

TCP Server
    ↑↓
TCP Client

Common concepts include:

conceptExplanation
IP addressUsed to locate devices on the network, such as 192.168.1.10
port numberused to distinguish different services on the same device, such as 8080
ServerServer, responsible for listening on a port and waiting for connections.
ClientClient, actively connect to the server.
SocketAbstract interface for network communication
ConnectionA connection must be established before TCP communication.
sticky packetTCP is a byte stream protocol, so you need to design your own application layer protocol.

It is important to note that TCP does not map one message to one receive operation. TCP is essentially a byte stream protocol, so in practical engineering, you usually need to design your own data protocol, such as frame header, length, command word, data area, checksum, etc.

Common TCP libraries on Linux

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

solutiontypeFeaturesSuitable scenarios
Linux socket APILinux native interfaceLowest level, few dependencies, high versatility.Want to deeply understand network communication mechanisms.
boost::asioGeneral-purpose C++ networking librarySupports synchronous/asynchronous modes, suitable for C++ project encapsulation.Recommended for C++ projects
Qt NetworkQt Network ModuleEasy to integrate with Qt interface programs.Qt host computer
Poco NetC++ networking libraryFully encapsulated, feature-rich.Medium-to-large C++ service programs
Python socketPython standard librarySimple and convenient, suitable for testing.Python debugging script
ROS 2 topic/service/actionROS 2 communication mechanismSuitable for ROS 2 internal node communication.ROS 2 internal communication

These solutions are all fundamentally designed to complete TCP connections, sending, and receiving, differing only in their level of encapsulation.

Comparison of Solutions

solutionAdvantagesDisadvantages
Linux socket APInative, stable, few dependencies, strong versatilityThe code is relatively cumbersome, and asynchronous handling is troublesome.
boost::asioC++ project-friendly, convenient asynchronous communication, suitable for encapsulating drivers.Need to understand concepts such as io_context, socket, and callbacks.
Qt NetworkIntegrates naturally with Qt programs; signals and slots are convenient to use.Not suitable to be detached from Qt's underlying driver.
Poco NetEncapsulation is complete, strong engineering capability.A bit heavy for beginners.
Python socketQuick to learn and suitable for testing.Not suitable as the main thread for C++ low-level
ROS 2 CommunicationROS 2's internal integration is convenient.Not suitable as a replacement for general TCP learning

This tutorial recommends selecting

This tutorial recommends prioritizing:

boost::asio

The reason is:

  • Suitable for C++ engineering development
  • Supports TCP, UDP, serial port, timer, and other features.
  • You can write synchronous code, or you can write 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 be combined with serial port, UDP, and timer to form a unified I/O programming style.

The recommended project structure is:

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

Relationship with ROS 2

Learning TCP communication does not mean you need to use TCP to replace ROS 2's topics, services, and actions.

Within the ROS 2 system, communication between nodes typically prioritizes using ROS 2's own communication mechanisms. TCP is better suited for use between devices, programs, or services outside the ROS 2 system.

For example:

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

In other words, ROS 2 is responsible for internal communication within the robot system, while TCP handles interaction with external systems, network devices, and remote services.

Chapter learning objectives

After completing this chapter, you should be able to:

  • Basic concepts of TCP communication
  • The meaning of IP address, port number, server, and client
  • The basic relationship between Linux socket and Boost.Asio
  • Creating a TCP client and server using C++
  • Understand TCP byte streams and the sticky packet problem
  • Design a simple TCP application-layer protocol
  • Encapsulate TCP communication as a reusable C++ class
  • Lay the foundation for subsequent network device communication, remote control, and Qt host computer.
音乐页