Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Network order, host order learning

2025-01-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report