zoukankan      html  css  js  c++  java
  • Linux网络编程IPv4和IPv6的inet_addr、inet_aton、inet_pton等函数小结

    知识背景:

    210.25.132.181属于IP地址的ASCII表示法,也就是字符串形式。英语叫做IPv4 numbers-and-dots notation。

    如果把210.25.132.181转换为整数形式,是3524887733,这个就是整数形式的IP地址。英语叫做binary data。(其实binary是二进制的意思)

    详细介绍,请参考: 网络字节序与主机字节序的转换 

     

     

    问题所在:

    如何在字符串形式的IP和整数形式的IP之间转换呢?

     

     

    转换函数:

    int inet_aton(const char *cp, struct in_addr *inp);

    in_addr_t inet_addr(const char *cp);

    in_addr_t inet_network(const char *cp);

    int inet_pton(int af, const char *src, void *dst);

    const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt);

     

    参考:http://beej.us/guide/bgnet/output/html/multipage/inet_ntopman.html

    =============================================================

    IPv4:

     

    IP字符串 ——》 网络字节流

    inet_addr、inet_network、inet_aton

     

    程序代码:

    1. #include <stdio.h>  
    2. #include <stdlib.h>  
    3. #include <unistd.h>  
    4. #include <string.h>  
    5. #include <netinet/in.h>  
    6. #include <sys/socket.h>  
    7. #include <sys/types.h>  
    8.   
    9. int main()  
    10. {  
    11.     char ip[] = "192.168.0.74";  
    12.     long r1, r2, r3;  //long  
    13.     struct in_addr addr;  
    14.   
    15.     r1 = inet_addr(ip); //返回网络字节序  
    16.     if(-1 == r1){  
    17.         printf("inet_addr return -1/n");  
    18.     }else{  
    19.         printf("inet_addr ip: %ld/n", r1);  
    20.     }  
    21.       
    22.     r2 = inet_network(ip);    //返回主机字节序  
    23.     if(-1 == r2){  
    24.         printf("inet_addr return -1/n");  
    25.     }else{  
    26.         printf("inet_network ip: %ld/n", r2);   
    27.         printf("inet_network ip: %ld/n", ntohl(r2));   //ntohl: 主机字节序 ——> 网络字节序  
    28.     }  
    29.       
    30.     r3 = inet_aton(ip, &addr);  //返回网络字节序  
    31.     if(0 == r3){  
    32.         printf("inet_aton return -1/n");  
    33.     }else{  
    34.         printf("inet_aton ip: %ld/n", addr.s_addr);  
    35.     }  
    36.   
    37. /*****  批量注释的一种方法  *****/  
    38. #if 0    
    39.     r3 = inet_aton(ip, addr);  
    40.     if(0 == r3){  
    41.         printf("inet_aton return -1/n");  
    42.     }else{  
    43.         printf("inet_aton ip: %ld/n", ntohl(addr.s_addr));  
    44.     }  
    45. #endif  
    46.   
    47.     return 0;  
    48. }  

    运行结果:

    [work@db-testing-com06-vm3.db01.baidu.com net]$ gcc -W -o inet_addr inet_addr.c 
    [work@db-testing-com06-vm3.db01.baidu.com net]$ ./inet_addr                     
    inet_addr ip: 1241557184
    inet_network ip: -1062731702
    inet_network ip: 1241557184
    inet_aton ip: 1241557184 

     

    --------------------------------------------------------------------------

     

    IP字符串 《——》 网络字节流

    inet_addr、inet_aton、inet_ntoa

     

     程序代码:

    1. #include <stdio.h>  
    2. #include <sys/socket.h>  
    3. #include <netinet/in.h>  
    4. #include <arpa/inet.h>  
    5. #include <string.h>  
    6. int main(int argc, char *argv[])  
    7. {  
    8.     char ip1[] = "192.168.0.74";  
    9.     char ip2[] = "211.100.21.179";  
    10.     struct in_addr addr1, addr2;  
    11.     long l1, l2;  
    12.     l1 = inet_addr(ip1);   //IP字符串——》网络字节  
    13.     l2 = inet_addr(ip2);  
    14.     printf("IP1: %s/nIP2: %s/n", ip1, ip2);  
    15.     printf("Addr1: %ld/nAddr2: %ld/n", l1, l2);  
    16.       
    17.     memcpy(&addr1, &l1, 4); //复制4个字节大小  
    18.     memcpy(&addr2, &l2, 4);  
    19.     printf("%s <--> %s/n", inet_ntoa(addr1), inet_ntoa(addr2)); //注意:printf函数自右向左求值、覆盖  
    20.     printf("%s/n", inet_ntoa(addr1)); //网络字节 ——》IP字符串  
    21.     printf("%s/n", inet_ntoa(addr2));  
    22.     return 0;  
    23. }  

    运行结果:

     

    [work@db-testing-com06-vm3.db01.baidu.com net]$ gcc -W -o inet_ntoa inet_ntoa.c 

    [work@db-testing-com06-vm3.db01.baidu.com net]$ ./inet_ntoa                                      

    IP1: 192.168.0.74

    IP2: 211.100.21.179

    Addr1: 1241557184

    Addr2: 3004523731

    192.168.0.74 <--> 192.168.0.74

    192.168.0.74

    211.100.21.179

     

    ============================================================= 

     

    IPv6:

     

    IPv4 字符串 《——》网络字节流

    inet_pton、inet_ntop

     

    程序代码:

     

    1. #include <stdio.h>  
    2. #include <sys/types.h>  
    3. #include <sys/socket.h>  
    4. #include <arpa/inet.h>  
    5.   
    6. int main()  
    7. {  
    8.     char ip[] = "192.168.0.74";   
    9.     struct in_addr addr;  
    10.       
    11.     int ret = inet_pton(AF_INET, ip, (void *)&addr);   //IP字符串 ——》网络字节流  
    12.     if(0 == ret){  
    13.         printf("inet_pton error, return 0/n");  
    14.         return -1;  
    15.     }else{  
    16.         printf("inet_pton ip: %ld/n", addr.s_addr);  
    17.         printf("inet_pton ip: 0x%x/n", addr.s_addr);  
    18.     }  
    19.   
    20.     const char *pstr = inet_ntop(AF_INET, (void *)&addr, ip, 128);  //网络字节流 ——》IP字符串  
    21.     if(NULL == pstr){  
    22.         printf("inet_ntop error, return NULL/n");  
    23.         return -1;  
    24.     }else{  
    25.         printf("inet_ntop ip: %s/n", ip);  
    26.     }  
    27.       
    28.     return 0;  
    29. }  

    运行结果:

    [work@db-testing-com06-vm3.db01.baidu.com net]$ gcc -W -o inet_ptoa inet_ptoa.c 
    [work@db-testing-com06-vm3.db01.baidu.com net]$ ./inet_ptoa                     
    inet_pton ip: 1241557184
    inet_pton ip: 0x4a00a8c0
    inet_ntop ip: 192.168.0.74

     

     

    --------------------------------------------------------------------------

    IPv6 字符串 《——》网络字节流

    inet_pton、inet_ntop

     

    程序代码:

    1. #include <stdio.h>  
    2. #include <stdlib.h>  
    3. #include <string.h>  
    4. #include <arpa/inet.h>  
    5.   
    6. int   
    7. main(int argc, char **argv)  
    8. {  
    9.     unsigned char buf[sizeof(struct in6_addr)];  
    10.     int domain, s;  
    11.     char str[INET6_ADDRSTRLEN];  
    12.   
    13.     if(argc != 3){  
    14.         fprintf(stderr, "usage: %s {i4|i6|<num>} string/n", argv[0]);  
    15.         exit(EXIT_FAILURE);  
    16.     }  
    17.   
    18.     domain = (strcmp(argv[1], "i4") == 0) ? AF_INET:(strcmp(argv[1], "i6") == 0) ? AF_INET6 : atoi(argv[1]);  
    19.       
    20.     //IP字符串 ——》网络字节流  
    21.     s = inet_pton(domain, argv[2], buf);  
    22.     if(s<=0){  
    23.         if(0 == s)  
    24.             fprintf(stderr, "Not in presentation format/n");  
    25.         else  
    26.             perror("inet_pton");  
    27.         exit(EXIT_FAILURE);  
    28.     }  
    29.   
    30.     //网络字节流 ——》IP字符串  
    31.     if(inet_ntop(domain, buf, str, INET6_ADDRSTRLEN) == NULL){    
    32.         perror("inet ntop/n");  
    33.         exit(EXIT_FAILURE);  
    34.     }  
    35.   
    36.     printf("%s/n", str);  
    37.     exit(EXIT_SUCCESS);  
    38. }  
    运行结果:

    [work@db-testing-com06-vm3.db01.baidu.com net]$ gcc -W -o inet_ptoa6 inet_ptoa6.c                               
    [work@db-testing-com06-vm3.db01.baidu.com net]$ ./inet_ptoa6 i6 0:0:0:0:0:FFFF:204.152.189.116   
    ::ffff:204.152.189.116
    [work@db-testing-com06-vm3.db01.baidu.com net]$ ./inet_ptoa6 i4 204.152.189.116               
    204.152.189.116

  • 相关阅读:
    Numpy学习1
    spark SQL学习(综合案例-日志分析)
    spark SQL学习(认识spark SQL)
    spark SQL学习(案例-统计每日销售)
    spark SQL学习(案例-统计每日uv)
    spark SQL学习(spark连接 mysql)
    spark SQL学习(spark连接hive)
    spark SQL学习(数据源之json)
    常用的Java工具类——十六种
    Idea格式化快捷键无效,没反应
  • 原文地址:https://www.cnblogs.com/xuxm2007/p/2144994.html
Copyright © 2011-2022 走看看