Chinaunix首页 | 论坛 | 博客
  • 博客访问: 138702
  • 博文数量: 20
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 247
  • 用 户 组: 普通用户
  • 注册时间: 2014-04-12 22:24
个人简介

学霸

文章分类

全部博文(20)

文章存档

2016年(1)

2015年(11)

2014年(8)

我的朋友

分类: C/C++

2016-03-01 22:10:41

地址转换函数:在ASCII地址字符串(IPv4的点分十进制或IPv6的格式)与网络字节序的二进制值之间的转换
   
a:ASCILL n:network

//inet_aton/inet_addr/inet_ntoa 适用于IPv4
int inet_aton(const char *cp, struct in_addr *inp);
返回值:成功(cp有效)返回1,否则为0
注意:如果inp为NULL,对cp依然做有效性检查,但不存储结果。

in_addr_t inet_addr(const char *cp);
返回值:成功(cp有效)返回32位二进制IPv4地址,否则返回INADDR_NONE(通常情况下为常值0xffffffff,但在某些编译器中为常值-1)
注意:当解析到的IP地址本来就是255.255.255.255时,与失败返回值冲突。

char *inet_ntoa(struct in_addr in);
返回值:返回指向一个点分十进制字符串的指针

//inet_pton/inet_ntop    适用于IPv4和IPv6
int inet_pton(int af, const char *src, void *dst);
返回值:成功返回1,cp无效或与af不对应返回0,出错(af不支持的地址族)返回-1

const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);
返回值:成功返回指向一个ASCII地址字符串的指针,出错(af不支持的地址族)返回NULL

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <strings.h>
  3. #include <arpa/inet.h>
  4. #include <netinet/in.h>

  5. int main(void)
  6. {
  7.     int ret = 0;
  8.     struct sockaddr_in addr_in;
  9.     char *str = NULL;
  10.     char str_addr[256] = { 0 };
  11.     char *ip_string = "10.240.197.172";
  12.     unsigned int ip_int = 0xacc5f00a;

  13.     /* inet_aton */
  14.     addr_in.sin_addr.s_addr = 0;
  15.     ret = inet_aton(ip_string, &addr_in.sin_addr);
  16.     printf("inet_aton 000, ret = %d, addr = %x\n",ret, addr_in.sin_addr.s_addr);

  17.     addr_in.sin_addr.s_addr = 0;
  18.     ret = inet_aton(ip_string, NULL);
  19.     printf("inet_aton 111, ret = %d, addr = %x\n",ret, addr_in.sin_addr.s_addr);

  20.     addr_in.sin_addr.s_addr = 0;
  21.     ret = inet_aton("aaa", &addr_in.sin_addr);
  22.     printf("inet_aton 222, ret = %d, addr = %x\n",ret, addr_in.sin_addr.s_addr);

  23.     /* inet_addr */
  24.     addr_in.sin_addr.s_addr = 0;
  25.     addr_in.sin_addr.s_addr = inet_addr("aaa");
  26.     printf("inet_addr aaa, addr = %x\n", addr_in.sin_addr.s_addr);

  27.     addr_in.sin_addr.s_addr = 0;
  28.     addr_in.sin_addr.s_addr = inet_addr(ip_string);
  29.     printf("inet_addr bbb, addr = %x\n", addr_in.sin_addr.s_addr);

  30.     /* inet_ntoa */
  31.     addr_in.sin_addr.s_addr = 123;
  32.     str = inet_ntoa(addr_in.sin_addr);
  33.     printf("inet_ntoa 333, addr = %s\n", str);

  34.     addr_in.sin_addr.s_addr = ip_int;
  35.     str = inet_ntoa(addr_in.sin_addr);
  36.     printf("inet_ntoa 444, addr = %s\n", str);

  37.     /* inet_pton */
  38.     addr_in.sin_addr.s_addr = 0;
  39.     ret = inet_pton(AF_INET, ip_string, &addr_in.sin_addr);
  40.     printf("inet_pton ccc, ret = %d, addr = %x\n",ret, addr_in.sin_addr.s_addr);

  41.     addr_in.sin_addr.s_addr = 0;
  42.     ret = inet_pton(AF_INET, "aaa", &addr_in.sin_addr);
  43.     printf("inet_pton ddd, ret = %d, addr = %x\n",ret, addr_in.sin_addr.s_addr);

  44.     addr_in.sin_addr.s_addr = 0;
  45.     ret = inet_pton(AF_INET6, ip_string, &addr_in.sin_addr);
  46.     printf("inet_pton eee, ret = %d, addr = %x\n",ret, addr_in.sin_addr.s_addr);

  47.     addr_in.sin_addr.s_addr = 0;
  48.     ret = inet_pton(AF_LOCAL, ip_string, &addr_in.sin_addr);
  49.     printf("inet_pton fff, ret = %d, addr = %x\n", ret, addr_in.sin_addr.s_addr);

  50.     /* inet_ntop */
  51.     addr_in.sin_addr.s_addr = ip_int;
  52.     str = (char *)inet_ntop(AF_INET, &addr_in.sin_addr, str_addr, INET_ADDRSTRLEN);
  53.     printf("inet_ntop 555, str = %s, addr = %s\n", str, str_addr);

  54.     bzero(str_addr, 256);
  55.     str = (char *)inet_ntop(AF_INET6, &addr_in.sin_addr, str_addr, INET6_ADDRSTRLEN);
  56.     printf("inet_ntop 666, str = %s, addr = %s\n", str, str_addr);

  57.     bzero(str_addr, 256);
  58.     str = (char *)inet_ntop(AF_LOCAL, &addr_in.sin_addr, str_addr, INET_ADDRSTRLEN);
  59.     printf("inet_ntop 777, str = %s, addr = %s\n", str, str_addr);

  60.     return;
  61. }

--result
inet_aton 000, ret = 1, addr = acc5f00a
inet_aton 111, ret = 1, addr = 0
inet_aton 222, ret = 0, addr = 0
inet_addr aaa, addr = ffffffff
inet_addr bbb, addr = acc5f00a
inet_ntoa 333, addr = 123.0.0.0
inet_ntoa 444, addr = 10.240.197.172
inet_pton ccc, ret = 1, addr = acc5f00a
inet_pton ddd, ret = 0, addr = 0
inet_pton eee, ret = 0, addr = 0
inet_pton fff, ret = -1, addr = 0
inet_ntop 555, str = 10.240.197.172, addr = 10.240.197.172
inet_ntop 666, str = af0:c5ac:850a:4000::a0fb:a02a, addr = af0:c5ac:850a:4000::a0fb:a02a
inet_ntop 777, str = (null), addr =
阅读(1734) | 评论(0) | 转发(0) |
0

上一篇:设备

下一篇:没有了

给主人留下些什么吧!~~