主机字节序和网络字节序转换函数

#include <arpa/inet.h>

uint32_t htonl(uint32_t hostlong);<主机字节序转换位网络字节序32,转换IP地址时用>
uint16_t htons(uint16_t hostshort);<主机字节序16位转换为网络字节序16位,转换端口时用>

uint32_t ntohl(uint32_t netlong);<网络字节序32位转换为主机字节序32,读的时候会涉及>
uint16_t ntohs(uint6_t netshort)<网络字节序16位转换位16位主机字节序,同上>

IP地址转换函数;
#include <arpa/inet.h>
int inet_pton(int af,const charsrc,chardst);192.168.1.24----->网络字节序
const char* inet_ntop(int af,const voidsrc,char dst,socklen_tsize);网络字节序—》点分十进制字符串IP;
参数af:如果时IPv4,选AF_INET,IPV6选择AF_INET6;

猜你喜欢

转载自blog.csdn.net/weixin_37603532/article/details/90140269