{"id":3045,"date":"2025-07-24T10:00:00","date_gmt":"2025-07-24T10:00:00","guid":{"rendered":"https:\/\/learnbydoing.dev\/?p=3045"},"modified":"2026-01-10T22:10:54","modified_gmt":"2026-01-10T22:10:54","slug":"how-to-build-and-test-a-ros-2-service-server-in-cpp","status":"publish","type":"post","link":"https:\/\/learnbydoing.dev\/how-to-build-and-test-a-ros-2-service-server-in-cpp\/","title":{"rendered":"How to Build and Test a ROS 2 Service Server in C++"},"content":{"rendered":"<div data-elementor-type=\"wp-post\" data-elementor-id=\"3045\" class=\"elementor elementor-3045\" data-elementor-post-type=\"post\">\n\t\t\t\t<div class=\"elementor-element elementor-element-e28ab35 e-flex e-con-boxed e-con e-parent\" data-id=\"e28ab35\" data-element_type=\"container\" data-e-type=\"container\" id=\"content\" data-settings=\"{&quot;background_background&quot;:&quot;classic&quot;}\">\n\t\t\t\t\t<div class=\"e-con-inner\">\n\t\t<div class=\"elementor-element elementor-element-62ab6e3 e-con-full e-flex e-con e-child\" data-id=\"62ab6e3\" data-element_type=\"container\" data-e-type=\"container\">\n\t\t\t\t<div class=\"elementor-element elementor-element-62ab203 elementor-align-center elementor-widget elementor-widget-post-info\" data-id=\"62ab203\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"post-info.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<ul class=\"elementor-inline-items elementor-icon-list-items elementor-post-info\">\n\t\t\t\t\t\t\t\t<li class=\"elementor-icon-list-item elementor-repeater-item-2c98363 elementor-inline-item\" itemprop=\"about\">\n\t\t\t\t\t\t\t\t\t\t\t\t\t<span class=\"elementor-icon-list-text elementor-post-info__item elementor-post-info__item--type-terms\">\n\t\t\t\t\t\t\t\t\t\t<span class=\"elementor-post-info__terms-list\">\n\t\t\t\t<span class=\"elementor-post-info__terms-list-item\">ROS 2<\/span>, <span class=\"elementor-post-info__terms-list-item\">Tutoriales<\/span>\t\t\t\t<\/span>\n\t\t\t\t\t<\/span>\n\t\t\t\t\t\t\t\t<\/li>\n\t\t\t\t<\/ul>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t<div class=\"elementor-element elementor-element-0650e10 e-con-full e-flex e-con e-child\" data-id=\"0650e10\" data-element_type=\"container\" data-e-type=\"container\">\n\t\t\t\t<div class=\"elementor-element elementor-element-ac19582 elementor-view-default elementor-widget elementor-widget-icon\" data-id=\"ac19582\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"icon.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<div class=\"elementor-icon-wrapper\">\n\t\t\t<div class=\"elementor-icon\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"75\" height=\"75\" viewbox=\"0 0 75 75\" fill=\"none\"><path d=\"M74.9999 75H13.1889V73.0002H71.5859L0.460938 1.87521L1.87515 0.460999L73.0001 71.586V13.1889H74.9999V75Z\" fill=\"white\"><\/path><\/svg>\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t<div class=\"elementor-element elementor-element-47aa245d e-flex e-con-boxed e-con e-parent\" data-id=\"47aa245d\" data-element_type=\"container\" data-e-type=\"container\" data-settings=\"{&quot;background_background&quot;:&quot;classic&quot;}\">\n\t\t\t\t\t<div class=\"e-con-inner\">\n\t\t\t\t<div class=\"elementor-element elementor-element-206a001 elementor-widget elementor-widget-image\" data-id=\"206a001\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"image.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<img fetchpriority=\"high\" decoding=\"async\" width=\"1920\" height=\"1080\" src=\"https:\/\/learnbydoing.dev\/wp-content\/uploads\/2025\/07\/Services-Server-_1_.webp\" class=\"attachment-full size-full wp-image-3051\" alt=\"\" srcset=\"https:\/\/learnbydoing.dev\/wp-content\/uploads\/2025\/07\/Services-Server-_1_.webp 1920w, https:\/\/learnbydoing.dev\/wp-content\/uploads\/2025\/07\/Services-Server-_1_-300x169.webp 300w, https:\/\/learnbydoing.dev\/wp-content\/uploads\/2025\/07\/Services-Server-_1_-1024x576.webp 1024w, https:\/\/learnbydoing.dev\/wp-content\/uploads\/2025\/07\/Services-Server-_1_-768x432.webp 768w, https:\/\/learnbydoing.dev\/wp-content\/uploads\/2025\/07\/Services-Server-_1_-1536x864.webp 1536w, https:\/\/learnbydoing.dev\/wp-content\/uploads\/2025\/07\/Services-Server-_1_-18x10.webp 18w, https:\/\/learnbydoing.dev\/wp-content\/uploads\/2025\/07\/Services-Server-_1_-1320x743.webp 1320w, https:\/\/learnbydoing.dev\/wp-content\/uploads\/2025\/07\/Services-Server-_1_-600x338.webp 600w\" sizes=\"(max-width: 1920px) 100vw, 1920px\" \/>\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-4bc31ca3 elementor-widget elementor-widget-text-editor\" data-id=\"4bc31ca3\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p data-start=\"353\" data-end=\"667\">In robotics, especially with ROS 2, it\u2019s common for different nodes to interact and collaborate to accomplish tasks. So far, we\u2019ve used the publisher\/subscriber model to communicate between them \u2014 which works well for data streaming. But what if you just need to trigger a specific operation and get a result back?<\/p><p data-start=\"669\" data-end=\"851\">That\u2019s where <strong data-start=\"682\" data-end=\"694\">Services<\/strong> in ROS 2 come into play. They allow one node to offer a specific functionality and other nodes to access it <strong data-start=\"803\" data-end=\"816\">on demand<\/strong> using a request-response protocol.<\/p><p data-start=\"853\" data-end=\"1073\">In this tutorial, we\u2019ll explore the <strong data-start=\"889\" data-end=\"904\">server side<\/strong> of services in ROS 2 using Python. We\u2019ll build a service that <strong data-start=\"967\" data-end=\"988\">sums two integers<\/strong>, exposing the logic as a reusable node that other nodes can call any time they need.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-12a4ca0 elementor-widget elementor-widget-text-editor\" data-id=\"12a4ca0\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<h3 data-start=\"1080\" data-end=\"1112\">\ud83e\udd14 Why use services in ROS 2?<\/h3><p data-start=\"1114\" data-end=\"1266\">Imagine this: you\u2019ve already built a face detection feature for your robot in one node. Later, you write another node that needs the same functionality.<\/p><p data-start=\"1268\" data-end=\"1456\">Copy-pasting the same face detection code into multiple files might sound like a quick fix, but it introduces risk and maintenance headaches \u2014 especially if the code needs to change later.<\/p><p data-start=\"1458\" data-end=\"1644\">Instead, you can <strong data-start=\"1475\" data-end=\"1507\">wrap that logic in a service<\/strong>, turning it into a callable unit. This way, other nodes just send a request, get a response, and stay decoupled from the implementation.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-e5ca195 elementor-widget elementor-widget-image\" data-id=\"e5ca195\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"image.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<img decoding=\"async\" width=\"800\" height=\"450\" src=\"https:\/\/learnbydoing.dev\/wp-content\/uploads\/2025\/07\/Services-Server-1024x576.gif\" class=\"attachment-large size-large wp-image-3040\" alt=\"\" srcset=\"https:\/\/learnbydoing.dev\/wp-content\/uploads\/2025\/07\/Services-Server-1024x576.gif 1024w, https:\/\/learnbydoing.dev\/wp-content\/uploads\/2025\/07\/Services-Server-300x169.gif 300w, https:\/\/learnbydoing.dev\/wp-content\/uploads\/2025\/07\/Services-Server-768x432.gif 768w, https:\/\/learnbydoing.dev\/wp-content\/uploads\/2025\/07\/Services-Server-1536x864.gif 1536w, https:\/\/learnbydoing.dev\/wp-content\/uploads\/2025\/07\/Services-Server-18x10.gif 18w, https:\/\/learnbydoing.dev\/wp-content\/uploads\/2025\/07\/Services-Server-1320x743.gif 1320w, https:\/\/learnbydoing.dev\/wp-content\/uploads\/2025\/07\/Services-Server-600x338.gif 600w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/>\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-324d52d elementor-widget elementor-widget-text-editor\" data-id=\"324d52d\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<h3 data-start=\"1777\" data-end=\"1809\">\ud83e\udde0 How Services Work in ROS 2<\/h3><p data-start=\"1811\" data-end=\"1859\">ROS 2 services follow a <strong data-start=\"1835\" data-end=\"1858\">client-server model<\/strong>:<br \/><br \/><\/p><ol data-start=\"1861\" data-end=\"2079\"><li data-start=\"1861\" data-end=\"1940\"><p data-start=\"1864\" data-end=\"1940\">A <strong data-start=\"1866\" data-end=\"1884\">Service Server<\/strong> node offers functionality (e.g., summing two numbers)<br \/><br \/><\/p><\/li><li data-start=\"1941\" data-end=\"2011\"><p data-start=\"1944\" data-end=\"2011\">A <strong data-start=\"1946\" data-end=\"1964\">Service Client<\/strong> node requests that functionality when needed<br \/><br \/><\/p><\/li><li data-start=\"2012\" data-end=\"2079\"><p data-start=\"2015\" data-end=\"2079\">The server processes the request and sends a <strong data-start=\"2060\" data-end=\"2072\">response<\/strong> back<br \/><br \/><\/p><\/li><\/ol><p data-start=\"2081\" data-end=\"2226\">This is fundamentally different from topics. Topics are about continuous data streams. Services are about one-time actions with expected outputs.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-6c6f0fb elementor-widget elementor-widget-text-editor\" data-id=\"6c6f0fb\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<h3 data-start=\"2353\" data-end=\"2395\">\ud83e\uddf1 Step 1: Define the Service Interface<\/h3><p data-start=\"2397\" data-end=\"2475\">Before building the service, we must define the message structure it will use.<\/p><p data-start=\"2477\" data-end=\"2503\">Each service in ROS 2 has:<\/p><ol><li data-start=\"2506\" data-end=\"2527\">A <strong data-start=\"2508\" data-end=\"2519\">request<\/strong> message<\/li><li data-start=\"2506\" data-end=\"2527\">A <strong data-start=\"2532\" data-end=\"2544\">response<\/strong> message<\/li><\/ol><p data-start=\"2554\" data-end=\"2607\">Let\u2019s define a simple service that adds two integers.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-1d4dd17 elementor-widget elementor-widget-text-editor\" data-id=\"1d4dd17\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<h4><strong>\ud83d\udce6 Create a new package for messages<\/strong><\/h4>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-5b7353d elementor-widget elementor-widget-code-highlight\" data-id=\"5b7353d\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<div class=\"prismjs-tomorrow copy-to-clipboard\">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-bash\">\n\t\t\t\t<code readonly=\"true\" class=\"language-bash\">\n\t\t\t\t\t<xmp>cd ~\/arduinobot_ws\/src\r\nros2 pkg create --build-type ament_cmake arduinobot_msgs\r\n<\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-5e1cac9 elementor-widget elementor-widget-text-editor\" data-id=\"5e1cac9\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>Create the interface folder and file:<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-3703481 elementor-widget elementor-widget-code-highlight\" data-id=\"3703481\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<div class=\"prismjs-tomorrow copy-to-clipboard\">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-bash\">\n\t\t\t\t<code readonly=\"true\" class=\"language-bash\">\n\t\t\t\t\t<xmp>mkdir -p arduinobot_msgs\/srv\r\nnano arduinobot_msgs\/srv\/AddTwoInts.srv\r\n<\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-952f9ec elementor-widget elementor-widget-text-editor\" data-id=\"952f9ec\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>Add the request\/response structure:<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-b7f57d6 elementor-widget elementor-widget-code-highlight\" data-id=\"b7f57d6\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<div class=\"prismjs-tomorrow copy-to-clipboard\">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-bash\">\n\t\t\t\t<code readonly=\"true\" class=\"language-bash\">\n\t\t\t\t\t<xmp># Request\r\nint64 a\r\nint64 b\r\n---\r\n# Response\r\nint64 sum\r\n<\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-1852e89 elementor-widget elementor-widget-text-editor\" data-id=\"1852e89\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>Update <code data-start=\"2972\" data-end=\"2988\">CMakeLists.txt<\/code>:<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-e82bb3f elementor-widget elementor-widget-code-highlight\" data-id=\"e82bb3f\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<div class=\"prismjs-tomorrow copy-to-clipboard\">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-bash\">\n\t\t\t\t<code readonly=\"true\" class=\"language-bash\">\n\t\t\t\t\t<xmp>find_package(rosidl_default_generators REQUIRED)\r\nfind_package(std_msgs REQUIRED)\r\n\r\nrosidl_generate_interfaces(${PROJECT_NAME}\r\n  \"srv\/AddTwoInts.srv\"\r\n)\r\n<\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-8966d58 elementor-widget elementor-widget-text-editor\" data-id=\"8966d58\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>Update <code data-start=\"3162\" data-end=\"3175\">package.xml<\/code>:<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-4f63df6 elementor-widget elementor-widget-code-highlight\" data-id=\"4f63df6\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<div class=\"prismjs-tomorrow copy-to-clipboard\">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-bash\">\n\t\t\t\t<code readonly=\"true\" class=\"language-bash\">\n\t\t\t\t\t<xmp><build_depend>rosidl_default_generators<\/build_depend>\r\n<exec_depend>rosidl_default_runtime<\/exec_depend>\r\n<depend>std_msgs<\/depend>\r\n<member_of_group>rosidl_interface_packages<\/member_of_group>\r\n<\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-5b49413 elementor-widget elementor-widget-text-editor\" data-id=\"5b49413\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>Build the workspace:<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-633d36c elementor-widget elementor-widget-code-highlight\" data-id=\"633d36c\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<div class=\"prismjs-tomorrow copy-to-clipboard\">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-bash\">\n\t\t\t\t<code readonly=\"true\" class=\"language-bash\">\n\t\t\t\t\t<xmp>cd ~\/arduinobot_ws\r\ncolcon build\r\n. install\/setup.bash\r\n<\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-dd95e0f elementor-widget elementor-widget-text-editor\" data-id=\"dd95e0f\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<h3 data-start=\"3475\" data-end=\"3506\"><img decoding=\"async\" class=\"emoji\" role=\"img\" draggable=\"false\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/16.0.1\/svg\/1f9e0.svg\" alt=\"\ud83e\udde0\" \/> The Full Code<\/h3><p data-start=\"3508\" data-end=\"3685\">Now that we\u2019ve defined the service interface, let\u2019s create the actual <strong data-start=\"3578\" data-end=\"3593\">C++ node<\/strong> that will act as the server. Here&#8217;s the full code, followed by an explanation of each part.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-66d9afa elementor-widget elementor-widget-code-highlight\" data-id=\"66d9afa\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<div class=\"prismjs-tomorrow copy-to-clipboard\">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-cpp line-numbers\">\n\t\t\t\t<code readonly=\"true\" class=\"language-cpp\">\n\t\t\t\t\t<xmp>#include <memory>\r\n#include <rclcpp\/rclcpp.hpp>\r\n#include \"arduinobot_msgs\/srv\/add_two_ints.hpp\"\r\n\r\nusing std::placeholders::_1;\r\nusing std::placeholders::_2;\r\n\r\nclass SimpleServiceServer : public rclcpp::Node\r\n{\r\npublic:\r\n  SimpleServiceServer()\r\n  : Node(\"simple_service_server\")\r\n  {\r\n    service_ = this->create_service<arduinobot_msgs::srv::AddTwoInts>(\r\n      \"add_two_ints\", std::bind(&SimpleServiceServer::serviceCallback, this, _1, _2));\r\n\r\n    RCLCPP_INFO(this->get_logger(), \"Service add_two_ints Ready\");\r\n  }\r\n\r\nprivate:\r\n  void serviceCallback(\r\n    const std::shared_ptr<arduinobot_msgs::srv::AddTwoInts::Request> request,\r\n    const std::shared_ptr<arduinobot_msgs::srv::AddTwoInts::Response> response)\r\n  {\r\n    RCLCPP_INFO(this->get_logger(), \"New Request Received: a=%ld, b=%ld\", request->a, request->b);\r\n\r\n    response->sum = request->a + request->b;\r\n\r\n    RCLCPP_INFO(this->get_logger(), \"Returning sum: %ld\", response->sum);\r\n  }\r\n\r\n  rclcpp::Service<arduinobot_msgs::srv::AddTwoInts>::SharedPtr service_;\r\n};\r\n\r\nint main(int argc, char **argv)\r\n{\r\n  rclcpp::init(argc, argv);\r\n  auto node = std::make_shared<SimpleServiceServer>();\r\n  rclcpp::spin(node);\r\n  rclcpp::shutdown();\r\n  return 0;\r\n}\r\n<\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-4b5438c elementor-widget elementor-widget-text-editor\" data-id=\"4b5438c\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<h3 data-start=\"3475\" data-end=\"3506\">\ud83e\udde0 Let\u2019s break down the code<\/h3><p data-start=\"3508\" data-end=\"3685\">Let\u2019s go through the code of our <code data-start=\"443\" data-end=\"469\">simple_service_server.cpp<\/code> line by line, so you fully understand how the node is built and how the service is created and exposed in ROS 2.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-0a44575 elementor-widget elementor-widget-text-editor\" data-id=\"0a44575\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<h5><strong>Include Headers<\/strong><\/h5>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-dd8ffa0 elementor-widget elementor-widget-code-highlight\" data-id=\"dd8ffa0\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<div class=\"prismjs-tomorrow copy-to-clipboard\">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-cpp\">\n\t\t\t\t<code readonly=\"true\" class=\"language-cpp\">\n\t\t\t\t\t<xmp>#include <memory>\r\n#include <rclcpp\/rclcpp.hpp>\r\n#include \"arduinobot_msgs\/srv\/add_two_ints.hpp\"\r\n<\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-f981deb elementor-widget elementor-widget-text-editor\" data-id=\"f981deb\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<ol><li data-start=\"333\" data-end=\"454\"><code data-start=\"333\" data-end=\"341\">memory<\/code>: This is from the C++ standard library and provides <code data-start=\"394\" data-end=\"411\">std::shared_ptr<\/code>, which we use for smart memory management.<br \/><br \/><\/li><li data-start=\"333\" data-end=\"454\"><code data-start=\"457\" data-end=\"465\">rclcpp<\/code>: The core ROS 2 client library for C++.<br \/><br \/><\/li><li data-start=\"333\" data-end=\"454\"><code data-start=\"508\" data-end=\"548\">\"arduinobot_msgs\/srv\/add_two_ints.hpp\"<\/code>: The header file generated from your custom <code data-start=\"593\" data-end=\"599\">.srv<\/code> definition. It contains the <code data-start=\"628\" data-end=\"640\">AddTwoInts<\/code> service interface.<\/li><\/ol>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-21c357b elementor-widget elementor-widget-text-editor\" data-id=\"21c357b\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<h5><strong>Placeholders for std::bind<\/strong><\/h5>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-2231fa3 elementor-widget elementor-widget-code-highlight\" data-id=\"2231fa3\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<div class=\"prismjs-tomorrow copy-to-clipboard\">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-cpp\">\n\t\t\t\t<code readonly=\"true\" class=\"language-cpp\">\n\t\t\t\t\t<xmp>using std::placeholders::_1;\r\nusing std::placeholders::_2;\r\n<\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-a4ec8db elementor-widget elementor-widget-text-editor\" data-id=\"a4ec8db\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p data-start=\"771\" data-end=\"955\">These are used to specify arguments when binding a method to a callback. <code data-start=\"844\" data-end=\"848\">_1<\/code> and <code data-start=\"853\" data-end=\"857\">_2<\/code> correspond to the first and second arguments passed during a service call (request and response).<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-fb9b16c elementor-widget elementor-widget-text-editor\" data-id=\"fb9b16c\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<h5><strong>Define the service node class<\/strong><\/h5>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-efeb3fd elementor-widget elementor-widget-code-highlight\" data-id=\"efeb3fd\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<div class=\"prismjs-tomorrow copy-to-clipboard\">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-cpp\">\n\t\t\t\t<code readonly=\"true\" class=\"language-cpp\">\n\t\t\t\t\t<xmp>class SimpleServiceServer : public rclcpp::Node\r\n<\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-d0ee944 elementor-widget elementor-widget-text-editor\" data-id=\"d0ee944\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>We&#8217;re declaring a class called <code data-start=\"1091\" data-end=\"1112\">SimpleServiceServer<\/code>, and it inherits from <code data-start=\"1135\" data-end=\"1149\">rclcpp::Node<\/code>, which is the base class for any ROS 2 node in C++.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-2e7573c elementor-widget elementor-widget-text-editor\" data-id=\"2e7573c\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<h5><strong>Constructor: initialize the node<\/strong><\/h5>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-3dd01d3 elementor-widget elementor-widget-code-highlight\" data-id=\"3dd01d3\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<div class=\"prismjs-tomorrow copy-to-clipboard\">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-cpp\">\n\t\t\t\t<code readonly=\"true\" class=\"language-cpp\">\n\t\t\t\t\t<xmp>SimpleServiceServer()\r\n: Node(\"simple_service_server\")<\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-8c6409d elementor-widget elementor-widget-text-editor\" data-id=\"8c6409d\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p data-start=\"1315\" data-end=\"1424\">This constructor initializes the node and registers its name in the ROS 2 graph as <code data-start=\"1398\" data-end=\"1423\">\"simple_service_server\"<\/code>.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-e5aab62 elementor-widget elementor-widget-text-editor\" data-id=\"e5aab62\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<h5><strong>Create the service<\/strong><\/h5>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-2f5d2df elementor-widget elementor-widget-code-highlight\" data-id=\"2f5d2df\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<div class=\"prismjs-tomorrow copy-to-clipboard\">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-cpp\">\n\t\t\t\t<code readonly=\"true\" class=\"language-cpp\">\n\t\t\t\t\t<xmp>service_ = this->create_service<arduinobot_msgs::srv::AddTwoInts>(\r\n  \"add_two_ints\", std::bind(&SimpleServiceServer::serviceCallback, this, _1, _2));\r\n<\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-c8c2ad2 elementor-widget elementor-widget-text-editor\" data-id=\"c8c2ad2\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p data-start=\"1620\" data-end=\"1657\">This line creates the actual service:<\/p><ol><li data-start=\"1661\" data-end=\"1716\">It is of type <code data-start=\"1675\" data-end=\"1687\">AddTwoInts<\/code>, defined in the <code data-start=\"1704\" data-end=\"1710\">.srv<\/code> file.<\/li><li data-start=\"1661\" data-end=\"1716\">The service is registered with the name <code data-start=\"1759\" data-end=\"1774\">\/add_two_ints<\/code> in ROS 2.<\/li><li data-start=\"1661\" data-end=\"1716\">When a client sends a request, the method <code data-start=\"1829\" data-end=\"1846\">serviceCallback<\/code> will be executed, with <code data-start=\"1870\" data-end=\"1874\">_1<\/code> and <code data-start=\"1879\" data-end=\"1883\">_2<\/code> representing the incoming request and response objects.<\/li><\/ol>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-964721b elementor-widget elementor-widget-text-editor\" data-id=\"964721b\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<h5><strong>Log a message when ready<\/strong><\/h5>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-0a501c6 elementor-widget elementor-widget-code-highlight\" data-id=\"0a501c6\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<div class=\"prismjs-tomorrow copy-to-clipboard\">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-cpp\">\n\t\t\t\t<code readonly=\"true\" class=\"language-cpp\">\n\t\t\t\t\t<xmp>RCLCPP_INFO(this->get_logger(), \"Service add_two_ints Ready\");\r\n<\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-6e7b8be elementor-widget elementor-widget-text-editor\" data-id=\"6e7b8be\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p data-start=\"2054\" data-end=\"2159\">This logs an informational message to the terminal so you know your server has been successfully created.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-e4e50b5 elementor-widget elementor-widget-text-editor\" data-id=\"e4e50b5\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<h5><strong>Define the service callback<\/strong><\/h5>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-2d21b79 elementor-widget elementor-widget-code-highlight\" data-id=\"2d21b79\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<div class=\"prismjs-tomorrow copy-to-clipboard\">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-cpp\">\n\t\t\t\t<code readonly=\"true\" class=\"language-cpp\">\n\t\t\t\t\t<xmp>void serviceCallback(\r\n  const std::shared_ptr<arduinobot_msgs::srv::AddTwoInts::Request> request,\r\n  const std::shared_ptr<arduinobot_msgs::srv::AddTwoInts::Response> response)\r\n<\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-7c4658a elementor-widget elementor-widget-text-editor\" data-id=\"7c4658a\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p data-start=\"2054\" data-end=\"2159\">This function will be called every time a client sends a request to the service.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-a20621d elementor-widget elementor-widget-code-highlight\" data-id=\"a20621d\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<div class=\"prismjs-tomorrow copy-to-clipboard\">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-cpp\">\n\t\t\t\t<code readonly=\"true\" class=\"language-cpp\">\n\t\t\t\t\t<xmp>RCLCPP_INFO(this->get_logger(), \"New Request Received: a=%ld, b=%ld\", request->a, request->b);\r\n<\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-5dc6e38 elementor-widget elementor-widget-text-editor\" data-id=\"5dc6e38\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>This logs the two integers received from the client.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-b14dc99 elementor-widget elementor-widget-code-highlight\" data-id=\"b14dc99\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<div class=\"prismjs-tomorrow copy-to-clipboard\">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-cpp\">\n\t\t\t\t<code readonly=\"true\" class=\"language-cpp\">\n\t\t\t\t\t<xmp>response->sum = request->a + request->b;\r\n<\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-07c78b5 elementor-widget elementor-widget-text-editor\" data-id=\"07c78b5\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>This performs the core functionality of the service: summing the two values<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-d933c67 elementor-widget elementor-widget-code-highlight\" data-id=\"d933c67\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<div class=\"prismjs-tomorrow copy-to-clipboard\">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-cpp\">\n\t\t\t\t<code readonly=\"true\" class=\"language-cpp\">\n\t\t\t\t\t<xmp>RCLCPP_INFO(this->get_logger(), \"Returning sum: %ld\", response->sum);\r\n<\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-621fad0 elementor-widget elementor-widget-text-editor\" data-id=\"621fad0\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>This logs the result before it&#8217;s returned to the client.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-9379e83 elementor-widget elementor-widget-text-editor\" data-id=\"9379e83\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<h5><strong>Declare the service object<\/strong><\/h5>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-61fad03 elementor-widget elementor-widget-code-highlight\" data-id=\"61fad03\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<div class=\"prismjs-tomorrow copy-to-clipboard\">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-cpp\">\n\t\t\t\t<code readonly=\"true\" class=\"language-cpp\">\n\t\t\t\t\t<xmp>rclcpp::Service<arduinobot_msgs::srv::AddTwoInts>::SharedPtr service_;\r\n<\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-4b912ad elementor-widget elementor-widget-text-editor\" data-id=\"4b912ad\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>This member variable holds the reference to the service so it can live as long as the node itself. It&#8217;s a <code data-start=\"3133\" data-end=\"3144\">SharedPtr<\/code> (shared pointer) to manage memory safely.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-0cf9947 elementor-widget elementor-widget-text-editor\" data-id=\"0cf9947\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<h5><strong>Main function<\/strong><\/h5>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-ab0a5d0 elementor-widget elementor-widget-code-highlight\" data-id=\"ab0a5d0\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<div class=\"prismjs-tomorrow copy-to-clipboard\">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-cpp\">\n\t\t\t\t<code readonly=\"true\" class=\"language-cpp\">\n\t\t\t\t\t<xmp>int main(int argc, char **argv)\r\n{\r\n  rclcpp::init(argc, argv);\r\n  auto node = std::make_shared<SimpleServiceServer>();\r\n  rclcpp::spin(node);\r\n  rclcpp::shutdown();\r\n  return 0;\r\n}\r\n<\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-4817b07 elementor-widget elementor-widget-text-editor\" data-id=\"4817b07\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<ul><li data-start=\"3402\" data-end=\"3461\"><p data-start=\"3404\" data-end=\"3461\"><code data-start=\"3404\" data-end=\"3430\">rclcpp::init(argc, argv)<\/code>: Initializes the ROS 2 system.<br \/><br \/><\/p><\/li><li data-start=\"3462\" data-end=\"3547\"><p data-start=\"3464\" data-end=\"3547\"><code data-start=\"3464\" data-end=\"3505\">std::make_shared&lt;SimpleServiceServer&gt;()<\/code>: Creates an instance of the service node.<br \/><br \/><\/p><\/li><li data-start=\"3548\" data-end=\"3644\"><p data-start=\"3550\" data-end=\"3644\"><code data-start=\"3550\" data-end=\"3570\">rclcpp::spin(node)<\/code>: Starts the event loop so the node can process incoming service requests.<br \/><br \/><\/p><\/li><li data-start=\"3645\" data-end=\"3719\"><p data-start=\"3647\" data-end=\"3719\"><code data-start=\"3647\" data-end=\"3667\">rclcpp::shutdown()<\/code>: Shuts down ROS 2 cleanly when the node is stopped.<\/p><\/li><\/ul>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-b066648 elementor-widget elementor-widget-text-editor\" data-id=\"b066648\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<h3 data-start=\"5448\" data-end=\"5479\"><strong>\u2699\ufe0f Add it to CMakeLists.txt<\/strong><\/h3><p data-start=\"5481\" data-end=\"5497\">Make sure to register this executable:<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-ef3cef0 elementor-widget elementor-widget-code-highlight\" data-id=\"ef3cef0\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<div class=\"prismjs-tomorrow copy-to-clipboard\">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-bash\">\n\t\t\t\t<code readonly=\"true\" class=\"language-bash\">\n\t\t\t\t\t<xmp>add_executable(simple_service_server src\/simple_service_server.cpp)\r\nament_target_dependencies(simple_service_server rclcpp arduinobot_msgs)\r\ninstall(TARGETS simple_service_server DESTINATION lib\/${PROJECT_NAME})\r\n<\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-6c7872d elementor-widget elementor-widget-text-editor\" data-id=\"6c7872d\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>Also, don\u2019t forget to declare dependencies in <code data-start=\"4513\" data-end=\"4526\">package.xml<\/code><\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-a03cf66 elementor-widget elementor-widget-text-editor\" data-id=\"a03cf66\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<h3 data-start=\"4534\" data-end=\"4563\">\ud83d\ude80 Build and Run Your Node<\/h3><p data-start=\"4565\" data-end=\"4591\">Let\u2019s put it all together:<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-373c6bd elementor-widget elementor-widget-code-highlight\" data-id=\"373c6bd\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<div class=\"prismjs-tomorrow copy-to-clipboard\">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-bash\">\n\t\t\t\t<code readonly=\"true\" class=\"language-bash\">\n\t\t\t\t\t<xmp># Build your workspace\r\ncd ~\/arduinobot_ws\r\ncolcon build\r\n\r\n# Source it\r\n. install\/setup.bash\r\n\r\n# Run your service node\r\nros2 run arduinobot_cpp_examples simple_service_server\r\n<\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-0332979 elementor-widget elementor-widget-text-editor\" data-id=\"0332979\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<h3 data-start=\"4930\" data-end=\"4960\">\ud83e\uddea Test the Service via CLI<\/h3><p data-start=\"4962\" data-end=\"4990\">Open a new terminal and run:<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-11a11a7 elementor-widget elementor-widget-code-highlight\" data-id=\"11a11a7\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<div class=\"prismjs-tomorrow copy-to-clipboard\">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-bash\">\n\t\t\t\t<code readonly=\"true\" class=\"language-bash\">\n\t\t\t\t\t<xmp>. install\/setup.bash\r\nros2 service list\r\n<\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-face3f2 elementor-widget elementor-widget-text-editor\" data-id=\"face3f2\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p data-start=\"5044\" data-end=\"5075\">You should see <code data-start=\"5059\" data-end=\"5074\">\/add_two_ints<\/code>.<\/p><p data-start=\"5077\" data-end=\"5112\">Now test it by calling the service:<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-2941502 elementor-widget elementor-widget-code-highlight\" data-id=\"2941502\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<div class=\"prismjs-tomorrow copy-to-clipboard\">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-bash\">\n\t\t\t\t<code readonly=\"true\" class=\"language-bash\">\n\t\t\t\t\t<xmp>ros2 service call \/add_two_ints arduinobot_msgs\/srv\/AddTwoInts \"{a: 4, b: 6}\"\r\n<\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-6f5a12b elementor-widget elementor-widget-text-editor\" data-id=\"6f5a12b\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>You should get:<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-8026f9c elementor-widget elementor-widget-code-highlight\" data-id=\"8026f9c\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<div class=\"prismjs-tomorrow copy-to-clipboard\">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-bash\">\n\t\t\t\t<code readonly=\"true\" class=\"language-bash\">\n\t\t\t\t\t<xmp>requester: making request: arduinobot_msgs.srv.AddTwoInts.Request(a=4, b=6)\r\nresponse:\r\n  sum: 10\r\n<\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t<div class=\"elementor-element elementor-element-c5d20e7 e-con-full e-flex e-con e-parent\" data-id=\"c5d20e7\" data-element_type=\"container\" data-e-type=\"container\">\n\t\t\t\t<div class=\"elementor-element elementor-element-0741169 elementor-bg-transform elementor-bg-transform-move-left elementor-cta--layout-image-left elementor-cta--mobile-layout-image-above elementor-cta--skin-classic elementor-animated-content elementor-widget elementor-widget-call-to-action\" data-id=\"0741169\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"call-to-action.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<div class=\"elementor-cta\">\n\t\t\t\t\t<div class=\"elementor-cta__bg-wrapper\">\n\t\t\t\t<div class=\"elementor-cta__bg elementor-bg\" style=\"background-image: url(https:\/\/learnbydoing.dev\/wp-content\/uploads\/2025\/06\/manipulation-ros2.webp);\" role=\"img\" aria-label=\"manipulation ros2\"><\/div>\n\t\t\t\t<div class=\"elementor-cta__bg-overlay\"><\/div>\n\t\t\t<\/div>\n\t\t\t\t\t\t\t<div class=\"elementor-cta__content\">\n\t\t\t\t\n\t\t\t\t\t\t\t\t\t<h2 class=\"elementor-cta__title elementor-cta__content-item elementor-content-item\">\n\t\t\t\t\t\tWant to learn more?\t\t\t\t\t<\/h2>\n\t\t\t\t\n\t\t\t\t\t\t\t\t\t<div class=\"elementor-cta__description elementor-cta__content-item elementor-content-item\">\n\t\t\t\t\t\tExplore all the ROS 2 services in the \"Robotics and ROS 2 - Learn by Doing! Manipulators\" course\t\t\t\t\t<\/div>\n\t\t\t\t\n\t\t\t\t\t\t\t\t\t<div class=\"elementor-cta__button-wrapper elementor-cta__content-item elementor-content-item\">\n\t\t\t\t\t<a class=\"elementor-cta__button elementor-button elementor-size-\" href=\"\" target=\"_blank\">\n\t\t\t\t\t\tEnroll Now\t\t\t\t\t<\/a>\n\t\t\t\t\t<\/div>\n\t\t\t\t\t\t\t<\/div>\n\t\t\t\t\t\t\t<div class=\"elementor-ribbon elementor-ribbon-right\">\n\t\t\t\t<div class=\"elementor-ribbon-inner\">\n\t\t\t\t\tDISCOUNT\t\t\t\t<\/div>\n\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-180426a elementor-widget elementor-widget-spacer\" data-id=\"180426a\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"spacer.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<div class=\"elementor-spacer\">\n\t\t\t<div class=\"elementor-spacer-inner\"><\/div>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<\/div>","protected":false},"excerpt":{"rendered":"<p>In robotics, especially with ROS 2, it\u2019s common for different nodes to interact and collaborate to accomplish tasks. So far, we\u2019ve used the publisher\/subscriber model to communicate between them \u2014 which works well for data streaming. But what if you just need to trigger a specific operation and get a result back? That\u2019s where Services [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":3051,"comment_status":"closed","ping_status":"open","sticky":false,"template":"elementor_theme","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[45,43],"tags":[126,84,83,179,66,74,247,131,100,75,71,107,72,246,245],"class_list":["post-3045","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ros-2","category-tutorials","tag-base","tag-c","tag-cpp","tag-lbd","tag-learn-by-doing","tag-linux","tag-maniplators","tag-node","tag-robot","tag-robotics","tag-ros","tag-ros-2","tag-ros2","tag-server","tag-service"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.2 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to Build and Test a ROS 2 Service Server in C++ - Learn by Doing!<\/title>\n<meta name=\"description\" content=\"Learn how to build and test a ROS 2 Service Server in C++. Write the service definition, implement the node, and run it with ROS 2 tools.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/learnbydoing.dev\/es\/how-to-build-and-test-a-ros-2-service-server-in-cpp\/\" \/>\n<meta property=\"og:locale\" content=\"es_ES\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Build and Test a ROS 2 Service Server in C++\" \/>\n<meta property=\"og:description\" content=\"Learn by Doing!\" \/>\n<meta property=\"og:url\" content=\"https:\/\/learnbydoing.dev\/es\/how-to-build-and-test-a-ros-2-service-server-in-cpp\/\" \/>\n<meta property=\"og:site_name\" content=\"Learn by Doing!\" \/>\n<meta property=\"article:published_time\" content=\"2025-07-24T10:00:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-01-10T22:10:54+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/learnbydoing.dev\/wp-content\/uploads\/2025\/07\/Services-Server-_1_.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1920\" \/>\n\t<meta property=\"og:image:height\" content=\"1080\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Escrito por\" \/>\n\t<meta name=\"twitter:data1\" content=\"\" \/>\n\t<meta name=\"twitter:label2\" content=\"Tiempo de lectura\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutos\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/learnbydoing.dev\/how-to-build-and-test-a-ros-2-service-server-in-cpp\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/learnbydoing.dev\/how-to-build-and-test-a-ros-2-service-server-in-cpp\/\"},\"author\":{\"name\":\"\",\"@id\":\"\"},\"headline\":\"How to Build and Test a ROS 2 Service Server in C++\",\"datePublished\":\"2025-07-24T10:00:00+00:00\",\"dateModified\":\"2026-01-10T22:10:54+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/learnbydoing.dev\/how-to-build-and-test-a-ros-2-service-server-in-cpp\/\"},\"wordCount\":798,\"publisher\":{\"@id\":\"https:\/\/learnbydoing.dev\/es\/#organization\"},\"image\":{\"@id\":\"https:\/\/learnbydoing.dev\/how-to-build-and-test-a-ros-2-service-server-in-cpp\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/learnbydoing.dev\/wp-content\/uploads\/2025\/07\/Services-Server-_1_.webp\",\"keywords\":[\"Base\",\"c++\",\"cpp\",\"lbd\",\"learn by doing\",\"Linux\",\"maniplators\",\"node\",\"robot\",\"Robotics\",\"ROS\",\"ROS 2\",\"ROS2\",\"server\",\"service\"],\"articleSection\":[\"ROS 2\",\"Tutorials\"],\"inLanguage\":\"es\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/learnbydoing.dev\/how-to-build-and-test-a-ros-2-service-server-in-cpp\/\",\"url\":\"https:\/\/learnbydoing.dev\/how-to-build-and-test-a-ros-2-service-server-in-cpp\/\",\"name\":\"How to Build and Test a ROS 2 Service Server in C++ - Learn by Doing!\",\"isPartOf\":{\"@id\":\"https:\/\/learnbydoing.dev\/es\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/learnbydoing.dev\/how-to-build-and-test-a-ros-2-service-server-in-cpp\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/learnbydoing.dev\/how-to-build-and-test-a-ros-2-service-server-in-cpp\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/learnbydoing.dev\/wp-content\/uploads\/2025\/07\/Services-Server-_1_.webp\",\"datePublished\":\"2025-07-24T10:00:00+00:00\",\"dateModified\":\"2026-01-10T22:10:54+00:00\",\"description\":\"Learn how to build and test a ROS 2 Service Server in C++. Write the service definition, implement the node, and run it with ROS 2 tools.\",\"breadcrumb\":{\"@id\":\"https:\/\/learnbydoing.dev\/how-to-build-and-test-a-ros-2-service-server-in-cpp\/#breadcrumb\"},\"inLanguage\":\"es\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/learnbydoing.dev\/how-to-build-and-test-a-ros-2-service-server-in-cpp\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"es\",\"@id\":\"https:\/\/learnbydoing.dev\/how-to-build-and-test-a-ros-2-service-server-in-cpp\/#primaryimage\",\"url\":\"https:\/\/learnbydoing.dev\/wp-content\/uploads\/2025\/07\/Services-Server-_1_.webp\",\"contentUrl\":\"https:\/\/learnbydoing.dev\/wp-content\/uploads\/2025\/07\/Services-Server-_1_.webp\",\"width\":1920,\"height\":1080},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/learnbydoing.dev\/how-to-build-and-test-a-ros-2-service-server-in-cpp\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/learnbydoing.dev\/es\/learn-by-doing-es\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Build and Test a ROS 2 Service Server in C++\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/learnbydoing.dev\/es\/#website\",\"url\":\"https:\/\/learnbydoing.dev\/es\/\",\"name\":\"Learn by Doing!\",\"description\":\"Learn Robotics the fun way\",\"publisher\":{\"@id\":\"https:\/\/learnbydoing.dev\/es\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/learnbydoing.dev\/es\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"es\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/learnbydoing.dev\/es\/#organization\",\"name\":\"Learn by Doing!\",\"url\":\"https:\/\/learnbydoing.dev\/es\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"es\",\"@id\":\"https:\/\/learnbydoing.dev\/es\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/learnbydoing.dev\/wp-content\/uploads\/2025\/06\/cropped-cropped-cropped-Progetto-senza-titolo-6-1.png\",\"contentUrl\":\"https:\/\/learnbydoing.dev\/wp-content\/uploads\/2025\/06\/cropped-cropped-cropped-Progetto-senza-titolo-6-1.png\",\"width\":512,\"height\":512,\"caption\":\"Learn by Doing!\"},\"image\":{\"@id\":\"https:\/\/learnbydoing.dev\/es\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.linkedin.com\/in\/antonio-brandi-512166bb\/\"]},{\"@type\":\"Person\",\"@id\":\"\",\"url\":\"https:\/\/learnbydoing.dev\/es\/author\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Build and Test a ROS 2 Service Server in C++ - Learn by Doing!","description":"Learn how to build and test a ROS 2 Service Server in C++. Write the service definition, implement the node, and run it with ROS 2 tools.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/learnbydoing.dev\/es\/how-to-build-and-test-a-ros-2-service-server-in-cpp\/","og_locale":"es_ES","og_type":"article","og_title":"How to Build and Test a ROS 2 Service Server in C++","og_description":"Learn by Doing!","og_url":"https:\/\/learnbydoing.dev\/es\/how-to-build-and-test-a-ros-2-service-server-in-cpp\/","og_site_name":"Learn by Doing!","article_published_time":"2025-07-24T10:00:00+00:00","article_modified_time":"2026-01-10T22:10:54+00:00","og_image":[{"width":1920,"height":1080,"url":"https:\/\/learnbydoing.dev\/wp-content\/uploads\/2025\/07\/Services-Server-_1_.webp","type":"image\/webp"}],"twitter_card":"summary_large_image","twitter_misc":{"Escrito por":"","Tiempo de lectura":"6 minutos"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/learnbydoing.dev\/how-to-build-and-test-a-ros-2-service-server-in-cpp\/#article","isPartOf":{"@id":"https:\/\/learnbydoing.dev\/how-to-build-and-test-a-ros-2-service-server-in-cpp\/"},"author":{"name":"","@id":""},"headline":"How to Build and Test a ROS 2 Service Server in C++","datePublished":"2025-07-24T10:00:00+00:00","dateModified":"2026-01-10T22:10:54+00:00","mainEntityOfPage":{"@id":"https:\/\/learnbydoing.dev\/how-to-build-and-test-a-ros-2-service-server-in-cpp\/"},"wordCount":798,"publisher":{"@id":"https:\/\/learnbydoing.dev\/es\/#organization"},"image":{"@id":"https:\/\/learnbydoing.dev\/how-to-build-and-test-a-ros-2-service-server-in-cpp\/#primaryimage"},"thumbnailUrl":"https:\/\/learnbydoing.dev\/wp-content\/uploads\/2025\/07\/Services-Server-_1_.webp","keywords":["Base","c++","cpp","lbd","learn by doing","Linux","maniplators","node","robot","Robotics","ROS","ROS 2","ROS2","server","service"],"articleSection":["ROS 2","Tutorials"],"inLanguage":"es"},{"@type":"WebPage","@id":"https:\/\/learnbydoing.dev\/how-to-build-and-test-a-ros-2-service-server-in-cpp\/","url":"https:\/\/learnbydoing.dev\/how-to-build-and-test-a-ros-2-service-server-in-cpp\/","name":"How to Build and Test a ROS 2 Service Server in C++ - Learn by Doing!","isPartOf":{"@id":"https:\/\/learnbydoing.dev\/es\/#website"},"primaryImageOfPage":{"@id":"https:\/\/learnbydoing.dev\/how-to-build-and-test-a-ros-2-service-server-in-cpp\/#primaryimage"},"image":{"@id":"https:\/\/learnbydoing.dev\/how-to-build-and-test-a-ros-2-service-server-in-cpp\/#primaryimage"},"thumbnailUrl":"https:\/\/learnbydoing.dev\/wp-content\/uploads\/2025\/07\/Services-Server-_1_.webp","datePublished":"2025-07-24T10:00:00+00:00","dateModified":"2026-01-10T22:10:54+00:00","description":"Learn how to build and test a ROS 2 Service Server in C++. Write the service definition, implement the node, and run it with ROS 2 tools.","breadcrumb":{"@id":"https:\/\/learnbydoing.dev\/how-to-build-and-test-a-ros-2-service-server-in-cpp\/#breadcrumb"},"inLanguage":"es","potentialAction":[{"@type":"ReadAction","target":["https:\/\/learnbydoing.dev\/how-to-build-and-test-a-ros-2-service-server-in-cpp\/"]}]},{"@type":"ImageObject","inLanguage":"es","@id":"https:\/\/learnbydoing.dev\/how-to-build-and-test-a-ros-2-service-server-in-cpp\/#primaryimage","url":"https:\/\/learnbydoing.dev\/wp-content\/uploads\/2025\/07\/Services-Server-_1_.webp","contentUrl":"https:\/\/learnbydoing.dev\/wp-content\/uploads\/2025\/07\/Services-Server-_1_.webp","width":1920,"height":1080},{"@type":"BreadcrumbList","@id":"https:\/\/learnbydoing.dev\/how-to-build-and-test-a-ros-2-service-server-in-cpp\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/learnbydoing.dev\/es\/learn-by-doing-es\/"},{"@type":"ListItem","position":2,"name":"How to Build and Test a ROS 2 Service Server in C++"}]},{"@type":"WebSite","@id":"https:\/\/learnbydoing.dev\/es\/#website","url":"https:\/\/learnbydoing.dev\/es\/","name":"Learn by Doing!","description":"Learn Robotics the fun way","publisher":{"@id":"https:\/\/learnbydoing.dev\/es\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/learnbydoing.dev\/es\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"es"},{"@type":"Organization","@id":"https:\/\/learnbydoing.dev\/es\/#organization","name":"Learn by Doing!","url":"https:\/\/learnbydoing.dev\/es\/","logo":{"@type":"ImageObject","inLanguage":"es","@id":"https:\/\/learnbydoing.dev\/es\/#\/schema\/logo\/image\/","url":"https:\/\/learnbydoing.dev\/wp-content\/uploads\/2025\/06\/cropped-cropped-cropped-Progetto-senza-titolo-6-1.png","contentUrl":"https:\/\/learnbydoing.dev\/wp-content\/uploads\/2025\/06\/cropped-cropped-cropped-Progetto-senza-titolo-6-1.png","width":512,"height":512,"caption":"Learn by Doing!"},"image":{"@id":"https:\/\/learnbydoing.dev\/es\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.linkedin.com\/in\/antonio-brandi-512166bb\/"]},{"@type":"Person","@id":"","url":"https:\/\/learnbydoing.dev\/es\/author\/"}]}},"_links":{"self":[{"href":"https:\/\/learnbydoing.dev\/es\/wp-json\/wp\/v2\/posts\/3045","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnbydoing.dev\/es\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnbydoing.dev\/es\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnbydoing.dev\/es\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/learnbydoing.dev\/es\/wp-json\/wp\/v2\/comments?post=3045"}],"version-history":[{"count":16,"href":"https:\/\/learnbydoing.dev\/es\/wp-json\/wp\/v2\/posts\/3045\/revisions"}],"predecessor-version":[{"id":5532,"href":"https:\/\/learnbydoing.dev\/es\/wp-json\/wp\/v2\/posts\/3045\/revisions\/5532"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/learnbydoing.dev\/es\/wp-json\/wp\/v2\/media\/3051"}],"wp:attachment":[{"href":"https:\/\/learnbydoing.dev\/es\/wp-json\/wp\/v2\/media?parent=3045"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnbydoing.dev\/es\/wp-json\/wp\/v2\/categories?post=3045"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnbydoing.dev\/es\/wp-json\/wp\/v2\/tags?post=3045"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}