title: Send UDP packet with fixed source port number using getaddrinfo and bind
body: <p>Using BJ's talker.c code as a template:
  <a href="http://beej.us/guide/bgnet/examples/talker.c" rel="nofollow noreferrer">http://beej.us/guide/bgnet/examples/talker.c</a></p>
  
  <pre><code>#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 SERVERPORT "4950"    // the port users will be connecting to
  
  int main(int argc, char *argv[])
  {
      int sockfd;
      struct addrinfo hints, *servinfo, *p;
      int rv;
      int numbytes;
      struct sockaddr_storage their_addr;
      socklen_t addr_len;
      addr_len = sizeof their_addr;
  
      if (argc != 3) {
          fprintf(stderr,"usage: talker hostname message\n");
          exit(1);
      }
  
      memset(&amp;hints, 0, sizeof hints);
      hints.ai_family = AF_UNSPEC;
      hints.ai_socktype = SOCK_DGRAM;
  
      if ((rv = getaddrinfo(argv[1], SERVERPORT, &amp;hints, &amp;servinfo)) != 0) {
          fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
          return 1;
      }
  
      // loop through all the results and make a socket
      for(p = servinfo; p != NULL; p = p-&gt;ai_next) {
          if ((sockfd = socket(p-&gt;ai_family, p-&gt;ai_socktype,
                  p-&gt;ai_protocol)) == -1) {
              perror("talker: socket");
              continue;
          }
  
          break;
      }
  
      if (p == NULL) {
          fprintf(stderr, "talker: failed to create socket\n");
          return 2;
      }
  
      if ((numbytes = sendto(sockfd, argv[2], strlen(argv[2]), 0,
               p-&gt;ai_addr, p-&gt;ai_addrlen)) == -1) {
          perror("talker: sendto");
          exit(1);
      }
  
      freeaddrinfo(servinfo);
  
      printf("talker: sent %d bytes to %s\n", numbytes, argv[1]);
  
  
  //============== Added Code for recvfrom() (pseudocode-ish) =============
  
  
      if ((numbytes = recvfrom(sockfd, buf, MAXBUFLEN , 0, (struct sockaddr *)&amp;their_addr, &amp;addr_len)) == -1) 
      {
          close(sockfd);
          perror("talker: recvfrom");    
          exit(1);
      }
  
      close(sockfd);
  
      printf("Got packet\n");
  
  //============== End Added Code for recvfrom() =============
  
  
      close(sockfd);
  
      return 0;
  }
  </code></pre>
  
  <p>I have a requirement whereby the client UDP process that talks to the server must use a fixed, known <code>source</code> port number. In this case, assume it's <code>SERVERPORT</code> (4950). The server then responds <strong>to that port number</strong>. Yes, this is unusual as most servers respond to the ephemeral port number that the system assigns to the sender.</p>
  
  <p>After sending a packet using <code>sendto()</code>, I listen for a response using <code>recvfrom()</code>. That's the (pseudo)code I added in the above example.</p>
  
  <p>All my searches online point to using <code>bind()</code> but that code is usually on the server side. I haven't found a way to bind on the client side using the modern <code>getaddrinfo()</code> method. I tried to add a <code>bind()</code> right after the <code>socket()</code> setup but that wouldn't work because <code>p</code> is a server-side structure (derived from the hints structure that uses the server IP address) and I get a bind Error: </p>
  
  <p><code>Error 99 (Cannot assign requested address)</code></p>
  
  <p>code added:</p>
  
  <p><code>bind(sockfd, p-&gt;ai_addr, p-&gt;ai_addrlen)</code></p>
  
  <p>I want to do this in a way that will work for both IPv4 and IPv6.</p>
  
  <p>I've seen other examples whereby a local/source sockaddr_in structure is filled out with the client's information and <strong>that</strong> is used in the bind, but those are IPv4 or IPv6 specific. </p>
  
  <p>Can someone please show me how to properly update the <code>talker.c</code> code to <code>sendto()</code> and <code>recvfrom()</code> a UDP server using a fixed <em>source</em> port number? Assume that the server is immutable.</p>