Linux 之网络编程-地址转换

在这里插入图片描述

1、IPV4地址转换函数

/*将点分十进制IP地址转换为32位网络字节顺序的IP地址*/
int inet_addr(const char* cp);

/*将点分十进制IP地址转换为32位主机字节顺序的IP地址*/
int inet_network(const char* cp);

/*将点分十进制的字符串转换为32位网络字节顺序的IP地址*/
char* inet_aton(const char* cp,struct in_addr in);

/*将32位网络字节顺序的IP地址转换为点分十进制的字符串*/
char* inet_ntoa(struct in_addr in);

2、地址类型转换

/*将存在在cp位置,地址类型为af的点分十进制地址转换到buf中,若af为IPv4,则buf应为struct in_addr,若af为IPv6,则buf应为struct in6_addr*/
int inet_pton(int af,const char* cp,void* buf);

/*将存储在位置cp,地址协议为af的网络字节序的32位IP地址转换为点分十进制,并存储在长度为len的buf中,若af为IPv4,则cp应为struct in_addr类型,若af为IPv6,则cp应为struct in6_addr类型*/
char* inet_ntop(int af,void* cp,char* buf,socketlen_t len);

3、通过IP地址获取网络号和主机号(IPv4)

/*从IP地址in(32位网络字节序)中提取标准主机号*/
int inet_lnaof(struct in_addr in);

/*从IP地址in(32位网络字节序)中提取标准网络号*/
int inet_netof(struct in_addr in);

/*将网络号(网络字节序)和主机号(网络字节序)组合成一个标准IP地址(网络字节序)*/
struct in_addr inet_makeaddr(int net,int host);

4、字节顺序转换函数

/*长字节字节顺序转换函数*/
unsigned long int ntohl(unsigned long int net);
unsigned long int htonl(unsigned long int host);

/*短字节字节顺序转换函数*/
unsigned short int ntohl(unsigned short int net);
unsigned short int htonl(unsigned short int host);

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Chiang木

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值