TCP / IP Network Programming byte order and network byte order

I. Summary

This article based mainly on the network byte order .net tcp / ip communication network programming. At the same time record the contents in the process of self-improvement, it is convenient for you to remember also hope to help everyone. Advances in technology stems from sharing and constant self-breakthrough.

Technical exchange QQ group: 580 749 909 welcome to exchange Insider, the end of the article have a personal micro-channel public number of small partners interested a lot of attention.

Second, Introduction

Prior to this we need a clear understanding of concepts.

CPU architecture : often said x86 x64 is an architecture, but he has a 32-bit and 64-bit. 32 called x86, later appeared on its 64-bit version, called x64.

Median OS  : 64-bit system is based on 64-bit processors come, so 64-bit systems can run on 64-bit processors, but can not run on 32 processors!

And because 64-bit processor is based on 32-bit X86 comes, so backward compatibility, support for 32-bit systems!

details as following:

64-bit processor (X64): 64-bit system, 32-bit system

32-bit processor (X86): 32-bit system. 64-bit 16-bit system processor may also run.

After only into long mode, no longer compatible with 16-bit instruction that is. If not long after power feeding mode, or the 16-bit instructions can run.

What is the byte order and network byte order?

Network byte order  is TCP / IP represented in a predetermined format to a data well, with the CPU type, operating system independent, and thus can ensure that data can be properly interpreted in the transmission between the different hosts.

Host byte sequence , i.e. the sequence when the sequence of input (output) of the byte is stored in the computer is first come to the front or rear of the front.

 As long as Intel or AMD x86 / x64 architecture is necessarily little-endian byte order host.

 

Third, the main content

Why is the concept of "byte order", because there are different CPU architectures so save the data are different ways for each CPU.

There are two ways to the CPU memory for storing data, corresponding to the resolution mode has two kinds:

Big endian (Big Endian): high byte is stored in the lower address.

Little-endian (Little Endian): high byte is stored in the upper address.

Integer 0x12345678, 0x12 is the most significant byte, 0x78 is the least significant byte. Thus the program first save big-endian byte 0x12 MSB (most significant byte stored in low address 0x12)

First save of the least significant byte is 0x78. From the above analysis, the data is stored in different ways for each CPU. Thus CPU data representative of data stored in the endian mode of the host CPU are different, also different.

Big endian system considering data transfer byte order, which directly transmitted in the order of 0x1234 0x12,0x34 time. Result receiving side data stored in the small end, and therefore the little-endian data received programmed 0x3412.

Such little-endian transmission system is not impossible to use it? This time we agreed to a unified approach, unified according to the big-endian to deal with it. .Net how to deal with it have a special function to deal with this problem.

 

Guess you like

Origin www.cnblogs.com/justzhuzhu/p/12129203.html