title: getsockname always returning 0.0.0.0?
body: <p>Here is the code. It is the same as the code from this similar question: <a href="http://monkey.org/freebsd/archive/freebsd-stable/200401/msg00032.html" rel="noreferrer">http://monkey.org/freebsd/archive/freebsd-stable/200401/msg00032.html</a>. When I run it I always get the output:</p>
  
  <p>listening on 0.0.0.0:54493 or something. Obviously the port changes, but I have no idea why I keep getting an IP address of 0.0.0.0. Am I missing something?</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;arpa/inet.h&gt;
  
  int main()
  {
      int sock;
      int len = sizeof(struct sockaddr);
      struct sockaddr_in addr, foo;
  
      if((sock=socket(AF_INET, SOCK_STREAM, 0))&lt;0)
      {
          exit(0);
      }
  
      memset(&amp;addr, 0, sizeof(struct sockaddr_in));
      addr.sin_family = AF_INET;
      addr.sin_addr.s_addr = INADDR_ANY;
      addr.sin_port = htons(0);
  
      if(bind(sock, (struct sockaddr *) &amp;addr, sizeof(struct sockaddr_in))&lt;0)
      {
          perror("bind");
          exit(0);
      }
  
      if(listen(sock, 5)&lt;0)
      {
           perror("listen");
           exit(0);
      }
  
      getsockname(sock, (struct sockaddr *) &amp;foo, &amp;len);
      fprintf(stderr, "listening on %s:%d\n", inet_ntoa(foo.sin_addr), 
              ntohs(foo.sin_port));
  
      return 0;
  }
  </code></pre>