linux c根据ip获取主机名,Linux 网络编程之通过IP或者主机名获取信息

struct hostent

{

char *h_name; /* 主机的正式名称*/

char **h_aliases; /* 主机的别名列表*/

int h_addrtype; /* 主机的地址类型AF_INET */

int h_length; /* 主机的地址长度*/

char **h_addr_list; /* 主机的IP地址列表*/

}

#define h_addr h_addr_list[0] /* 主机的第一个IP地址*/

struct hostent *gethostbyname(const char *name);

通过主机名 name 获取 主机IP等信息。成功返回给定主机名包含的主机名字和地址信息的 hostent结构体指针。

示例如下:

#include #include #include int main(int argc, char *argv[])

{

if(argc != 2)

{

printf("Usage: %s domain_name \n",argv[0]);

exit(EXIT_FAILURE);

}

struct hostent *ht = gethostbyname(argv[1]);

if( ht )

{

int i = 0;

printf("Source domain: %s \n", argv[1]);

printf("Formal domain: %s \n", ht->h_name);

printf("Address type: %s \n", \

ht->h_addrtype == AF_INET ? "AF_INET" : "AF_INET6" );

printf("Address length:%d \n", ht->h_length);

for( ; ; ++i)

{

if (ht->h_aliases[i])

printf("Aliases name %d : %s \n",i+1 ,ht->h_aliases[i]);

else

break;

}

for(i=0; ; ++i)

{

if (ht->h_addr_list[i])

printf("IP Address %d : %s \n",i+1, \

inet_ntoa((unsigned int *)ht->h_addr_list[i]));

else

break;

}

}

return 0;

}

struct hostent *gethostbyaddr(const void *addr, socklen_t len, int type);

返回对应于给定地址的包含主机名字和地址信息的hostent结构指针。

addr 指向网络字节序地址的指针。

len 地址的长度。

type 地址类型。

示例如下:

#include #include #include #define ADDR_LEN 4

#define ADDR_TYPEAF_INET

int main(int argc, char *argv[])

{

if (argc != 2)

{

printf("Usage: %s IP Address\n",argv[0]);

exit(EXIT_FAILURE);

}

struct in_addr *addr = (struct in_addr *)malloc(sizeof(struct in_addr));

if ( ! inet_aton(argv[1], addr))

{

printf(" %s Invalid address",argv[1]);

exit(EXIT_FAILURE);

}

struct hostent *ht = gethostbyaddr(addr, ADDR_LEN, ADDR_TYPE);

if(ht)

{

int i = 0;

printf("Source IP: %s\n",argv[1]);

printf("Formal domain: %s \n",ht->h_name);

printf("Address type: %s\n",ht->h_addrtype == AF_INET ? "AF_INET" : "AF_INET6");

printf("Address length: %d \n", ht->h_length);

for( ; ; ++i)

{

if(ht->h_aliases[i])

printf("Aliases name %d : %s\n",i+1,ht->h_aliases[i]);

else

break;

}

for(i = 0; ; ++i)

{

if(ht->h_addr_list[i])

printf("IP Address %d : %s\n",i+1, \

inet_ntoa((unsigned int *)ht->h_addr_list[i]));

else

break;

}

}

else

{

switch(h_errno)

{

case HOST_NOT_FOUND :

printf("The specified host is unknown\n");

break;

case NO_ADDRESS:

printf("The requested name is valid but does not have an IP address.\n");

break;

case NO_RECOVERY:

printf(" A nonrecoverable name server error occurred.\n");

break;

case TRY_AGAIN:

printf("A temporary error occurred in the authoritative \

domain name server. Please try again later.\n");

break;

}

}

return 0;

}

gethostbyname()和gethostbyaddr()函数功能返回的HOSTENT的结构或NULL指针。

如果出现错误。h_errno的变量保存的错误号。

错误的可变h_errno的可以具有以下值:

HOST_NOT_FOUND

指定的主机是未知的。

NO_ADDRESS或NO_DATA

请求的名称是有效的,但没有一个IP地址。

NO_RECOVERY

不可恢复的名称服务器发生错误。

TRY_AGAIN

一个临时错误发生在权威域名服务器。请稍后再试。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值