10.ROS2 service communication client

 

1. Introduction to service communication

Service communication is a communication model based on request and response. Between the two communicating parties, the client sends request data to the server, and the server responds to the client.

The client/server model is as follows:image8

From the perspective of the service implementation mechanism, this form of question-and-answer is called the client/server model, or CS model for short.When the client needs certain data, it sends request information for a specific service. After the server receives the request, it will process it and feedback the response information.

This communication mechanism is also very common in life, such as the various web pages we often browse. At this time, your computer browser is the client.Send a request to the website server through the domain name or various operations. After receiving it, the server returns the page data that needs to be displayed.

 

This case is located in the factory docker container. The source code location is:

 

2. Client implementation

Create a new file [client_demo.py] in the same directory as [server_demo.py]

image-20231023184928672

Next edit [client_demo.py] to implement client functions and add the following code:

 

3. Edit configuration file

image-20231023185118116

 

4. Compile workspace

 

5. Run program

The sub-terminal execution is as follows:

image-20231023184847347

Run the server first, then run the client. The client provides a=10, b=90. The server performs the sum and the result is 100. The result is printed on both terminals.