title: creating daemon processing from server socket
body: <p>I have one socket, which act as server while executed and response some results.
  First I compile it: <code>g++ -o a daemon.cpp dictionary.cpp -lpthread c++11</code>
  then execute : <code>./a</code></p>
  
  <p>Now it will listen for the request on some port.</p>
  
  <p>I want that one I create the object file <code>a</code>, it should not manually executed. Rather work as daemon file, which continously listen for the request.</p>
  
  <p>I saw using <code>fork()</code> id something can be done. But I could not correct place in my below code:</p>
  
  <p>variable declation I have removed:</p>
  
  <pre><code>using namespace std;
  using namespace boost;
  void *SocketHandler(void *);
  
  int main(int argv, char **argc)
  {
      pthread_t thread_id = 0;
  
  
      hsock = socket(AF_INET, SOCK_STREAM, 0);
      if (hsock == -1) 
      {
          printf("Error initializing socket %dn", errno);
          goto FINISH;
      }
  
      p_int = (int *) malloc(sizeof(int));
      *p_int = 1;
  
      if ((setsockopt(hsock, SOL_SOCKET, SO_REUSEADDR, (char *) p_int, sizeof(int)) == -1) 
          || (setsockopt(hsock, SOL_SOCKET, SO_KEEPALIVE, (char *) p_int, sizeof(int)) == -1)) 
      {
          printf("Error setting options %dn", errno);
          free(p_int);
          goto FINISH;
      }
      free(p_int);
  
      my_addr.sin_family = AF_INET;
      my_addr.sin_port = htons(host_port);
  
      memset(&amp;(my_addr.sin_zero), 0, 8);
      my_addr.sin_addr.s_addr = INADDR_ANY;
  
      if (bind(hsock, (sockaddr *) &amp; my_addr, sizeof(my_addr)) == -1) 
      {
          fprintf(stderr, "Error binding to socket, make sure nothing else is listening on this port %dn", errno);
          goto FINISH;
      }
  
      if (listen(hsock, 10) == -1) 
      {
          fprintf(stderr, "Error listening %dn", errno);
          goto FINISH;
      }
      //Now lets do the server stuff
  
      addr_size = sizeof(sockaddr_in);
  
      int pid;
      pid_t pid=fork();
      if(pid&lt;0)
            exit(EXIT_FAILURE);    
      else if(pid&gt;0){
      //this is parent process, exit now
           exit(EXIT_SUCCESS); // again no goto
     }
     else{
          //this is child or daemon
         unmask();
     pid_t childid = setsid();
  
      while (true) 
      {
          printf("waiting for a connectionn\n");
          csock = (int *) malloc(sizeof(int));
          if ((*csock = accept(hsock, (sockaddr *) &amp; sadr, &amp;addr_size)) != -1) 
          {
              printf("---------------------nReceived connection from %s\n", inet_ntoa(sadr.sin_addr));
              pthread_create(&amp;thread_id, 0, &amp;SocketHandler, (void *) csock);
              pthread_detach(thread_id);
          } 
          else 
          {
              fprintf(stderr, "Error accepting %dn", errno);
          }
      sleep(60);
      }
  
  FINISH:
      ;
  }
  
  void *SocketHandler(void *lp)
  {   
      char *ch;/* stores references to 50 words. */
      char *ch2[50] = { 0 };  
      char *excluded_string;
      char *word;
  
      if ((bytecount = recv(*csock, (char*) rcv.c_str(), rcv.length(), 0)) == -1) 
      { 
          fprintf(stderr, "Error receiving data %d \n", errno);
          goto FINISH;
      }    
  
      do 
      {
          bytesReceived = recv(*csock, buffer.data(), buffer.size(), 0);
          // append string from buffer.
  
          if ( bytesReceived == -1 ) 
          { 
              fprintf(stderr, "Error receiving data %d \n", errno);
              goto FINISH;
          } 
          else 
              rcv.append( buffer.cbegin(), buffer.cend() );
  
      } while ( bytesReceived == MAX_BUF_LENGTH );
  
      word = strtok(&amp; rcv[0]," ");
      while (word!= NULL) 
      {
          skp = BoyerMoore_skip(word, strlen(word) );
  
          if(skp != NULL)
          {
              i++;
              printf("this also \n");
              word = strtok(NULL, " ");
              continue;
          }
  
          printf("\n Word %s \n",word);
          bfr &lt;&lt; word &lt;&lt; " ";
          result_string = bfr.str();
          word = strtok(NULL, " ");
          j++; 
      }
  
      ss&lt;&lt;result_string;
  
      while (std::getline(ss, item, ' ')) 
      {
          writable.push_back(item);
      }
  
      for (std::vector &lt; std::string &gt;::iterator it = writable.begin(); it != writable.end(); it++)
          ++src[*it];
  
      std::transform(src.begin(), src.end(), std::inserter(dst, dst.begin()), mytransform);
  
      rec=dst.begin();   
      for (auto it = dst.begin(); it != dst.end(); ++it)
          std::cout &lt;&lt; it-&gt;second &lt;&lt; ":" &lt;&lt; it-&gt;first &lt;&lt; std::endl;
  
  
      if ((bytecount = send(*csock, (char *)ar, i *sizeof(int), 0)) == -1) 
      { // Here we cant send lenth-1. It consider exact
          fprintf(stderr, "Error sending data %d\n", errno);
          goto FINISH;
      }
  
  FINISH:
      free(csock);
      return 0;
  }
  </code></pre>