title: socket() returns 0 in C client server application
body: <p>I'm working on an application that contains several server sockets that each run in a unique thread.<br/>
  An external utility (script) is called by one of the threads.  This script calls a utility (client) that sends a message to one of the server sockets.</p>
  
  <p>Initially, I was using <code>system()</code> to execute this external script, but we couldn't use that because we had to make sure the server sockets were closed in the child that was forked to execute the external script.<br/>
  I now call <code>fork()</code> and <code>execvp()</code> myself.  I <code>fork()</code> and then in the child I close all the server sockets and then call <code>execvp()</code> to execute the script.</p>
  
  <p>Now, all of that works fine.  The problem is that at times the script reports errors to the server app.  The script sends these errors by calling another application (client) which opens a TCP socket and sends the appropriate data.  My issue is that the client app gets a value of <code>0</code> returned by the <code>socket()</code> system call.</p>
  
  <p>NOTE: This ONLY occurs when the script/client app is called using my forkExec() function.  If the script/client app is called manually the <code>socket()</code> call performs appropriately and things work fine.</p>
  
  <p>Based on that information I suspect it's something in my fork() execvp() code below... Any ideas?</p>
  
  <pre><code>void forkExec()
  {    
      int stat;
  
      stat = fork();
      if (stat &lt; 0)
      {
          printf("Error forking child: %s", strerror(errno));
      }
      else if (stat == 0)
      {
          char *progArgs[3];
  
          /*
           * First, close the file descriptors that the child 
           * shouldn't keep open
           */
          close(ServerFd);
          close(XMLSocket);
          close(ClientFd);
          close(EventSocket);
          close(monitorSocket);
  
          /* build the arguments for script */
          progArgs[0] = calloc(1, strlen("/path_to_script")+1);
          strcpy(progArgs[0], "/path_to_script");
          progArgs[1] = calloc(1, strlen(arg)+1);
          strcpy(progArgs[1], arg);
          progArgs[2] = NULL; /* Array of args must be NULL terminated for execvp() */
  
          /* launch the script */
          stat = execvp(progArgs[0], progArgs);
          if (stat != 0)
          {
              printf("Error executing script: '%s' '%s' : %s", progArgs[0], progArgs[1], strerror(errno));
          }
          free(progArgs[0]);
          free(progArgs[1]);
          exit(0);
      }
  
      return;
  }
  </code></pre>
  
  <p>Client app code:</p>
  
  <pre><code>static int connectToServer(void)
  {
  int socketFD = 0;
  int status;
  struct sockaddr_in address;
  struct hostent* hostAddr = gethostbyname("localhost");
  
  socketFD = socket(PF_INET, SOCK_STREAM, 0);
  </code></pre>
  
  <p><strong>The above call returns 0.</strong></p>
  
  <pre><code>if (socketFD &lt; 0)
  {
      fprintf(stderr, "%s-%d: Failed to create socket: %s", 
                                  __func__, __LINE__, strerror(errno));
      return (-1);
  }
  
  memset(&amp;address, 0, sizeof(struct sockaddr));
  address.sin_family = AF_INET;
  memcpy(&amp;(address.sin_addr.s_addr), hostAddr-&gt;h_addr, hostAddr-&gt;h_length);
  address.sin_port = htons(POLLING_SERVER_PORT);
  
  status = connect(socketFD, (struct sockaddr *)&amp;address, sizeof(address));
  if (status &lt; 0)
  {
      if (errno != ECONNREFUSED)
      {
          fprintf(stderr, "%s-%d: Failed to connect to server socket: %s",
                     __func__, __LINE__, strerror(errno));
      }
      else
      {
          fprintf(stderr, "%s-%d: Server not yet available...%s",
                     __func__, __LINE__, strerror(errno));
          close(socketFD);
          socketFD = 0;
      }
  }
  
  return socketFD;
  }
  </code></pre>
  
  <p><strong>FYI</strong><br/>
  OS: Linux<br/>
  Arch: ARM32<br/>
  Kernel: 2.6.26<br/></p>