title: Sendto: invalid argument error
body: <p>I'm creating a concurrent UDP socket for a university project. The client waits for a <code>stdin</code> command, then creates a new process and a new socket with the function child_job. The server receives the command from the client, creates a new process that initializes a new socket and then tries to send a string back to client. The problem is that the first sendto of the server create the error:</p>
  
  <blockquote>
    <p>Invalid argument </p>
  </blockquote>
  
  <p>I am not figuring out why. Can anyone help me please?</p>
  
  <p>Code of the client: </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;sys/wait.h&gt;
  #include &lt;netdb.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;arpa/inet.h&gt;
  
  
  void child_job(int sockfd, struct sockaddr_in servaddr, char* buffer){
  
      int n = sendto(sockfd, buffer, sizeof(buffer), 0, (struct sockaddr*)&amp;servaddr, sizeof(servaddr));
          if(n &lt; 0){
  
              perror("Error while sending roba to server\n");
              exit(1);
  
          }
  
      printf("Connected to server\n");
  
  
      char bufferaccio[128];
  
      int length = sizeof(struct sockaddr);
  
      int m = recvfrom(sockfd, bufferaccio, sizeof(bufferaccio), 0, (struct sockaddr*)&amp;servaddr, (socklen_t *)&amp;length);
  
      if(m &lt; 0){
  
          perror("Error while receiving from server\n");
          exit(1);
  
      }
  
  
  
      char stringa[128] = "I am a string from client";
  
      m = sendto(sockfd, stringa, strlen(stringa), 0, (struct sockaddr *)&amp;servaddr, sizeof(servaddr));
  
      if(m &lt; 0){
  
          perror("Error while sending stringa to server\n");
          exit(1);
  
      }
  
      exit(1);
  
  }
  
  
  int main(int argc, char** argv){
  
      int sockfd;
      struct sockaddr_in str;
  
      if(argc &lt; 3){
  
          fprintf(stderr, "Usage: %s portno ip\n", argv[0]);
          exit(1);
  
      }
  
      sockfd = socket(AF_INET, SOCK_DGRAM, 0);
      if(sockfd &lt; 0){
  
          perror("Error while creating socket\n");
          exit(1);
  
      }
  
  
      memset((void*)&amp;str, 0, sizeof(str));
      str.sin_family = AF_INET;
      str.sin_port = htons(atoi(argv[1]));
  
      if(inet_pton(AF_INET, argv[2], &amp;str.sin_addr)&lt; 0){
  
          perror("Error while inet_pton\n");
          exit(1);
  
      }
  
      pid_t pid;
  
      char buffer[128];
  
      printf("Write something to send\n");
      scanf("%s", buffer);
  
      pid = fork();
  
      if(pid == 0){
  
          printf("I am a children with pid %d\n", getpid());
  
          sockfd = socket(AF_INET, SOCK_DGRAM, 0);
          if(sockfd &lt; 0){
  
              perror("Error while creating socket in the child process\n");
              exit(1);
  
          }
  
          child_job(sockfd, str, buffer);
  
  
  
      }else if(pid &lt; 0){
  
          perror("Error while creating child process\n");
          exit(1);
  
      }
  
      wait(NULL);
  
      exit(1);
  
  
  }
  </code></pre>
  
  <p>Code of the server: </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;netdb.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;arpa/inet.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;sys/wait.h&gt;
  #include &lt;sys/types.h&gt;
  
  void initialize_newSocket(int* sock_fd, struct sockaddr_in *servaddr){
  
      int sockfd;
  
      sockfd = socket(AF_INET, SOCK_DGRAM, 0);
      if(sockfd &lt; 0){
  
          perror("Error while creating new socket\n");
          exit(1);
  
      }
  
      int ret = bind(sockfd, (struct sockaddr *)servaddr, sizeof(*servaddr));
      if(ret &lt; 0){
  
          perror("Error while binding in son\n");
          exit(1);
  
      }
  
      *sock_fd = sockfd;
  
  
  }
  
  
  void child_server_job(){
  
      struct sockaddr_in addr;
  
      int sockfd;
  
      memset((void *)&amp;addr, 0, sizeof(addr));
  
      addr.sin_family = AF_INET;
      addr.sin_addr.s_addr = INADDR_ANY;
      addr.sin_port = htons(0);
  
      initialize_newSocket(&amp;sockfd, &amp;addr);
  
      char string[128] = "I am a string";
  
      socklen_t fromlen = sizeof(addr);
  
      int n = sendto(sockfd, string, strlen(string), 0, (struct sockaddr *)&amp;addr, fromlen);
  
      if(n &lt; 0){
  
          perror("Error while sending something\n");
          exit(1);
  
      }
  
      int lenght = sizeof(struct sockaddr);
  
  
      n = recvfrom(sockfd, string, strlen(string), 0, (struct sockaddr *)&amp;addr, (socklen_t *)&amp;lenght);
  
      if(n &lt; 0){
  
          perror("Error while receveing data from client\n");
          exit(1);
  
      }
  
      printf("I received this string %s\n", string);
  
      exit(1);
  
  }
  
  
  int main(int argc, char** argv){
  
      if(argc &lt; 2){
  
          fprintf(stderr, "Usage: %s portno\n", argv[0]);
          exit(1);
  
      }
  
      int sockfd;
      struct sockaddr_in addr;
  
      sockfd = socket(AF_INET, SOCK_DGRAM, 0);
      if(sockfd &lt; 0){
  
          perror("Error while creating socket\n");
          exit(1);
  
      }
  
      addr.sin_family = AF_INET;
      addr.sin_port = htons(atoi(argv[1]));
      addr.sin_addr.s_addr = INADDR_ANY;
  
      int retBind = bind(sockfd, (struct sockaddr*)&amp;addr, sizeof(addr));
      if(retBind &lt; 0){
  
          perror("Error while binding socket\n");
          exit(1);
  
      }
  
      char bufferone[129];
  
      int length = sizeof(struct sockaddr);
  
      int n = recvfrom(sockfd, bufferone, sizeof(bufferone), 0, (struct sockaddr*)&amp;addr, (socklen_t *)&amp;length);
  
      if(n &lt; 0){
  
          perror("Error while receiving roba from client\n");
          exit(1);
  
      }
  
      printf("Printing: %s\n", bufferone);
  
      pid_t pid = fork();
  
      if(pid == 0){
  
          printf("I am the child with pid %d\n", getpid());
  
          child_server_job(sockfd, addr);
  
  
          exit(1);
  
  
      }else if(pid &lt; 0){
  
          perror("Error while forking new process\n");
          exit(1);
  
      }
  
      wait(NULL);
  
      exit(1);
  
  
  
  }
  </code></pre>
  
  <p>Client output:</p>
  
  <pre><code>Write something to send
  string
  I am a children with pid 5756
  Connected to server
  </code></pre>
  
  <p>Server output:</p>
  
  <pre><code>Printing: string
  I am the child with pid 5757
  Now it's my turn to send something
  Error while sending something
  : Invalid argument
  </code></pre>