Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C Unix-如何获取域名的IP地址?_C_Unix_Localhost_Hosts File - Fatal编程技术网

C Unix-如何获取域名的IP地址?

C Unix-如何获取域名的IP地址?,c,unix,localhost,hosts-file,C,Unix,Localhost,Hosts File,在UNIX的C程序中,可以使用gethostbyname()获取域的地址,如“localhost”。如何将gethostbyname()的结果转换为点十进制表示法 struct hostent* pHostInfo; long nHostAddress; /* get IP address from name */ pHostInfo=gethostbyname("localhost"); if(!pHostInfo){ printf("Could not resolve host

在UNIX的C程序中,可以使用gethostbyname()获取域的地址,如“localhost”。如何将gethostbyname()的结果转换为点十进制表示法

struct hostent* pHostInfo;
long nHostAddress;

/* get IP address from name */
pHostInfo=gethostbyname("localhost");

if(!pHostInfo){
    printf("Could not resolve host name\n");
    return 0;
}

/* copy address into long */
memset(&nHostAddress, 0, sizeof(nHostAddress));
memcpy(&nHostAddress,pHostInfo->h_addr,pHostInfo->h_length);
nHostAddress包含以下内容:

16777243
如何转换结果,以便获得以下输出:

127.0.0.1

您可以使用
inet\u ntoa()
直接将\u addr中的
结构转换为字符串:


不过,您得到的值(16777243)看起来是错误的——结果是1.0.0.27

您可以使用
inet\u ntoa()
直接从\u addr
中的
结构转换为字符串:

不过,您得到的值(16777243)看起来是错误的——结果是1.0.0.27

inet\u ntoa()
API满足您的需求,但显然已被弃用:

如果您想要更具未来性的IPV6ish,可以使用
inet\u ntop()

inet\u ntoa()API满足您的需求,但显然已被弃用:

如果您想要更具未来性的IPV6ish,可以使用
inet\u ntop()


编译这段代码很容易

#include<stdio.h>
#include<netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main()
{
    struct hostent *ghbn=gethostbyname("www.kamonesium.in");//change the domain name
    if (ghbn) {
        printf("Host Name->%s\n", ghbn->h_name);
        printf("IP ADDRESS->%s\n",inet_ntoa(*(struct in_addr *)ghbn->h_name) );
    }
}
#包括
#包括
#包括
#包括
#包括
int main()
{
struct hostent*ghbn=gethostbyname(“www.kamonesium.in”);//更改域名
如果(ghbn){
printf(“主机名->%s\n”,ghbn->h_名称);
printf(“IP地址->%s\n”,inet\u ntoa(*(地址中的结构)ghbn->h\u名称));
}
}

编译这段代码很容易

#include<stdio.h>
#include<netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main()
{
    struct hostent *ghbn=gethostbyname("www.kamonesium.in");//change the domain name
    if (ghbn) {
        printf("Host Name->%s\n", ghbn->h_name);
        printf("IP ADDRESS->%s\n",inet_ntoa(*(struct in_addr *)ghbn->h_name) );
    }
}
#包括
#包括
#包括
#包括
#包括
int main()
{
struct hostent*ghbn=gethostbyname(“www.kamonesium.in”);//更改域名
如果(ghbn){
printf(“主机名->%s\n”,ghbn->h_名称);
printf(“IP地址->%s\n”,inet\u ntoa(*(地址中的结构)ghbn->h\u名称));
}
}
最后一条语句中的变量“h\u name”需要更改为“h\u addr”,如下所示:

printf(“IP地址->%s\n”,inet\u ntoa(*(地址中的结构*)ghbn->h\u addr))

最后一条语句中的变量“h\u name”需要更改为“h\u addr”,如下所示:



printf(“IP地址->%s\n”,inet\u ntoa(*(地址中的结构*)ghbn->h\u addr))

你只需要正确地格式化它;每个字节对应一个IP字段(例如,第一个字节是
127
,等等),您只需正确格式化它;每个字节对应一个IP字段(例如,第一个字节是
127
,等等),可能与他的计算机的结尾有关。我可以理解1.0.0.127,但27是无法解释的。它实际上是1.0.0.127,@Dashwuff请再次检查。奇怪。。。我在Python shell中使用
socket.inet_ntoa(struct.pack(“>L”,16777243))
并获得
'1.0.0.27'
输出。可能与他的计算机的终端或其他东西有关。我可以理解1.0.0.127,但27是无法解释的。它实际上是1.0.0.127,@Dashwuff请再次检查它。奇怪。。。我正在Python shell中使用
socket.inet\u ntoa(struct.pack(“>L”,16777243))
并将
'1.0.0.27'
输出。这两个链接不再存在exist@MichaelDodd更新。但是,一般来说,您可以通过“建议编辑”自己执行此类更新。修复断开的链接通常是被批准的。我知道,我有足够的代表直接编辑问题和答案。然而,C并不是我最擅长的领域,所以我不想用可能错误或误导的东西来更新你的答案。这两个链接不再存在exist@MichaelDodd更新。但是,一般来说,您可以通过“建议编辑”自己执行此类更新。修复断开的链接通常是被批准的。我知道,我有足够的代表直接编辑问题和答案。但是,C不是我最擅长的领域,所以我不想用可能错误或误导的内容更新你的答案。这是对其他答案之一的评论吗?这是对其他答案之一的评论吗?AFAIK
gethostbyname
现在已经过时了。应该使用
getaddrinfo
。这将返回一个垃圾IP。“localhost”解析为
108.111.99.97
,这甚至不是我的真实IP…AFAIK
gethostbyname
现在已过时。应该使用
getaddrinfo
。这将返回一个垃圾IP。“localhost”解析为
108.111.99.97
,这甚至不是我真正的IP。。。