The host byte sequence is what we usually call the big and small-end mode: different CPUs have different byte sequence types. These byte sequences refer to the order in which integers are stored in the memory. This is called the host sequence. Big-Endian and Little-Endian. The definitions of reference standard Big-Endian and Little-Endian are as follows:
A) Little-Endian is the low-byte emission at the low-address end of the memory, and the high-byte emission at the High-address end of the memory.
B) Big-Endian refers to the low address of the memory where the high byte is discharged, and the low byte is discharged to the high address of the memory.
Network byte order: the 32-bit values of four bytes are transmitted in the following order: the first is 0 ~ 7 bit, followed by 8 ~ 15bit, then 16 ~ 23bit, last 24 ~ 31bit. This transmission order is called the Large-end byte order. Because all the binary integers in the TCP/IP Header must be transmitted in this order, it is also called the network byte order. The byte order, as the name implies, is the order in which data of the same byte type is stored in the memory. The data of a single byte is not sequential.
The following four conversion functions are provided for bsd socket conversion:
Htons converts the unsigned short type from host to Network
Htonl converts the unsigned long type from the host sequence to the network Sequence
Ntohs converts the unsigned short type from the network sequence to the host Sequence
Ntohl converts the unsigned long type from the network sequence to the host Sequence
In systems using little endian, these functions convert the byte order.
In systems using the big endian type, these functions are defined as empty macros.
Note:
1. network and host byte Conversion Function: htons ntohs htonl ntohl (s means that short l is long h is host n is network)
2. different operating systems run on different CPUs, And the byte order is also different.