简体   繁体   中英

C get hostname by IP

I cant convert ip to hostname, I want to print Hostname by IP address but i dont know how

#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
int main(int argc, char **argv)
{
  if (argc < 2)
  {
    printf("Usage: %s hostname", argv[0]);
    exit(-1);
  }
  struct hostent *hp = gethostbyname(argv[1]);
  if (hp == NULL)
  {
    printf("gethostbyname() failed\n");
  }
  else
  {
    printf("%s = ", hp->h_name); 
    unsigned int i = 0;
    while (hp->h_addr_list[i] != NULL)
    {
      printf(" %s ", inet_ntoa(*(struct in_addr *)(hp->h_addr_list[i])));
      i++;
    }
    printf("\n");
  }
}

I want this, ex. input

172.217.194.94

output

172.217.194.94 = www.google.co.uk

I cant convert ip to hostname, I want to print Hostname by IP address but i dont know how

#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
int main(int argc, char **argv)
{
  if (argc < 2)
  {
    printf("Usage: %s hostname", argv[0]);
    exit(-1);
  }
  struct hostent *hp = gethostbyname(argv[1]);
  if (hp == NULL)
  {
    printf("gethostbyname() failed\n");
  }
  else
  {
    printf("%s = ", hp->h_name); 
    unsigned int i = 0;
    while (hp->h_addr_list[i] != NULL)
    {
      printf(" %s ", inet_ntoa(*(struct in_addr *)(hp->h_addr_list[i])));
      i++;
    }
    printf("\n");
  }
}

I want this, ex. input

172.217.194.94

output

172.217.194.94 = www.google.co.uk

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM