title: Question about INADDR_ANY
body: <blockquote>
    <p>The constant INADDR_ANY is the so-called IPv4 wildcard address. The
    wildcard IP address is useful for applications that bind Internet
    domain sockets on multihomed hosts. If an application on a multihomed
    host binds a socket to just one of its host’s IP addresses, then that
    socket can receive only UDP datagrams or TCP connection requests sent
    to that IP address. However, we normally want an application on a
    multihomed host to be able to receive datagrams or connection requests
    that specify any of the host’s IP addresses, and binding the socket to
    the wildcard IP address makes this possible.</p>
  </blockquote>
  
  <pre><code>struct sockaddr_in server_address;
  int server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
  memset(&amp;server_address, 0, sizeof(struct sockaddr_in));
  server_address.sin_family = AF_INET;
  server_address.sin_addr.s_addr = htonl(INADDR_ANY); // here is my quesion
  server_address.sin_port = htons(9734);
  
  bind(server_sockfd, (struct sockaddr*)&amp;server_address, sizeof(server_address));
  </code></pre>
  
  <p>Question></p>
  
  <p>If we bind the socket to a specific IP address, then the socket can only receive UPD/TCP requests sent sent to that IP address.</p>
  
  <p>As I show in the above code, now the socket server_sockfd is bound with INADDR_ANY.
  I just feel confused here b/c if the socket can receive any request on the internet, how it can still work well. There are tons of requests of UDP/TCP on internet, if the socket responses to everybody, 
  , how can it still work?</p>
  
  <p>// updated code for client side //</p>
  
  <pre><code>int
  main(int argc, char *argv[])
  {
      struct sockaddr_in6 svaddr;
      int sfd, j;
      size_t msgLen;
      ssize_t numBytes;
      char resp[BUF_SIZE];
  
      if (argc &lt; 3 || strcmp(argv[1], "--help") == 0)
          usageErr("%s host-address msg...\n", argv[0]);
  
      /* Create a datagram socket; send to an address in the IPv6 somain */
  
      sfd = socket(AF_INET6, SOCK_DGRAM, 0);      /* Create client socket */
      if (sfd == -1)
          errExit("socket");
  
      memset(&amp;svaddr, 0, sizeof(struct sockaddr_in6));
      svaddr.sin6_family = AF_INET6;
      svaddr.sin6_port = htons(PORT_NUM);
      if (inet_pton(AF_INET6, argv[1], &amp;svaddr.sin6_addr) &lt;= 0)
          fatal("inet_pton failed for address '%s'", argv[1]);
  
      /* Send messages to server; echo responses on stdout */
  
      for (j = 2; j &lt; argc; j++) {
          msgLen = strlen(argv[j]);
          if (sendto(sfd, argv[j], msgLen, 0, (struct sockaddr *) &amp;svaddr,
                      sizeof(struct sockaddr_in6)) != msgLen)
              fatal("sendto");
  
          numBytes = recvfrom(sfd, resp, BUF_SIZE, 0, NULL, NULL);
          if (numBytes == -1)
              errExit("recvfrom");
  
          printf("Response %d: %.*s\n", j - 1, (int) numBytes, resp);
      }
  
      exit(EXIT_SUCCESS);
  }
  </code></pre>
  
  <p>// updated for server side code</p>
  
  <pre><code>int
  main(int argc, char *argv[])
  {
      struct sockaddr_in6 svaddr, claddr;
      int sfd, j;
      ssize_t numBytes;
      socklen_t len;
      char buf[BUF_SIZE];
      char claddrStr[INET6_ADDRSTRLEN];
  
      /* Create a datagram socket bound to an address in the IPv6 somain */
  
      sfd = socket(AF_INET6, SOCK_DGRAM, 0);
      if (sfd == -1)
          errExit("socket");
  
      memset(&amp;svaddr, 0, sizeof(struct sockaddr_in6));
      svaddr.sin6_family = AF_INET6;
      svaddr.sin6_addr = in6addr_any;                     /* Wildcard address */
      svaddr.sin6_port = htons(PORT_NUM);
  
      if (bind(sfd, (struct sockaddr *) &amp;svaddr,
                  sizeof(struct sockaddr_in6)) == -1)
          errExit("bind");
  
      /* Receive messages, convert to uppercase, and return to client */
  
      for (;;) {
          len = sizeof(struct sockaddr_in6);
          numBytes = recvfrom(sfd, buf, BUF_SIZE, 0,
                              (struct sockaddr *) &amp;claddr, &amp;len);
          if (numBytes == -1)
              errExit("recvfrom");
  
          /* Display address of client that sent the message */
  
          if (inet_ntop(AF_INET6, &amp;claddr.sin6_addr, claddrStr,
                      INET6_ADDRSTRLEN) == NULL)
              printf("Couldn't convert client address to string\n");
          else
              printf("Server received %ld bytes from (%s, %u)\n",
                      (long) numBytes, claddrStr, ntohs(claddr.sin6_port));
  
          for (j = 0; j &lt; numBytes; j++)
              buf[j] = toupper((unsigned char) buf[j]);
  
          if (sendto(sfd, buf, numBytes, 0, (struct sockaddr *) &amp;claddr, len) !=
                  numBytes)
              fatal("sendto");
      }
  }
  </code></pre>
  
  <p>// updated for how to run this server/client programs.</p>
  
  <pre><code>$ ./server_program &amp;
  [1] 31047
  $ ./client_program ::1 ciao // Send to server on local host
  Server received 4 bytes from (::1, 32770)
  Response 1: CIAO
  </code></pre>