1.网络和主机字节序间转换函数 32位主机序转网络字节序
hotnl 函数将 32 位整数由主机字节顺序转换为网络字节顺序。ntohl 函数将 32 位整数从网络字节顺序转换为主机字节。
htons 和 ntohs 函数为 16 位无符号整数执行相应的转换。注意,没有对应的处理 64 位值的函数
#include <arpa/inet.h>
uint32_t htonl(uint32_t hostlong);
uint16_t htons(uint16_t hostshort);
// 返回:按照网络字节顺序的值。
uint32_t ntohl(uint32_t netlong);
uint16_t ntohs(unit16_t netshort);
// 返回:按照主机字节顺序的值。
2.点分十进制 字符串转网络字节序
补充:
HOSTNAME 命令来确定你自己主机的点分十进制地址:
linux> hostname -i
128.2.210.175
应用程序使用 inet_pton 和 inet_ntop 函数来实现 IP 地址和点分十进制串之间的转换
函数中p和n分别代表表达(presentation)和数值(numeric)
#include <arpa/inet.h>
int inet_pton(AF_INET, const char *src, void *dst);
// 返回:若成功则为 1,若 src 为非法点分十进制地址则为 0,若出错则为 -1。
const char *inet_ntop(AF_INET, const void *src, char *dst,
socklen_