Get the App
SLTechnology News&Howtos  ›  Network Security  › 

Network order, host order learning

Shulou Source: shulou.com Published: 2022-06-01 04:23:25 10月02日 Update

Network order, host order learning

Network order, host order is always confused. It needs to be tidied up.

A thousand words are not as real as the code. Haha, just do it.

# include # include # include int host_order () {unsigned long a = 0x12345678; unsigned char * p = (unsigned char *) (& a); printf ("host byte order:% 0x% 0x% 0x% 0x\ n", p [0], p [1], p [2], p [3]); unsigned long b = htonl (a); p = (unsigned char *) (& b) Printf ("Network byte order:% 0x% 0x% 0x\ n", p [0], p [1], p [2], p [3]); return 0;} int net_order () {struct in_addr ipaddr; unsigned long addr = inet_addr ("192.168.1.100"); unsigned char * p = (unsigned char *) (& addr); printf ("addr =% u\ n", addr) Printf ("% 0x% 0x% 0x: inet_addr 192.168.1.100\ n", p [0], p [1], p [2], p [3]); printf ("% d% d: inet_addr 192.168.1.100\ n", p [0], p [1], p [2], p [3]); unsigned long addr_host = ntohl (addr); p = (unsigned char *) (& addr_host) Printf ("% 0x% 0x% 0x: inet_addr ntohl 192.168.1.100\ n", p [0], p [1], p [2], p [3]); printf ("% d% d: inet_addr ntohl 192.168.1.100\ n", p [0], p [1], p [2], p [3]); printf ("addr_host =% u\ n", addr_host); return 0 } int main () {int ret = 0; / / ret = host_order (); ret = net_order (); return ret;}

Compile it.

Gcc-g-o test_order test_order.c

Run.

[root@localhost test] #. / test_order addr = 1677830336 c0a8 1 64: inet_addr 192.168.1.100192 168 1100: inet_addr 192.168.1.10064 1 a8 c0: inet_addr ntohl 192.168.1.100100 1168 192: inet_addr ntohl 192.168.1.100addr_host = 3232235876

Clear and clear. It helps to understand quickly.

Tags: Host network byte learning thousands of words code learning network understanding ambiguity compilation Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno MariaDB macOS Shulou Technology MySQL