inet_netof()并返回主机字节顺序 #包括 #包括 #包括 #包括 #包括 int main(int argc,字符**argv){ 无符号长地址; 无符号长网络标识; 地址中的结构sockaddr_; addr=inet_addr(“192.168.50.25”); //按主机字节顺序打印1932A8C0,在本例中为小尾端 printf(“主机字节顺序的IP地址:%08X\n”,(无符号int)addr); //将结构归零 memset(&address,0,sizeof address); //将地址复制到结构中 memcpy(&address.sin\u addr.s\u addr,&addr,sizeof addr); //inet_netof以主机字节顺序返回网络ID net\u id=inet\u netof(address.sin\u addr); //打印00C0A832为什么不打印0032A8C0? printf(“主机字节顺序的网络ID:%08X\n”,(未签名的int)网络ID); 返回0; }

inet_netof()并返回主机字节顺序 #包括 #包括 #包括 #包括 #包括 int main(int argc,字符**argv){ 无符号长地址; 无符号长网络标识; 地址中的结构sockaddr_; addr=inet_addr(“192.168.50.25”); //按主机字节顺序打印1932A8C0,在本例中为小尾端 printf(“主机字节顺序的IP地址:%08X\n”,(无符号int)addr); //将结构归零 memset(&address,0,sizeof address); //将地址复制到结构中 memcpy(&address.sin\u addr.s\u addr,&addr,sizeof addr); //inet_netof以主机字节顺序返回网络ID net\u id=inet\u netof(address.sin\u addr); //打印00C0A832为什么不打印0032A8C0? printf(“主机字节顺序的网络ID:%08X\n”,(未签名的int)网络ID); 返回0; },c,sockets,endianness,C,Sockets,Endianness,我有一个小的endian机器,所以主机字节顺序是小endian。IP地址192.168.50.25按主机字节顺序为0x1932A8C0。这对我来说是显而易见的 现在,inet_netof函数以主机字节顺序返回地址192.168.50.25的networkID。其输出为0x00C0A832。这让我很困惑。在我看来,这真的不像是一件小事。如果我把它转换成带点的十进制数,它将是:0.192.168.50 我假设网络ID在little endian中是这样的:0.50.168.192或0x0032A8C

我有一个小的endian机器,所以主机字节顺序是小endian。IP地址192.168.50.25按主机字节顺序为0x1932A8C0。这对我来说是显而易见的

现在,inet_netof函数以主机字节顺序返回地址192.168.50.25的networkID。其输出为0x00C0A832。这让我很困惑。在我看来,这真的不像是一件小事。如果我把它转换成带点的十进制数,它将是:0.192.168.50

我假设网络ID在little endian中是这样的:0.50.168.192或0x0032A8C0十六进制,而不是inet_netof()返回0x00C0A832

为什么inet_netof()返回0x00C0A832而不是0x0032A8C0

  • inet\u addr()
    返回网络字节顺序
  • 您收到的网络号为三个字节
  • inet\u netof()
    返回主机字节顺序,即小尾端。由于您的机器是Little endian,因此与返回的
    inet\u addr()
    的输出相比,
    inet\u netof()返回的字节顺序相反

  • 参考请参见。

    0x00C0A832
    正如您所说,
    0.192.168.50

    它是按主机字节顺序排列的,并且是正确的值


    文档中的措辞有点模糊(“在中返回Internet地址的网络号部分”),但如果我选中an,则会出现右移,因此一切似乎都正常。

    网络字节顺序是Big-endian
    inet\u addr()
    返回网络字节顺序。是。inet_addr()返回网络字节顺序,但我询问的是inet_netof()函数。函数的作用是:返回主机字节顺序。我在第一个printf()语句中犯了一个错误,它显然是按网络字节顺序的。看来我的逻辑全错了?!对我解决了我的错误思想。我假设inet_addr()返回little endian,但正如您指出的那样,这是错误的。相反,它是big endian。如果我假设inet_addr()返回网络字节顺序(确实如此),那么inet_netof()的输出完全有意义。
    #include <stdio.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <string.h>
    
    int main(int argc, char **argv) {
      unsigned long addr;
      unsigned long net_id;
      struct sockaddr_in address;
    
      addr = inet_addr("192.168.50.25");
      // prints 1932A8C0 in host byte-order which in this case is little-endian
      printf("IP-Address in Host-Byte Order: %08X\n", (unsigned int) addr);
    
    
      // zero the struct out
      memset(&address, 0, sizeof address);
    
      // copy address into the struct   
      memcpy(&address.sin_addr.s_addr, &addr, sizeof addr); 
    
      // inet_netof returns the network ID in host byte order
      net_id = inet_netof(address.sin_addr);
    
      // prints 00C0A832 Why not 0032A8C0?
      printf("Network ID in Host-Byte-Order: %08X\n", (unsigned int) net_id);
      return 0;
    }