title: sendto() crashes with error code "Success"
body: <p>My problem is quite infuriating, actually. I'll show you the code first.</p>
  
  <pre><code>/*
  ** listener.c -- a datagram sockets "server" demo
  */
  //Original Code: Brian Hall (beej@beej.us)
  //Commented and modified by Vishal Kotcherlakota (PID A07124450)
  
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;string.h&gt;
  #include &lt;sys/types.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;
  
  #define MYPORT "4960"   // the port users will be connecting to
  #define YOURPORT "4961"
  #define MAXBUFLEN 10000 
  
  // get sockaddr, IPv4 or IPv6:
  void *get_in_addr(struct sockaddr *sa)
  {
      //If the socket address struct says it's an IPv4...
      if (sa-&gt;sa_family == AF_INET) {
          //...return the IPv4 variable.
          return &amp;(((struct sockaddr_in*)sa)-&gt;sin_addr);
      }
      //otherwise, assume it's IPv6, and get the IPv6 variable
      return &amp;(((struct sockaddr_in6*)sa)-&gt;sin6_addr);
  }
  
  int main(int argc, char *argv[])
  {
      int sockfd, sockfdAck; //socket file descriptor (handle)
      struct addrinfo hints, *servinfo, *p, *q;
      int rv;
      int numbytes;
      unsigned int seqNum, stateNum=0, ackNum;
      struct sockaddr_storage their_addr;
      struct timeval recvTime, timeStamp, latency;
      char buf[MAXBUFLEN], junk[MAXBUFLEN];
      size_t addr_len;
      char *ackBack;
      char s[INET6_ADDRSTRLEN];
  
      if (argc != 2)
      {
          fprintf(stderr, "usage: &lt;executable&gt; &lt;hostname&gt;\n");
          exit(0);
      }
  
      memset(&amp;hints, 0, sizeof hints);
      hints.ai_family = AF_UNSPEC; // set to AF_INET to force IPv4
      hints.ai_socktype = SOCK_DGRAM;
      hints.ai_flags = AI_PASSIVE; // use my IP
  
      if ((rv = getaddrinfo(NULL, MYPORT, &amp;hints, &amp;servinfo)) != 0) {
          fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
          return 1;
      }
  
      // loop through all the results and bind to the first we can
      for(q = servinfo; q != NULL; q = q-&gt;ai_next) {
          if ((sockfd = socket(q-&gt;ai_family, q-&gt;ai_socktype,
                  q-&gt;ai_protocol)) == -1) {
              perror("listener: socket");
              continue;
          }
  
          if (bind(sockfd, q-&gt;ai_addr, q-&gt;ai_addrlen) == -1) {
              close(sockfd);
              perror("listener: bind");
              continue;
          }
  
          break;
      }
  
      if (q == NULL) {
          fprintf(stderr, "listener: failed to bind socket\n");
          return 2;
      }
  
      freeaddrinfo(servinfo);
  
      memset(&amp;hints, 0, sizeof hints);
      hints.ai_family = AF_UNSPEC; // set to AF_INET to force IPv4
      hints.ai_socktype = SOCK_DGRAM;
  
      if ((rv = getaddrinfo(argv[1], BACKPORT, &amp;hints, &amp;servinfo)) != 0) {
          fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
          return 1;
      }
  
      // loop through all the results and bind to the first we can
      for(p = servinfo; p != NULL; p = p-&gt;ai_next) {
          if ((sockfdAck = socket(p-&gt;ai_family, p-&gt;ai_socktype,
                  p-&gt;ai_protocol)) == -1) {
              perror("listener: socket");
              continue;
          }
  
          break;
      }
  
      if (p == NULL) {
          fprintf(stderr, "listener: failed to bind socket\n");
          return 2;
      }
  
      freeaddrinfo(servinfo);
          printf("listener: waiting to recvfrom...\n");
      while (1)
      {
  
          addr_len = sizeof their_addr;
          if ((numbytes = recvfrom(sockfd, buf, MAXBUFLEN-1 , 0,
              (struct sockaddr *)&amp;their_addr, &amp;addr_len)) == -1) {
              perror("recvfrom");
              exit(1);
          }
  
          /*printf("listener: got packet from %s\n",
              inet_ntop(their_addr.ss_family,
                  get_in_addr((struct sockaddr *)&amp;their_addr),
                  s, sizeof s));
          printf("listener: packet is %d bytes long\n", numbytes);
          */
          buf[numbytes] = '\0';
          sscanf(buf,"%u %s",&amp;seqNum, junk);
          if (seqNum == stateNum + 1)
          {
              stateNum = seqNum;
              printf("Ok, state is now %u.\n", stateNum);
          }
          ackBack = malloc(20*sizeof(char));
          sprintf(ackBack, "%u acknowledged\0", stateNum);
          numbytes = sendto(sockfdAck, ackBack, strlen(ackBack), 0, p-&gt;ai_addr, p-&gt;ai_addrlen);
          if (numbytes == -1);
          {
              perror("sendto");
              exit(1);
          }
          free(ackBack);
      }
  
      return 0;
  } 
  </code></pre>
  
  <p>Please forgive the sloppy code; I'm desperately trying to finish this assignment on time. The goal is to develop an ARQ protocol using datagram sockets. This code should work, but when I run it, I get an error <code>sendto: Success</code>, meaning that my <code>sendto()</code> call failed. I can't find documentation for this anywhere, and I'm getting to be extremely desperate.</p>