Rosanswers logo

Hi and thanks for your attention. I want create a program in C++. This program is external to ROS. But I want that this program can be read and work with ROS TOPICS. For example, I have a node that calculate the distance that walk my robot, and publish a topic with this data. And now I want that my external-program read this topic. Any idea or example for resolve my question? Thanks for all.

EDIT------------------------------------- Ok, I can read topics out of ROS with client.http or client.python but now I want a client.c THIS is my client.c :

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>        
/* netbd.h es necesitada por la estructura hostent ;-) */

#define PORT 9090         
/* El Puerto Abierto del nodo remoto */

#define MAXDATASIZE 100   
/* El número máximo de datos en bytes */

int main(int argc, char *argv[])
   int fd, numbytes;       
   /* ficheros descriptores */

   char buf[MAXDATASIZE];  
   /* en donde es almacenará el texto recibido */

   struct hostent *he;         
   /* estructura que recibirá información sobre el nodo remoto */

   struct sockaddr_in server;  
   /* información sobre la dirección del servidor */

   if (argc !=2) { 
      /* esto es porque nuestro programa sólo necesitará un
      argumento, (la IP) */
      printf("Uso: %s <Dirección IP>\n",argv[0]);

   if ((he=gethostbyname(argv[1]))==NULL){       
      /* llamada a gethostbyname() */
      printf("gethostbyname() error\n");

   if ((fd=socket(AF_INET, SOCK_STREAM, 0))==-1){  
      /* llamada a socket() */
      printf("socket() error\n");

   server.sin_family = AF_INET;
   server.sin_port = htons(PORT); 
   /* htons() es necesaria nuevamente ;-o */
   server.sin_addr = *((struct in_addr *)he->h_addr);  
   /*he->h_addr pasa la información de ``*he'' a "h_addr" */

   if(connect(fd, (struct sockaddr *)&server,
      sizeof(struct sockaddr))==-1){ 
      /* llamada a connect() */
      printf("connect() error\n");
   char* message = "raw\r\n\r\n";
   int bytes = send(fd,message, strlen(message),0 );
   printf("bytes = %d\n", bytes);
   if ((bytes) == -1)
       printf("Error send \n");

   if ((numbytes=recv(fd,buf,MAXDATASIZE,0)) == -1){  
      /* llamada a recv() */
      printf("Error recv() \n");


   printf("Mensaje del Servidor: %s\n",buf); 
   /* muestra el mensaje de bienvenida del servidor =) */


   close(fd);   /* cerramos fd =) */


I build with gcc. and I launch with ./client or ./client localhost The steps that I do are:

1 console: roscore

2 console: roslaunch rosbridge_server rosbridge_websocket.launch

3 console: ./client

When I execute ./client, the printing of progrm:


bytes = 7

Mensaje del Servidor:

But the console 2 no say nothing. I think that this is for the handshake or similar, but I don´t know that do now. Ps: I use Ubuntu 12.04, Ros Hydro and rosbridge 2.0

Originally posted by alex.filgueira on ROS Answers with karma: 62 on 2014-04-11

Post score: 0


1 Answer 1


Rosanswers logo

There is rosc, a dependecy free C implementation of the ROS middle ware: http://wiki.ros.org/rosc

Maybe even better suited for you could be ros_bridge, which exposes ros topics as a JSON API, so anything talking http and JSON can receive messages: http://wiki.ros.org/rosbridge_suite

Originally posted by demmeln with karma: 4306 on 2014-04-11

This answer was ACCEPTED on the original site

Post score: 1

Original comments

Comment by alex.filgueira on 2014-04-13:
Ok, thanks. I prove these method and coment the results.

Comment by alex.filgueira on 2014-04-15:
Ok, ros_bridge can be a good idea for my problem, but I search and search and the all examples are in html. Someone have a example that can share in other language, example: c, c++, python? Thanks.

Comment by demmeln on 2014-04-15:
On the rosbridge webpackage there is a "simple python client" example: (http://rosbridge.org/doku.php?id=writing_a_rosbridge_client_in_python )

Comment by demmeln on 2014-04-15:
Also check out the answers to this question, for something in java http://answers.ros.org/question/152806/rosbridge-20-in-java/

Comment by alex.filgueira on 2014-04-24:
Ok, I did a client.python, but now I want a client.c . I edit the first post with this question. Thanks!


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.