How to read a topic for a external program?
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]);
exit(-1);
}
if ((he=gethostbyname(argv[1]))==NULL){
/* llamada a gethostbyname() */
printf("gethostbyname() error\n");
exit(-1);
}
if ((fd=socket(AF_INET, SOCK_STREAM, 0))==-1){
/* llamada a socket() */
printf("socket() error\n");
exit(-1);
}
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" */
bzero(&(server.sin_zero),8);
if(connect(fd, (struct sockaddr *)&server,
sizeof(struct sockaddr))==-1){
/* llamada a connect() */
printf("connect() error\n");
exit(-1);
}
else
{
printf("CONECT_ok!\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");
exit(-1);
}
buf[numbytes]='\0';
printf("Mensaje del Servidor: %s\n",buf);
/* muestra el mensaje de bienvenida del servidor =) */
while(1);
close(fd); /* cerramos fd =) */
}
I build with gcc. and I launch with ./client 127.0.0.1 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:
CONECT_ok!
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