tcp client for UR communication (popup generation) [closed]

asked 2016-02-16 10:28:27 -0600

altella gravatar image

Hello all;

I am writing a simple TCP client node so that I can connect to an UR robot, and send messages (popup XXXX) to the port number 29999, to generate popup messages in the UR screen. UR server does not respond correctly to the gethostbyaddr function when connecting, so, for testing, I connect directly to the IP and Port. The code of the simple TCP client is as follows:

#include "ros/ros.h"
#include "std_msgs/String.h"
#include "tekniker_tcp_comms/SendDataTCP.h"

#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h> 

const std::string SERVER_IP_DEFAULT = "";
const std::string SERVER_PORT_DEFAULT = "29999";

std::string serverIP;
std::string serverPort;
int sockfd, portno;
struct sockaddr_in serv_addr;

int initTCPSocket(void)
    struct in_addr ip;
    struct hostent *server;

    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0)
        ROS_INFO("ERROR opening socket");
        return -1;
    portno = atoi(serverPort.c_str());

    if (!inet_aton(serverIP.c_str(), &ip))
        ROS_INFO(" ERROR: error parsing IP address %s", serverIP.c_str());
    bzero((char *) &serv_addr, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = htons(ip.s_addr);
    serv_addr.sin_port = htons(portno);
    if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) 
        ROS_INFO("ERROR connecting to server");
        return -3;
        ROS_INFO("Connected to server");

bool SendDataTCP (tekniker_tcp_comms::SendDataTCP::Request &req, tekniker_tcp_comms::SendDataTCP::Response &res)
    if (sockfd < 0)
        ROS_INFO("ERROR in connection");
        return true;

    size_t msgLength =;
    //write to connected server.
    int n = write(sockfd,,(int)msgLength);
    if (n < 0) 
        ROS_INFO("ERROR writing to socket");
        ROS_INFO("write %d bytes. Message %s",n,;
    return true;

int main(int argc, char **argv)
    ros::init(argc, argv, "tcp_client_node");
    ros::NodeHandle nh;

    ROS_INFO ("tcp client node UP !!");

    if (!nh.getParam(ros::this_node::getName()+"/server_IP",serverIP)) serverIP = SERVER_IP_DEFAULT;
    if (!nh.getParam(ros::this_node::getName()+"/server_port",serverPort)) serverPort = SERVER_PORT_DEFAULT;    
    ros::ServiceServer srv = nh.advertiseService(ros::this_node::getName()+"/SendDataTCP", SendDataTCP);

    return 0;

I obtain messages of correct connection, and I can call the Service, but, no popup appears in the UR. It seems the UR does not receive correctly the messages. Connecting to the UR server for popup messages using ncat ( ncat 29999) and sending messages manually works also.

After debugging for some hours, I can not find any cause for this problem. I would appreciate a lot any kind of advise,

Thank you in advance,

edit retag flag offensive reopen merge delete

Closed for the following reason question is not relevant or outdated by altella
close date 2016-04-22 05:01:28.690979


If you haven't already done so, try your socket code stand-alone (so without any bits of ROS involved). That will make things a lot easier to debug. Make sure it works first, then re-integrate.

gvdhoorn gravatar image gvdhoorn  ( 2016-02-17 02:16:10 -0600 )edit