ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

wdsky2010's profile - activity

2022-07-28 03:06:11 -0500 received badge  Student (source)
2016-11-30 19:40:38 -0500 received badge  Famous Question (source)
2016-02-12 01:41:07 -0500 received badge  Notable Question (source)
2015-11-15 20:54:05 -0500 answered a question Handling SIGINT

Now I am facing the same problem just like yours, I wonder how did you solve it? Many thanks:-)

2015-11-14 10:18:05 -0500 commented answer problem with publish in fork() process

thank you again :-)

2015-11-13 01:09:02 -0500 received badge  Popular Question (source)
2015-11-12 20:49:46 -0500 commented answer problem with publish in fork() process

And what's more, I wonder if there is a good mechanism to make such a server, I want to make a server that can have more than one non-ROS clients, and the server should publish or subscribe topics. Thank you.

2015-11-12 20:28:33 -0500 commented answer problem with publish in fork() process

Firstly, many thanks for you, and according to your very appreciate explanation, I have soled this problem by multi-threads rather than using fork(). :-)

2015-11-12 20:20:16 -0500 received badge  Supporter (source)
2015-11-12 20:20:12 -0500 received badge  Scholar (source)
2015-11-11 10:10:33 -0500 asked a question problem with publish in fork() process

When I use fork() function, then the publish function of Publisher do not work, do any one who has met this problem, I am grateful that you can tell me something, thanks :-) the code

 #include "ros/ros.h"
#include "std_msgs/String.h"
#include <sstream>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

void error(const char *msg)
{
    perror(msg);
    exit(1);
}
/**
 * This tutorial demonstrates simple sending of messages over the ROS system.
 */
int main(int argc, char **argv)
{
  ros::init(argc, argv, "talker");

  ros::NodeHandle nh;

  ros::Publisher chatter_pub = nh.advertise<std_msgs::String>("chatter", 1000);

  int sockfd, newsockfd, portno, pid;
  socklen_t clilen;
  struct sockaddr_in serv_addr, cli_addr;

  if (argc < 2) {
      fprintf(stderr,"ERROR, no port provided\n");
      exit(1);
  }

  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd < 0)
     error("ERROR opening socket");
  bzero((char *) &serv_addr, sizeof(serv_addr));
  portno = atoi(argv[1]);
  //portno = 8888;
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_addr.s_addr = INADDR_ANY;
  serv_addr.sin_port = htons(portno);
  if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
      error("ERROR on binding");
  listen(sockfd,5);
  clilen = sizeof(cli_addr);
  while(ros::ok())
  {
      newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);   
      if (newsockfd < 0)
        error("ERROR on accept");
    pid = fork();
    if (pid < 0)
        error("ERROR on fork");
    if (pid == 0)  {
        close(sockfd);
        std_msgs::String msg;
       //ros::NodeHandle nh;
       //ros::Publisher chatter_pub = nh.advertise<std_msgs::String>("chatter", 1000);
       int n;
       char buffer[256];
       //while(ros::ok())
       //{         
           bzero(buffer,256);
          n = read(newsockfd,buffer,255);
          if (0 > n) error("ERROR reading from socket");
          else if(0 < n)
          {
              msg.data = buffer;
              ROS_INFO("%s", msg.data.c_str());
              chatter_pub.publish(msg);
              printf("Here is the message: %s\n",buffer);
              n = write(newsockfd,"I got your message",18);
              if (n < 0) error("ERROR writing to socket");
          }
          else ;
        //dostuff(newsockfd, chatter_pub);
        exit(0);
    }
    else close(newsockfd);
  }
  close(sockfd);
  return 0;
}
2015-11-11 10:10:33 -0500 asked a question problem within fork() process, publish do not work

When I use fork() function, then the publish function of Publisher do not work, do any one who has met this problem, I am grateful that you can tell me something, thanks :-) the code

 #include "ros/ros.h"
#include "std_msgs/String.h"
#include <sstream>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

void error(const char *msg)
{
    perror(msg);
    exit(1);
}
/**
 * This tutorial demonstrates simple sending of messages over the ROS system.
 */
int main(int argc, char **argv)
{
  ros::init(argc, argv, "talker");

  ros::NodeHandle nh;

  ros::Publisher chatter_pub = nh.advertise<std_msgs::String>("chatter", 1000);

  int sockfd, newsockfd, portno, pid;
  socklen_t clilen;
  struct sockaddr_in serv_addr, cli_addr;

  if (argc < 2) {
      fprintf(stderr,"ERROR, no port provided\n");
      exit(1);
  }

  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd < 0)
     error("ERROR opening socket");
  bzero((char *) &serv_addr, sizeof(serv_addr));
  portno = atoi(argv[1]);
  //portno = 8888;
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_addr.s_addr = INADDR_ANY;
  serv_addr.sin_port = htons(portno);
  if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
      error("ERROR on binding");
  listen(sockfd,5);
  clilen = sizeof(cli_addr);
  while(ros::ok())
  {
      newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);   
      if (newsockfd < 0)
        error("ERROR on accept");
    pid = fork();
    if (pid < 0)
        error("ERROR on fork");
    if (pid == 0)  {
        close(sockfd);
        std_msgs::String msg;
       //ros::NodeHandle nh;
       //ros::Publisher chatter_pub = nh.advertise<std_msgs::String>("chatter", 1000);
       int n;
       char buffer[256];
       //while(ros::ok())
       //{         
           bzero(buffer,256);
          n = read(newsockfd,buffer,255);
          if (0 > n) error("ERROR reading from socket");
          else if(0 < n)
          {
              msg.data = buffer;
              ROS_INFO("%s", msg.data.c_str());
              chatter_pub.publish(msg);
              printf("Here is the message: %s\n",buffer);
              n = write(newsockfd,"I got your message",18);
              if (n < 0) error("ERROR writing to socket");
          }
          else ;
        //dostuff(newsockfd, chatter_pub);
        exit(0);
    }
    else close(newsockfd);
  }
  close(sockfd);
  return 0;
}