title: I'm trying to create a tcp socket in one .c file and listen to it on another .c file. The connection attempt is failing
body: <p>So I've created a .c file which acts as a coordinator server. This accepts a clients request and forks out a process to execute a .c file that acts as the server that directly tends to the clients request.</p>
  
  <p>The coordinator listens on a udp port and receives the clients request. When it gets the request, it creates a tcp socket. It then forks a process which uses the execv() command to run the .c file of the other server. The socket is passed as a parameter. The .c file that has been executed by the child process will then use this socket, listen on it and attempt to accept the connection from the client.</p>
  
  <p>The client meanwhile, has received the port number of the tcp socket created by the coordinator and is attempting to connect to the new server.</p>
  
  <p>The issue is that the client fails when it reaches the connect() function. Can anyone help me figure out what's wrong?</p>
  
  <p>This is the code snippet in the coordinator where I fork out to execute the other server:</p>
  
  <pre><code>    int new_tcp_sock = setup_TCP();
  
      char cmd[5];
  
      sprintf(cmd, "%d", new_tcp_sock);
  
      char *argv[] = {cmd, NULL};
  
      if( (pid = fork()) == 0)
      {
          printf("%d\n", getpid());
          execv("./chat_server", argv);
      }
  </code></pre>
  
  <p>The setup_TCP() function merely creates the socket:</p>
  
  <pre><code>int setup_TCP()
  {
      printf("Session Server: Setting up server.\n");
  
      int b, s;
      struct sockaddr_in s1;
  
      bzero((char *)&amp;s1,sizeof(s1));
  
      s1.sin_family = AF_INET;
      s1.sin_addr.s_addr = inet_addr("127.0.0.1");
      s1.sin_port = htons(5024);
  
      s = socket(AF_INET, SOCK_STREAM, 0);
  
      if (s &lt; 0)
      {
          printf("Session Server: Error creating socket.\n");
          exit(1);
      }
  
      printf("Session Server: Reached after socket.\n");
  
      b = bind(s, (struct sockaddr *) &amp;s1, sizeof(s1));
  
      if(b &lt; 0)
      {
          printf("Session Server: Error binding socket. Attempting to bind to alternate port.\n");
          s1.sin_port = htons(0);
          if(bind(s, (struct sockaddr*)&amp;s1, sizeof(s1)) &lt; 0)
          {
              printf("Session Server: Error. Cannot bind.\n");
              exit(1);
          }
          else
          {
              int socklen = sizeof(s1);
  
              if(getsockname(s, (struct sockaddr*)&amp;s1, &amp;socklen) &lt; 0)
              {
                  printf("Session Server: Bind successful. Unable to get alternate port number.\n");
                  exit(1);
              }
              printf("Session Server: New server port number: %d\n", ntohs(s1.sin_port));
          }
      }
  
      printf("Session Server: After binding.\n");
  
      tcp_port = ntohs(s1.sin_port);
  }
  </code></pre>
  
  <p>In the .c file of the new server to be setup, I have the following:</p>
  
  <pre><code>int main(int argc, char *argv[])
  {
      int sock;
      char *host = "localhost";
      char *port_number = "5004";
      struct sockaddr_in server_addr;
      socklen_t server_addr_len;
  
      if( (sock = socket(PF_INET, SOCK_DGRAM, 0)) &lt; 0)
      {
              printf("Session server: Error creating socket.\n");
              exit(1);
      }
  
      server_addr = setup_UDP(host, port_number);
      server_addr_len = sizeof(server_addr);
  
      char buf[80];
      int a = tcp_listen(atoi(argv[1]));
  
      while(1)
      {
          read(a, buf, sizeof(buf)+1);
          write(a, buf, strlen(buf)+1);
      }
  }
  
  int tcp_listen(int socket)
  {
      int l;
      struct sockaddr_in client_addr;
      int addrlen = sizeof(client_addr);
  
      if( (l = listen(socket, 20)) &lt; 0)
      {
          printf("Error. Cannot listen.\n");
          exit(1);
      }
  
      printf("Listening\n");
  
      int csock = accept(socket, (struct sockaddr*)&amp;client_addr, &amp;addrlen);
  
      if(csock &lt; 0)
          printf("Error accepting request.\n");
      else
          printf("Accepted\n");
      int csock = 0;
  
      return csock;
  }
  </code></pre>
  
  <p>And finally, in the client, I firstly have this while loop to help communicate with coordinator and an inner while loop to talk to the other server:</p>
  
  <pre><code>while(fgets(buf, sizeof(buf), stdin))
      {
          if(strncmp(buf, "Start", 5) == 0 || strncmp(buf, "Find", 4) == 0)
          {
              w = sendto(sock, buf, strlen(buf) + 1, 0, (struct sockaddr *)&amp;server_addr, server_addr_len);
  
              if( (r = recvfrom(sock, buf, sizeof(buf), 0, (struct sockaddr *)&amp;server_addr, &amp;server_addr_len)) &lt; 0 )
              {
                  printf("Error reading back from server.\n");
                  exit(1);
              }
              else
              {
                  printf("Got: %s\n", buf);
                  int sock = chat_connect_function("127.0.0.1", buf);
                  while( fgets(buf, sizeof(buf), stdin) )
                  {
                      write(sock, buf, strlen(buf)+1);
                      read(sock, buf, sizeof(buf)+1);
                  }
              }
              fputs(buf, stdout);
          }
          else if(strncmp(buf, "Exit", 4) == 0)
          {
              exit(1);
          }
          else
          {
              printf("Invalid start of statement. Could not recognize command\n");
          }
      }
  </code></pre>
  
  <p>The chat_connect_function() in the while loop is supposed to connect to the tcp socket which should be accepted by the new server:</p>
  
  <pre><code>int chat_connect_function(char *host, char *port_number)
  {
      int s;
      struct hostent *host_info;
      struct sockaddr_in s1;
  
      bzero((char *)&amp;s1,sizeof(s1));
  
      s1.sin_family = AF_INET;
  
      if ((s1.sin_port=htons((unsigned short)atoi(port_number))) == 0)
      {
          printf("TCP: Problem with port number.\n");
          exit(1);
      }
  
      if( host_info = gethostbyname(host) )
      {
          memcpy(&amp;s1.sin_addr, host_info-&gt;h_addr, host_info-&gt;h_length);
      }
      else if ( (s1.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE )
      {
                  printf("TCP: Error. Can't get host entry of %s.\n", host);
                  exit(1);
          }
  
          if( (s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) &lt; 0)
          {
              printf("TCP: Error creating socket.\n");
              exit(1);
          }
  
          if (connect(s, (struct sockaddr *)&amp;s1, sizeof(s1)) &lt; 0)
          {
              printf("TCP: Error connecting to server.\n");
          }
          else
              printf("TCP: Connected to %s.\n", host);
  
          return s;
  }
  </code></pre>
  
  <p>I apologize if I've posted too much code, but I'm at my wits end and can't figure out why the client prints "TCP: Error connecting to server.\n". 
  Also, when I move the 'listen()' function from the code of the new server into the code of the coordinator, the client reports that the connection has been established.</p>