# Testing Publisher and Subscriber nodes

$ cd catkin\_ws $ catkin\_make $ source devel/setup.bash

$ roscore $ rosrun beginner\_tutorials talker.py $ rosrun beginner\_tutorials listener.py

Rospy Service & Client

Step1: Define the service message(srv file) Step2: Create a ROS server node Step3: Create a ROS client node\
Step4: Execute the service Step5: Consume the service by the client

Create srv folder inside beginner\_tutorials. Make a service file AddTwoInts.srv inside srv folder.

int64 a

int64 b

\---

int64 sum

Make changes in package.xml by adding these 2 lines, These lines will generate request and response It is responsible for reading service file and converting it to source code.

&#x20;message\_generation

&#x20;message\_runtime

Make changes in CMakelist.txt

Inside find\_package Add the following line:

message\_generation

Add Service file

add\_service\_files( FILES AddTwoInts.srv )

After making changes in package.xml and CMakelist.txt, Build your package

catkin\_make source devel/setup.bash

Make a file add\_two\_ints\_server.py inside scripts folder

```
#!/usr/bin/env python

from beginner_tutorials.srv import *
import rospy

def handle_add_two_ints(req):
    print "Returning [%s + %s = %s]"%(req.a, req.b, (req.a + req.b))
    return AddTwoIntsResponse(req.a + req.b)

def add_two_ints_server():
    rospy.init_node('add_two_ints_server')
    s = rospy.Service('add_two_ints', AddTwoInts, handle_add_two_ints)
    print "Ready to add two ints."
    rospy.spin()

if __name__ == "__main__":
    add_two_ints_server()


```

make the script executable by chmod +x add\_two\_ints\_server.py


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ptandur14.gitbook.io/everything-about-drone/ros-robotic-operating-system/examples/testing-publisher-and-subscriber-nodes.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
