Network byte order & host byte order

Source: Internet
Author: User

What is a byte order?       Refers to the order in which integers are saved in memory. There are two kinds of byte order: A.little Endinan Low store in memory, high address in memory, B.big endian high store in memory low address, low store in memory;

A simple example: DWORD dwcount = 0x01020304; Such a double-byte variable is distributed in memory as follows 0013ff70 04 03 02 01 (Note: The results of the experiment XPSP3 VC6.0, indicating that my machine is little Endinan order) And if the big endian order, it should be 0013ff70 01 02 03 04. With more x86 series of CPUs, are little Endinan sequence.

and network byte order This is a TCP/IP protocol defined in a data representation format, it is not related to your machine's CPU, the operating system and so on, so that the data in the network transmission, no matter how the kind can be interpreted correctly, the network byte order selected big endian. This gives the programmer to write the network program a problem, the programmer must convert the local data from the host byte sequence to the network byte sequence sent to the host, while the host must also transfer the received data from the network byte order to the cost of byte order, so as to ensure that the two sides correctly send and receive data, or generate a bug.

WINAPI provides four functions for network byte order to native byte order and native byte order to network byte order conversion: htons: Convert unsigned short from host to network order HTONL: Convert unsigned long from host to network order Ntohs: Convert the unsigned short type from the network sequence to the host order Ntohl: Convert the unsigned long type from the network sequence to the host order (these four APIs can be remembered, s means short,l means long,n represents the network, H = host)

In addition: The char type is a problem with no byte order, only a byte-order problem with data types larger than one byte. Many translation books translate little endinan into small-end mode, translating big endian into large-endian mode.

[End]

Network byte order & host byte order

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.