Htons (), ntohl (), ntohs (), htons () function tons (), ntohl (), ntohs (), htons () function

Source: Internet
Author: User
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.

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.