title: Socket programming: sendto always fails with errno 22 (EINVAL)
body: <p>I am always getting no bytes sent, with an errno of 22 (EINVAL, Invalid Argument) with this code.  The <code>destination_host</code> is set elsewhere and known to be valid, so I really don't see what is going on.  <code>MAXMSGSIZE</code> is 1000.  No errors, or warnings.  I am compiling with <code>-Wall -Werror -pedantic</code></p>
  
  <pre><code>char *data_rec;
  u_int data_len;
  
  int sockfd;
  uint16_t *ns;
  struct sockaddr_in address;
  struct sockaddr *addr;
  
  char *ip;
  
  int i;
  int errno;
  int bytes_sent;
  
  data_len = MAXMSGSIZE;
  data_rec = malloc(sizeof(char)*MAXMSGSIZE);
  ns = malloc(MAXMSGSIZE*sizeof(uint16_t));
  ip = malloc(MAXMSGSIZE*sizeof(char));
  
  data_rec = "some random test stuff";
  
  sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
  if(sockfd&lt;0) {
      printf("socket() failed\n");
  }
  
  inet_ntop(AF_INET,destination_host-&gt;h_addr,ip,MAXMSGSIZE);
  memset(&amp;address, 0, sizeof(address));
  address.sin_family = AF_INET;
  address.sin_port = htons(theirPort);
  address.sin_addr.s_addr = (unsigned long)ip;
  
  addr = (struct sockaddr*)&amp;address;
  bind(sockfd,addr,sizeof(address));
  /*Convert the message to uint16_t*/
  for(i=0; i&lt;MAXMSGSIZE; i++) {
      ns[i] = htons(data_rec[i]);
  }
  
  
  /* send the message */
  bytes_sent = sendto(sockfd, ns, data_len, MSG_DONTWAIT, addr, sizeof(addr));
  if(bytes_sent == -1) {
      printf("Error sending: %i\n",errno);
  }
  </code></pre>