[英]C get hostname by IP
我無法將 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");
}
}
我想要這個,前。 輸入
172.217.194.94
output
172.217.194.94 = www.google.co.uk
我無法將 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");
}
}
我想要這個,前。 輸入
172.217.194.94
output
172.217.194.94 = www.google.co.uk
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.