Write a simple server and client in C + +

Source: Internet
Author: User

We will create a server node Add_two_ints_server, which will receive two integers and return their and. Switch directories to the previously established Beginner_tutorials package:

CD ~/catkin_ws/src/beginner_tutorials

Edit Src/add_two_ints_server.cpp File:

Vim Src/add_two_ints_server.cpp

That is to say we write the customer point is also a node nodes, its CPP and the generated executable module and the previous talker, listener is the same, but they use the SRV service.

Copy the following code to the file and exit after saving:

#include"Ros/ros.h"#include"Beginner_tutorials/addtwoints.h"BOOLAdd (Beginner_tutorials::addtwoints::request &req, Beginner_tutorials::addtwoints::response&Res) {Res.sum= Req.a +req.b; Ros_info ("request:x=%ld, Y=%ld", (Long int) Req.a, (Long int) req.b); Ros_info ("Sending back response: [%ld]", (Long int) res.sum); return true;}intMainintargcChar**argv) {Ros::init (argc, argv,"Add_two_ints_server");  Ros::nodehandle N; Ros::serviceserver Service= N.advertiseservice ("add_two_ints", add); Ros_info ("Ready to Add the ints.");  Ros::spin (); return 0;}

The following explains the code:

" Ros/ros.h "  "beginner_tutorials/addtwoints.h"

beginner_tutorials/addtwoints.h is a header file that is automatically generated by the SRV file that we created earlier.

bool Add (beginner_tutorials::addtwoints::request  &&res)

This function provides a service for adding two integers, which uses the request and response types defined in the SRV file, and returns a Boolean type value.

{  = req.a + req.b;  Ros_info ("request:x=%ld, y=%ld", (longint) req.a, (long int ) req.b);  Ros_info ("sending back response: [%ld]", (longint ) res.sum);   return true ;}

Here the two integers are added and stored in the response, then output some information about the request and response, and finally return a true value.

Ros::serviceserver service = N.advertiseservice ("add_two_ints", add);

This line of code creates a service.

Next, create a client and open a terminal input:

Vim Src/add_two_ints_client.cpp

Copy the following code and exit after saving:

#include"Ros/ros.h"#include"Beginner_tutorials/addtwoints.h"#include<cstdlib>intMainintargcChar**argv) {Ros::init (argc, argv,"add_two_ints_client"); if(ARGC! =3) {Ros_info ("usage:add_two_ints_client X Y"); return 1;  } ros::nodehandle N; Ros::serviceclient Client= N.serviceclient<beginner_tutorials::addtwoints> ("add_two_ints");  Beginner_tutorials::addtwoints SRV; SRV.REQUEST.A= Atoll (argv[1]); srv.request.b= Atoll (argv[2]); if(Client.call (SRV)) {Ros_info ("Sum:%ld", (Long int) srv.response.sum); }  Else{ros_error ("Failed to call service add_two_ints"); return 1; }  return 0;}

Here's a look at the code explanation:

Ros::serviceclient client = n.serviceclient<beginner_tutorials::addtwoints> ("add_two_ints ");

This line of code creates a client for add_two_ints, and theros::serviceclient object is then used to invoke the service.

= Atoll (argv[1= Atoll (argv[2]);

We have an example of an automatically generated service class and assign values to its request members. A service class consists of two members, requests, and services. It also includes definitions, requests, and responses for two classes.

if (Client.call (SRV))

This code begins to actually invoke the service because the service call is blocked and returns immediately when the call is complete. If the service invocation succeeds, call() returns the value in true and SRV. Otherwise, call() returns the value in False and SRV.

Open ~/catkin_ws/src/beginner_tutorials/cmakelists.txt, copy the following code to the end of the file, save, exit:

Add_executable (add_two_ints_server src/add_two_ints_server.cpp) target_link_libraries (Add_two_ints_server ${ Catkin_libraries}) add_dependencies (Add_two_ints_server beginner_tutorials_gencpp) add_executable (add_two_ints_ Client src/add_two_ints_client.cpp) target_link_libraries (add_two_ints_client ${catkin_libraries}) add_ Dependencies (add_two_ints_client beginner_tutorials_gencpp)

This will create two executables,add_two_ints_server and add_two_ints_client, which will be generated by default in ~/catkin_ws/devel/lib/share/ <package name> directory, you can run them directly, or you can run them through Rosrun.

Start building now: After the build is complete, they are below this directory: Catkin_ws/devel/lib/share/<package name>

CD ~/Catkin_wscatkin_make

OK, everything is ready, now check to see if the program is correct. Open a terminal and run the server:

Rosrun beginner_tutorials Add_two_ints_server

A hint will appear:

Ready to add the ints.

Then open a terminal and run the client:

1 3

We can see the results returned as follows:

1+3134
It means that you have succeeded.

Write a simple server and client in C + +

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.