title: UDP C Server sendto() error: Can't assign requested address
body: <p>I'm writing a basic Client/Server program in C, using UDP. The idea of the program is that the client sends a message to the server, the server receives it, then echoes it back to the client (the goal being to measure RTT for UDP). Unfortunately, on the server side, when the program attempts to call sendto() to echo the message, I receive the error  "Can't assign requested address". </p>
  
  <pre><code>#include &lt;stdio.h&gt; 
  #include &lt;sys/types.h&gt; 
  #include &lt;sys/socket.h&gt; 
  #include &lt;netinet/in.h&gt; 
  #include &lt;netdb.h&gt; 
  #include &lt;stdlib.h&gt;
  #include &lt;strings.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;fcntl.h&gt;
  
  #define SERVER_PORT 7000 
  #define MAX_PENDING 10 
  #define MAX_LINE 1024000
  
  int main() 
  { 
      struct sockaddr_in sin, sout;
      socklen_t soutLen; 
      char buf[MAX_LINE]; 
      int len;
      int msgLen;
      int s; 
      char *msg;
      if( (s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) &lt; 0){
          perror("could not establish UDP socket");
          exit(1);
      }
  
      /* build address data structure */ 
      bzero((char *)&amp; sin, sizeof( sin)); 
      sin.sin_family = AF_INET; 
      sin.sin_addr.s_addr = INADDR_ANY;
      sin.sin_port = htons(SERVER_PORT);
  
      if( (bind(s, (struct sockaddr *)&amp;sin, sizeof(sin))) &lt; 0){
          perror("udpServer: bind"); 
          exit( 1); 
      }
  
      while(1){
          if((msgLen = recvfrom(s, buf, sizeof(buf), 0,(struct sockaddr *)&amp;sout, &amp;soutLen))&lt;0){
              perror("udpServer: recvfrom()"); 
              exit( 1); 
          }
  
          if( (sendto(s, buf, msgLen, 0, (struct sockaddr *)&amp;sout, sizeof(sout)))&lt;0 ){
              perror("udpServer: sendto()"); 
              exit( 1); 
          }
          free(msg);
      }
  
  } 
  </code></pre>
  
  <p>Thanks in advance: I'm pretty new to C, so any advice is much appreciated!</p>