socket地址转换函数归纳

  • 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代表主机位

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值