The principle of byte-order transformation of network communication and network byte-order, big-endian and small-end modes

Source: Internet
Author: User

is there a need for byte-order conversion in network communication? A platform with the same byte order may not perform byte-order conversions while network communication is in progress, but byte-order conversions must be performed for network data communication across platforms. The reasons are as follows: The network protocol specifies that the first byte to be received is high byte, which is stored to a low address, so the sending will first go to the low address to fetch the high byte of the data. The small-end mode of multi-byte data at the time of storage, low-address storage is low-byte, and the Sender network protocol function is sent to the low address to fetch data first (want to take high bytes, really get low bytes), the Receiver network protocol function will receive the first byte to the low address (want to receive high bytes, Really receive a low byte), so the last two sides are correctly send and receive data. While the same platform for communication, if both sides of the conversion, although the data can be correctly sent and received, but the conversion is meaningless, resulting in a waste of resources. while the different platforms to communicate must be converted, do not convert will cause errors in sending and receiving data, the byte-order conversion function will be based on the current platform's storage mode to make the appropriate conversion, if the current platform is big-endian, then directly return no conversion, if the current platform is a small end, The network byte order that is received will be converted. two, big and small end

"big-endian" and "small-end" indicate which end of a multibyte value is stored at the starting address of the value, where the small end is stored at the start address, which is the small-endian byte order, and the big end is stored at the starting address, which is the big-endian byte order, or 1. Small End Method (Little-endian) The low byte is emitted at the lower address end of the memory (that is, the starting address of the value), and the high byte is discharged at the upper address end of the memory; 2. Big-endian (Big-endian) is the high-bit byte emitted at the low address end of the memory (that is, the starting address of the value), low-bit bytes emitted in the memory of the higher address; As a simple example, for integer data 0x12345678, it is stored in a system of big-endian and small-ended methods, as shown in 1:

third, network byte order

the data transmitted on the network is a byte stream, for a multi-byte numeric value, in the network transmission, the first pass which bytes? That is, when the receiver receives the first byte, it will be a high-byte or low-byte processing, is a more meaningful problem; The UDP/TCP/IP protocol provides that the first byte received is treated as a high byte, which requires that the first byte sent by the sending side is a high byte, whereas the first byte sent when the sender sends the data is the byte that corresponds to the value at the start address in memory, i.e., The byte that corresponds at the start address in memory is the first high byte to be sent (that is, the high byte is stored at the low address), so that the multibyte value is stored in memory because it is in the big-endian state before it is sent; so, the network byte order is the big-endian byte order; When we send integer numeric 0x12345678 over the network, in the 80x86 platform, it is stored as a small end, and it is necessary to use the system-provided byte-order conversion function htonl () to convert it to a value stored in the big-endian method before sending it, as shown in 2:

The principle of byte-order transformation of network communication and network byte-order, big-endian and small-end modes

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.