title: C socket programming: Invalid argument error on connect()
body: <p>I'm writing a client side as part of a TCP client server program.</p>
  
  <p>My code reaches the connect part and throws an Invalid argument error, I have gone through the code several times and I couldn't find the problem.</p>
  
  <p>The code receives 3 arguments, first one is an IP address or a hostname, second one is port and the third is the maximum length of the message to be sent.</p>
  
  <p>My code uses getaddrinfo in order to convert the ip address or hostname, creates the needed variables, starts a connection, read from file, send data and receive data.</p>
  
  <p>I run the code with:</p>
  
  <pre><code>gcc -std=gnu99 -O3 -Wall -o pcc_client pcc_client.c
  ./pcc_client 127.0.0.1 2233 4
  </code></pre>
  
  <p>The output is:</p>
  
  <blockquote>
    <p>sockaddr_in initialized<br>
    Error starting connection : Invalid argument</p>
  </blockquote>
  
  <p></p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;assert.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;string.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/stat.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;arpa/inet.h&gt;
  #include &lt;netdb.h&gt;
  #include &lt;dirent.h&gt;
  
  #define FILE_ADDR   "/dev/urandom"
  
  int main(int argc, char *argv[]) {
      if (argc != 4) {
          printf("should receive 3 arguments Received %d args\n", argc);
          exit(1);
      }
  
      //Get command line arguments
      unsigned int port = atoi(argv[2]);
      int length = atoi(argv[3]); //Number of bytes to read
      char* buffer = malloc(length * sizeof(char)); //Buffer to hold data read from file
      char* recvBuf = malloc(10 * sizeof(char)); // Buffer to hold response from server
  
      struct addrinfo hints, *servinfo, *p;
      struct sockaddr_in *serv_addr;
      int rv;
      char ip[100];
  
      memset(&amp;hints, 0, sizeof hints);
      hints.ai_family = AF_INET;
      hints.ai_socktype = SOCK_STREAM;
  
      if ((rv = getaddrinfo(argv[1], argv[2], &amp;hints, &amp;servinfo)) != 0) {
          perror("getaddrinfo error\n");
          return 1;
      }
      for (p = servinfo; p != NULL; p = p-&gt;ai_next) {
          serv_addr = (struct sockaddr_in *) p-&gt;ai_addr;
          strcpy(ip, inet_ntoa(serv_addr-&gt;sin_addr));
      }
      //   inet_aton(ip, &amp;h.sin_addr);
      freeaddrinfo(servinfo);
  
      //Initialize socket
      int sockfd;
      sockfd = socket(AF_INET, SOCK_STREAM, 0);
      if (sockfd &lt; 0) //Error creating socket
              {
          perror("Error creating socket \n");
          exit(1);
      }
  
      printf("socket created\n");
  
      //Initialize sockaddr_in structure
      memset((void*)serv_addr, 0,(size_t) sizeof(*serv_addr));
      serv_addr-&gt;sin_family = AF_INET;
      serv_addr-&gt;sin_port = htons(port);
      serv_addr-&gt;sin_addr.s_addr = inet_addr("127.0.0.1"); //change?
  
  
      //Initialize connection
      if (connect(sockfd, (struct sockaddr *) &amp;serv_addr, sizeof(serv_addr)) &lt; 0) { //Error connecting
          perror("Error starting connection \n");
          exit(1);
      }
      printf("connect succesful\n");
  
      exit(0);
  
  }
  </code></pre>