title: Why is this call to sendto() failing?
body: <p>I'm trying to send a single UDP packet with a specific payload and my call to <code>sendto()</code> is failing at runtime. This is my first experiment with socket programming.</p>
  
  <p>The original code is part of a larger project, but I've reduced it to a much simpler test program:</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;netdb.h&gt;
  #include &lt;errno.h&gt;
  
  #define SIZE 16
  
  int main()
  {
       int sock;
       struct sockaddr *addr;
       socklen_t addrlen;
       struct addrinfo hints, *result;
       char *url = "tracker.openbittorrent.com";
       void *payload = malloc(SIZE);
  
       memset(&amp;hints, 0, sizeof(struct addrinfo));
       hints.ai_family = AF_INET;
       hints.ai_socktype = SOCK_DGRAM;
       hints.ai_flags = 0;
       hints.ai_protocol = IPPROTO_UDP;
       hints.ai_canonname = NULL;
       hints.ai_addr = NULL;
       hints.ai_next = NULL;
  
       int er = getaddrinfo(url, NULL, &amp;hints, &amp;result);
       if (er != 0) {
            fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(er));
            exit(EXIT_FAILURE);
       }
  
       sock = socket(result-&gt;ai_family, result-&gt;ai_socktype, result-&gt;ai_protocol);
       if (sock == -1) {
            perror("socket");
            exit(EXIT_FAILURE);
       }
  
       if (sendto(sock, payload, SIZE, 0, result-&gt;ai_addr, result-&gt;ai_addrlen) == -1)
            perror("sendto");
   }
  </code></pre>
  
  <p>Here's the output: <code>sendto: Invalid argument</code></p>
  
  <p>Some gdb output, as requested:</p>
  
  <pre><code>(gdb) p result-&gt;ai_addr-&gt;sa_data 
   $3 = "\000\000_\327&gt;\005\000\000\000\000\000\000\000"
  (gdb) p result-&gt;ai_addr-&gt;sa_family
   $4 = 2
  (gdb) p result-&gt;ai_addrlen
   $5 = 16
  (gdb) p result-&gt;ai_canonname
   $6 = 0x0
  (gdb) p result-&gt;ai_flags
   $8 = 0
  (gdb) p result-&gt;ai_protocol
   $9 = 17
  (gdb) p result-&gt;ai_socktype
   $10 = 2
  </code></pre>