linux把域名转换成ip,域名,ip相互转换(Linux,getaddrinfo, getnameinfo)--- 转

参考链接:

代码:

01/* ip_to_hostname ip */

02#include

03#include

04#include

05#include

06#include

07#include

08#include

09

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

11{

12if (argc != 2)

13{

14fprintf(stderr,"Usage: %s hostname\n", argv[0]);

15exit(EXIT_FAILURE);

16}

17struct addrinfo hints;

18struct addrinfo *result, *result_pointer;

19int ret;

20/* obtaining address matching host */

21memset(&hints, 0,sizeof(struct addrinfo));

22hints.ai_family = AF_UNSPEC;

23hints.ai_socktype = SOCK_STREAM;

24hints.ai_flags = AI_CANONNAME | AI_NUMERICHOST;

25hints.ai_protocol = 0;/* any protocol */

26

27//      ret = getaddrinfo(argv[1], NULL, &hints, &result);

28ret = getaddrinfo(argv[1], NULL, &hints, &result);

29if (ret != 0)

30{

31fprintf(stderr,"getaddrinfo: %s\n", gai_strerror(ret));

32exit(EXIT_FAILURE);

33}

34/* traverse the returned list and output the ip addresses */

35for (result_pointer = result; result_pointer != NULL; result_pointer = result_pointer->ai_next)

36{

37char hostname[1025] ="";

38ret = getnameinfo(result_pointer->ai_addr, result_pointer->ai_addrlen, hostname,sizeof(hostname), NULL, 0, NI_NAMEREQD);

39if (ret != 0)

40{

41fprintf(stderr,"error in getnameinfo: %s \n", gai_strerror(ret));

42}

43else

44{

45printf("hostname: %s \n", hostname);

46}

47//              printf("hostname: %s \n", result_pointer->ai_canonname);

48}

49freeaddrinfo(result);

50exit(EXIT_SUCCESS);

51}

01/* hostname_to_ip hostname */

02#include

03#include

04#include

05#include

06#include

07#include

08#include

09

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

11{

12if (argc != 2)

13{

14fprintf(stderr,"Usage: %s hostname\n", argv[0]);

15exit(EXIT_FAILURE);

16}

17struct addrinfo hints;

18struct addrinfo *result, *result_pointer;

19int ret;

20/* obtaining address matching host */

21memset(&hints, 0,sizeof(struct addrinfo));

22hints.ai_family = AF_UNSPEC;

23hints.ai_socktype = SOCK_STREAM;

24hints.ai_flags = AI_CANONNAME;

25hints.ai_protocol = 0;/* any protocol */

26

27//      ret = getaddrinfo(argv[1], NULL, &hints, &result);

28ret = getaddrinfo(argv[1], NULL, &hints, &result);

29if (ret != 0)

30{

31fprintf(stderr,"getaddrinfo: %s\n", gai_strerror(ret));

32exit(EXIT_FAILURE);

33}

34/* traverse the returned list and output the ip addresses */

35for (result_pointer = result; result_pointer != NULL; result_pointer = result_pointer->ai_next)

36{

37char hostname[1025] ="";

38ret = getnameinfo(result_pointer->ai_addr, result_pointer->ai_addrlen, hostname,sizeof(hostname), NULL, 0, NI_NUMERICHOST);

39if (ret != 0)

40{

41fprintf(stderr,"error in getnameinfo: %s \n", gai_strerror(ret));

42continue;

43}

44else

45{

46printf("IP: %s \n", hostname);

47}

48}

49freeaddrinfo(result);

50exit(EXIT_SUCCESS);

51}

结果:

root@localhost :/home/James/mypro/Linux-Pro/Network# ./hostname_to_ip baidu.com

IP: 123.125.114.144

IP: 220.181.111.85

IP: 220.181.111.86

root@localhost :/home/James/mypro/Linux-Pro/Network# ./ip_to_hostname 220.181.111.86

error in getnameinfo: Name or service not known

root@localhost :/home/James/mypro/Linux-Pro/Network# ./ip_to_hostname 10.0.0.78

hostname: localhost

root@localhost:/home/James/mypro/Linux-Pro/Network# ./ip_to_hostname 59.66.137.62

hostname: th137062.ip.tsinghua.edu.cn

root@localhost:/home/James/mypro/Linux-Pro/Network# ./ip_to_hostname 202.106.182.229

hostname: mail182-229.sinamail.sina.com.cn

root@localhost:/home/James/mypro/Linux-Pro/Network# ./ip_to_hostname 211.147.4.7

hostname: mail3.douban.com

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值