socket地址转换函数归纳

来源:互联网 发布:淘宝统计数据 编辑:程序博客网 时间:2024/06/06 05:02
  • inet_aton()

inet_aton()是将字符串形式的IP地址转换成网络字节序的32位IP地址。

语法:

    #include<sys/socket.h>

    #include<netinet/in.h>

    #include<arpa/inet.h>

int inet_aton(const char *string, struct in_addr *addr);


  • inet_ntoa()

把套接口中的IP地址转换成字符串形式的点分十进制形式,语法如下:

   #include<sys/socket.h>  

   #include<netinet/in.h>

   #include<arpa/inet.h>

char *inet_ntoa(struct in_addr addr);


  • inet_network() 

将点分十进制的IP地址转换成主机字节序的32位二进制IP地址,语法如下:

     #include<sys/socket.h>  

     #include<netinet/in.h>

     #include<arpa/inet.h>

unsigned long inet_network(const char *addr);

以主机字节序的形式返回结果可以保证用户安全地使用网络掩码。因为如果返回值是网络字节序的形式,那么对于不同的CPU平台,所使用的网络掩码和程序代码就会有所差异。

  • inet_lnaof()

将套接口地址中的IP地址(网络字节序)转换成没有网络位的主机ID(主机字节序)。语法如下:

  #include<sys/socket.h>  

  #include<netinet/in.h>

  #include<arpa/inet.h>

unsigned long inet_lnaof(struct in_addr addr);

比如:inet_lnaof(192.168.9.1)=0.0.0.1


  • inet_netof()

与上面函数不同处,inet_lnaof()返回主机ID,而inet_netof()的返回值是网络ID。语法如下:

  #include<sys/socket.h>  

  #include<netinet/in.h>

  #include<arpa/inet.h>

unsigned long inet_netof(struct in_addr addr);

比如:inet_netof(192.168.9.1)=0.192.168.9

  • inet_makeaddr()

把提取出的网络位和主机位合并起来生成一个新的IP地址。语法如下:

  #include<sys/socket.h>  

  #include<netinet/in.h>

  #include<arpa/inet.h>

struct in_addr inet_makeaddr(int net, int host);

net代表网络位,host代表主机位