How to terminate the connection of my service client to the server

I have a client that does its job but keeps listening to the server, how do I cut off the connection?
ROS 1 Noetic, with Python.

Hi @jacob17.y ,

It actually depends upon the way the client is designed.

  1. If the client is designed to run forever, then you will have to force terminate it by doing:
    a. Pressing Ctrl+C on the terminal it is running on, if it was launched by you.
    b. Calling rosnode kill <action_client_node_name> if it is running from another process that you did not start.

  2. By setting up a termination condition if the program was designed by you.
    a. You can send an β€œEND” string, to finish up the client (and server) if you are running the client to send requests as string. And, you can use β€œ-1” or β€œ-1.0” if you are using int or float messages.
    b. Or you can shut it down from another function from the same program, if you have some other process along with the service client.

  3. If the program was not designed by you, then look up the documentation on how to shut it down.

There are several ways to do this, but it is your thought on how to implement this.

Regards,
Girish

1 Like

Thank You @girishkumar.kannan
Yes, it was designed by me. How do i write this kind of condition in my server?

Hi @jacob17.y ,

Since you are working with ROS1, the way to implement a shutdown of server would be with two steps.

Step 1:
Create a shutdown condition in the server. Check for service with request for β€œEND” or -1 or -1.0.
Once you get this data in the request, within the condition, use the function to shutdown the service.
~ ~ ~ ~ ~
With Python: service.shutdown(reason="Your reason for shutdown") and later shut down the node (refer link below).
Refer Python: rospy: (Waiting for) shutdown, rospy: Shutting down a node
~ ~ ~ ~ ~
With C++: ros::shutdown(); from the node that runs the service.
Refer C++: roscpp: Shutting down a node
~ ~ ~ ~ ~

Step 2:
Once you send a request to shutdown server, you will only get a one final response. You will not get any more responses from the service once the server is successfully shutdown.
So after you get the response for shutdown request, you can do:
rosnode kill <your_service_server_node_name> from your service client.
Either use os.system() or subprocess.Popen() to do that in Python.
If you are using C++, you can use the system(...) function, or use your own process stream.

Regards,
Girish

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.