Tons (), ntohl (), ntohs (), htons () function
Posted on heaad reading (10158) Comments (1) EDIT favorites
When writing a network program in C/C ++, the network sequence and host sequence of bytes are often encountered. You may use the htons (), ntohl (), ntohs (), and htons () functions.
The conversion function between the network byte sequence and the local byte sequence:
Htonl () -- "host to network long"
Ntohl () -- "network to host long"
Htons () -- "host to network short"
Ntohs () -- "network to host short"
These functions are required because the computer data indicates that there are two byte sequences: nbo and HBO.
Network byte order ):
It stores data in a sequence from high to low and uses a unified network byte sequence on the network to avoid compatibility issues.
Host byte order (HBO, host byte order ):
The HBO of different machines is different and related to the CPU design. The data sequence is determined by the CPU, but not the operating system.
For example, in an intel X86 architecture, the short type 0x1234 indicates 34 12, and the int type 0x12345678 indicates 78 56 34 12.
For example, in the IBM Power PC architecture, the short number 0x1234 indicates 12 34, and the int number 0x12345678 indicates 12 34 56 78.
For this reason, machines of different architectures cannot communicate with each other. Therefore, to convert the data into a agreed number order, that is, the network byte order, is actually the same as the order of Power PC. In PC development, ntohl and htonl functions can be used to convert network bytes and host bytes.
When writing a network program in C/C ++, the network sequence and host sequence of bytes are often encountered. You may use the htons (), ntohl (), ntohs (), and htons () functions.
The conversion function between the network byte sequence and the local byte sequence:
Htonl () -- "host to network long"
Ntohl () -- "network to host long"
Htons () -- "host to network short"
Ntohs () -- "network to host short"
These functions are required because the computer data indicates that there are two byte sequences: nbo and HBO.
Network byte order ):
It stores data in a sequence from high to low and uses a unified network byte sequence on the network to avoid compatibility issues.
Host byte order (HBO, host byte order ):
The HBO of different machines is different and related to the CPU design. The data sequence is determined by the CPU, but not the operating system.
For example, in an intel X86 architecture, the short type 0x1234 indicates 34 12, and the int type 0x12345678 indicates 78 56 34 12.
For example, in the IBM Power PC architecture, the short number 0x1234 indicates 12 34, and the int number 0x12345678 indicates 12 34 56 78.
For this reason, machines of different architectures cannot communicate with each other. Therefore, to convert the data into a agreed number order, that is, the network byte order, is actually the same as the order of Power PC. In PC development, ntohl and htonl functions can be used to convert network bytes and host bytes.