[英]C get hostname by IP
I cant convert ip to hostname, I want to print Hostname by IP address but i dont know how我无法将 ip 转换为主机名,我想通过 IP 地址打印主机名,但我不知道如何
#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
172.217.194.94
output output
172.217.194.94 = www.google.co.uk
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我无法将 ip 转换为主机名,我想通过 IP 地址打印主机名,但我不知道如何
#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
172.217.194.94
output output
172.217.194.94 = www.google.co.uk
172.217.194.94 = www.google.co.uk
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.