为什么 inet_pton 和 inet_addr (1734763876) 的 IPv4 十进制值与使用这 2 个网站 (1684366951) 得到的值不同?
struct sockaddr_in sin;
inet_pton(AF_INET, "100.101.102.103", &(sin.sin_addr));
printf("%i\n%i\n", inet_addr("100.101.102.103"), sin.sin_addr);
- http://www.allredroster.com/iptodec.htm http://www.allredroster.com/iptodec.htm
- http://www.ipaddresslocation.org/convertip.php http://www.ipaddresslocation.org/convertip.php
字节序 - 它们具有相反顺序的四个字节:
1734763876 = 0x67 66 65 64
1684366951 = 0x64 65 66 67
您需要用于 URL 等的值是按“网络”顺序排列的值,即最高有效字节在前。使用htonl()
(host-to-network-long) 转换值,即
printf("%i\n%i\n", htonl(inet_addr("100.101.102.103")), htonl(sin.sin_addr));
caf 在下面指出,我可能有这个倒退:问题实际上是您需要将网络顺序数据从套接字函数转换回主机顺序以进行显示,即
printf("%i\n%i\n", ntohl(inet_addr("100.101.102.103")), ntohl(sin.sin_addr));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)