Principles of byte sequence conversion during network communication and network byte sequence, large-end and small-end Modes

Source: Internet
Author: User

Introduction: Do I need to convert the byte order during network communication? 

Platforms of the same byte sequence can not perform byte sequence conversion during network communication, but must perform byte sequence conversion for cross-platform network data communication. The reason is as follows: the network protocol specifies that the first byte received is a high byte, which is stored in a low address. Therefore, when sending a message, the high byte of data is obtained first. When the multi-byte data in the small-end mode is stored, the low address stores the low-byte data, and when sent by the sender's network protocol function, the data is first retrieved from the low address (to retrieve the high byte, the receiver's network protocol function stores the first received byte in the lower address (to receive the high byte, it actually receives the low byte ), therefore, both parties finally correctly send and receive data. When both parties perform the conversion on the same platform, although the data can be correctly sent and received, the conversion is meaningless, resulting in a waste of resources. Conversion is required for communication between different platforms. If the conversion is not performed, the data is incorrectly sent and received. The byte sequence conversion function converts the data according to the storage mode of the current platform, if the current platform is large, direct return is not converted. If the current platform is small, network bytes are received for conversion. The following introduces some concepts:

 

I. Big end and Small End
"Big End" and "Small End" indicate the end of the Multi-byte value stored at the starting address of the value; the small end is stored at the starting address, that is, the small end in the byte order; the large end is stored at the starting address, that is, the large-end byte order;
Or:
1. The little-Endian method is the low address (that is, the starting address of this value) of the low byte emission in the memory, and the high byte emission in the high address of the memory;
2. The big-Endian method is the low address (that is, the starting address of this value) of the high byte emission in the memory, and the low byte emission in the high address of the memory;
For a simple example, for integer data 0x12345678, its storage methods in big-end and small-end systems are shown in 1:


Ii. Network byte order
Data transmitted over the network is a byte stream. Which byte is transmitted for a Multi-byte value during network transmission? That is to say, when the receiving end receives the first byte, it makes sense to treat this Byte as a high byte or a low byte;
UDP/TCP/IP protocol rules: Treat the first byte received as a high byte, which requires the first byte sent by the sending end to be a high byte; when sending data from the sender, the first byte sent is the byte corresponding to the value at the starting address in the memory. That is to say, the Byte corresponding to the starting address of the value in the memory is the first high byte to be sent (that is, the high byte is stored at the low address, before sending a multi-byte value, it is stored in the memory as a large-end value;
Therefore, the network byte order is the large-end byte order;
For example, when an integer value 0x12345678 is sent over the network, it is stored on the 80x86 platform as a small client. Before sending, you need to use the byte sequence Conversion Function htonl () provided by the system () convert it to a value stored in the big terminal method, as shown in Figure 2:


Iii. Test in byte order
The bytecode on different CPU platforms is usually different. The following simple code can test the bytecode on different platforms:
# Include <stdio. h>
# Include <netinet/in. h>
Int main (INT argc, char ** argv)
{
Int num = 0x12345678;
Unsigned char * Pc = (unsigned char *) (& num );
Printf ("Local order: \ n ");
Printf ("[0]: 0x % x ADDR: % u \ n", PC [0], & PC [0]);
Printf ("[1]: 0x % x ADDR: % u \ n", PC [1], & PC [1]);
Printf ("[2]: 0x % x ADDR: % u \ n", PC [2], & PC [2]);
Printf ("[3]: 0x % x ADDR: % u \ n", PC [3], & PC [3]);
Num = htonl (Num );
Printf ("htonl order: \ n ");
Printf ("[0]: 0x % x ADDR: % u \ n", PC [0], & PC [0]);
Printf ("[1]: 0x % x ADDR: % u \ n", PC [1], & PC [1]);
Printf ("[2]: 0x % x ADDR: % u \ n", PC [2], & PC [2]);
Printf ("[3]: 0x % x ADDR: % u \ n", PC [3], & PC [3]);
Return 0;
}
Output on the iSCSI platform:
Local order:
[0]: 0x12 ADDR: 4290770212 // The high byte is stored at the low address, which is the big-end method;
[1]: 0x34 ADDR: 4290770213
[2]: 0x56 ADDR: 4290770214
[3]: 0x78 ADDR: 4290770215 // low byte is stored at the high address;
Htonl order:
[0]: 0x12 ADDR: 4290770212 // The host sequence is the same as that of network bytes;
[1]: 0x34 ADDR: 4290770213
[2]: 0x56 ADDR: 4290770214
[3]: 0x78 ADDR: 4290770215
Output on the x86 Platform:
Local order:
[0]: 0x78 ADDR: 4289157020 // the low-level bytes are stored at the low-end address;
[1]: 0x56 ADDR: 4289157021
[2]: 0x34 ADDR: 4289157022
[3]: 0x12 ADDR: 4289157023 // high byte is stored at the high address;
Htonl order:
[0]: 0x12 ADDR: 4289157020 // the host's byte order is different from that of network bytes;
[1]: 0x34 ADDR: 4289157021
[2]: 0x56 ADDR: 4289157022
[3]: 0x78 ADDR: 4289157023
Output on Intel Platform:
Local order:
[0]: 0x78 ADDR: 1245044 // the low-level bytes are stored at the low-end address;
[1]: 0x56 ADDR: 1245045
[2]: 0x34 ADDR: 1245046
[3]: 0x12 ADDR: 1245047 // high byte is stored at the high address;
Htonl order:
[0]: 0x12 ADDR: 1245044 // the host's byte order is different from that of network bytes;
[1]: 0x34 ADDR: 1245045
[2]: 0x56 ADDR: 1245046
[3]: 0x78 ADDR: 1245047

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.