title: Need help getting TCP port number and IP address in C
body: <p>I'm trying to find a way to get my server to print its TCP Port Number and IP address but how I have it right now is producing the wrong IP, I am getting an output of 0.0.33.32. Any help is appreciated!</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;netdb.h&gt;
  #include &lt;arpa/inet.h&gt;
  #include &lt;sys/wait.h&gt;
  #include &lt;signal.h&gt;
  #include &lt;time.h&gt;
  
  #define PORT "21467"  // the port users will be connecting to
  
  #define BACKLOG 10   // how many pending connections queue will hold
  
  void sigchld_handler(int s)
  {
  while(waitpid(-1, NULL, WNOHANG) &gt; 0);
  }
  
  // get sockaddr, IPv4 or IPv6:
  void *get_in_addr(struct sockaddr *sa)
  {
  if (sa-&gt;sa_family == AF_INET) {
      return &amp;(((struct sockaddr_in*)sa)-&gt;sin_addr);
  }
  
  return &amp;(((struct sockaddr_in6*)sa)-&gt;sin6_addr);
  }
  
  int main(void)
  {
  int sockfd, new_fd;  // listen on sock_fd, new connection on new_fd
  struct addrinfo hints, *servinfo, *p;
  struct sockaddr_storage their_addr; // connector's address information
  socklen_t sin_size;
  struct sigaction sa;
  int yes=1;
  char s[INET6_ADDRSTRLEN];
  int rv;
  
  memset(&amp;hints, 0, sizeof hints);
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = AI_PASSIVE; // use my IP
  
  char hname[100];
  gethostname(hname, sizeof hname);
  
  if ((rv = getaddrinfo(hname, PORT, &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 ((sockfd = socket(p-&gt;ai_family, p-&gt;ai_socktype,
              p-&gt;ai_protocol)) == -1) {
          perror("server: socket");
          continue;
      }
  
      if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &amp;yes,
              sizeof(int)) == -1) {
          perror("setsockopt");
          exit(1);
      }
  
      if (bind(sockfd, p-&gt;ai_addr, p-&gt;ai_addrlen) == -1) {
          close(sockfd);
          perror("server: bind");
          continue;
      }
  
      break;
  }
  
  if (p == NULL)  {
      fprintf(stderr, "server: failed to bind\n");
      return 2;
  }
  
  
  
  
  freeaddrinfo(servinfo); // all done with this structure
  
  if (listen(sockfd, BACKLOG) == -1) {
      perror("listen");
      exit(1);
  }
  
  sa.sa_handler = sigchld_handler; // reap all dead processes
  sigemptyset(&amp;sa.sa_mask);
  sa.sa_flags = SA_RESTART;
  if (sigaction(SIGCHLD, &amp;sa, NULL) == -1) {
      perror("sigaction");
      exit(1);
  }
  
  
  char host[100];
  char service[20];
  
  getnameinfo(p-&gt;ai_addr, p-&gt;ai_addrlen, host, sizeof host, service, sizeof service, 0);
  inet_ntop(p-&gt;ai_family, get_in_addr((struct sockaddr *)p-&gt;ai_addr),
          host, sizeof host);
  
  printf("server: has TCP port number %s and IP address %s\n", service, host); 
  
  printf("server: waiting for connections...\n");
  
  int numbytes;
  char buf[100];
  
  while(1) {  // main accept() loop
      sin_size = sizeof their_addr;
      new_fd = accept(sockfd, (struct sockaddr *)&amp;their_addr, &amp;sin_size);
      if (new_fd == -1) {
          perror("accept");
          continue;
      }
  
      inet_ntop(their_addr.ss_family,
          get_in_addr((struct sockaddr *)&amp;their_addr),
          s, sizeof s);
  
      //getnameinfo(get_in_addr((struct sockaddr *)&amp;their_addr), sizeof get_in_addr((struct sockaddr *)&amp;their_addr), NULL, NULL, service, sizeof service, 0);
  
      if ((numbytes = recv(new_fd, buf, 100, 0)) == -1)
          perror("recv");
  
      /* if ((numbytes = recv(sockfd, buf, 100, 0)) == -1) {
      perror("recv");
      exit(1);
  }*/ 
  
      struct tm* local;
      char toClient[50];
      printf("server: received '%s'\n",buf);
      time_t curTime = time(0);
      local = localtime(&amp;curTime);
      if(buf[0] == 't'    ){ 
          printf("server: received time request\n", s);
          sprintf(toClient, "Time: %d::%d::%d", local-&gt;tm_hour, local-&gt;tm_min, local-&gt;tm_sec);  //Seg faulting
          printf("Sent the time to the client having IP address %s and port number %s\n", s, service);
      }
      else if(buf[0] == 'd'){
          printf("server: received date request\n", s);
          sprintf(toClient, "Date: %d::%d::%d", 1900+local-&gt;tm_year, 1+local-&gt;tm_mon, local-&gt;tm_mday);
          printf("Sent the date to the client having IP address %s and port number %s\n", s, service);
      }
      else{
  
      }   
  
      if ((numbytes = send(new_fd, toClient, sizeof toClient, 0)) == -1) {
      perror("send");
      exit(1);
  }
      close(new_fd);
      break; //Temporary
  }
  
  
  close(sockfd);
  return 0;
  }
  </code></pre>