title: UDP sendto() error: invalid argument
body: <p>We are trying to apply a UDP based protocol and having some problem with sendto() function.</p>
  
  <p>when we try to response to the write-request with ack we get "invalid argument" from the sendto() function</p>
  
  <p>this is our code:</p>
  
  <pre><code>int                sock;                  // Socket 
  sockaddr_in_t      echoServAddr;          // Local address 
  sockaddr_in_t      echoClntAddr;          // Client address 
  unsigned int       cliAddrLen;            // Length of incoming message
  data_packet_t      echoBuffer;
  wrq_packet_t       wrqBuffer;
  unsigned short     echoServPort;          // Server port 
  int                recvMsgSize;           // Size of received message 
  ack_packet_t      Ack;
  struct timeval     timeout;
  fd_set             fds;
  
  
  
  /* Create socket for sending/receiving datagrams */
  if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) &lt; 0) perror("TTFTPERROR: socket() failed"); 
  
  /* Construct local address structure */
  memset(&amp;echoServAddr, 0, sizeof(echoServAddr));
  echoServAddr.sin_family = AF_INET;
  echoServAddr.sin_addr.s_addr = htonl(INADDR_ANY);
  echoServAddr.sin_port = htons(echoServPort);
  
  /*Bind to the local address */
  if (bind(sock, (struct sockaddr *) &amp;echoServAddr, sizeof(echoServAddr)) &lt; 0) perror("TTFTPERROR: bind() failed");
  
  FD_ZERO(&amp;fds);
  FD_SET(sock, &amp;fds);
  timeout.tv_sec = WAIT_FOR_PACKET_TIMEOUT;
  timeout.tv_usec = 0;
  
  while (1) {
      recvMsgSize = recvfrom(sock, &amp;wrqBuffer, FULL_PACKET_SIZE, 0, (struct sockaddr *) &amp;echoClntAddr, &amp;cliAddrLen);
      if (recvMsgSize &gt; 0) break; // we got something!
  }
  
  Ack = CreateAckPacket(0); // send ack 0
  if (sendto(sock, &amp;Ack, sizeof(Ack), 0, (struct sockaddr *) &amp;echoClntAddr, sizeof(echoClntAddr)) == -1){
      perror("TTFTPERROR: sendto() failed to send ack 0");
      exit(-1);
  }
  </code></pre>
  
  <p>Could you help us understand what is wrong?</p>