Conversion policies of big endian and little endian between socket communication ipvs and Linux

Source: Internet
Author: User

Big endian and little endian

In this test, only the 32 bits intel X86 system and the Moto Power PC system TCP/IP socket connection joint debugging are discussed.
Conclusion:

Transmits a struct.
# Pragma pack (4)
Typedef struct {
Char name [6];
Int age;
Char sex;
Short Tel;
} Student;
# Pragma pack ()

Nbo network byte order network byte
HBO host byte order host byte

1. When both peer ends are x86 systems, the two ends are little endian. We don't need to worry about nbo and HBO conversion,

Initiator:
Student stu1;
Stu1.age = 0x00000304;
Memcpy (stu1.name, "abcdef", 6 );
Stu1.sex = 'F ';
Stu1.tel = 0x0809;

Receiver:
Age = 0x304
Tel = 0x809

Value = 0x61 ADDR = 0xbfe6d3f0
Value = 0x62 ADDR = 0xbfe6d3f1
Value = 0x63 ADDR = 0xbfe6d3f2
Value = 0x64 ADDR = 0xbfe6d3f3
Value = 0x65 ADDR = 0xbfe6d3f4
Value = 0x66 ADDR = 0xbfe6d3f5
Value = 0x0 ADDR = 0xbfe6d3f6
Value = 0x0 ADDR = 0xbfe6d3f7
Value = 0x4 ADDR = 0xbfe6d3f8
Value = 0x3 ADDR = 0xbfe6d3f9
Value = 0x0 ADDR = 0xbfe6d3fa
Value = 0x0 ADDR = 0xbfe6d3fb
Value = 0x66 ADDR = 0xbfe6d3fc
Value = 0x0 ADDR = 0xbfe6d3fd
Value = 0x9 ADDR = 0xbfe6d3fe
Value = 0x8 ADDR = 0xbfe6d3ff

2. When both peer ends are power systems, both ends are big endian, so we don't have to worry about nbo and HBO conversion.

3. When one end is Power PC and the other end is x86, the small end needs to convert before and after sending, while the large end does not.
Before sending, the small end calls htons to the short type, and CALLS htonl to the long and INT types.
After receiving the request, the small end calls ntohs to the short type and ntohl to the long and INT types.
This ensures that the value obtained by calling the struct variable is correct.

That is, the sender age = 0x00000304, and the receiver is 0x000000304. If no conversion is made, it may be 0x04030000.

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.