The conversion process of host byte order and network byte order

Source: Internet
Author: User
Tags port number htons

When assigning a value to the IP address structure, the function htonl,htons,inet_addr is often used , and the function corresponding to the sockaddr_in is ntohl,ntohs,inet_ntoa.

Looking at the parsing of these functions, it is found that these functions are actually related to the conversion between the host byte order and the network byte order .

What is the network byte order, and what is the host byte order?

Here's a conversion between them:

Use IP address 127.0.0.1 as an example:


First step 127.         0.        0. 1 convert each part of the IP address to a 8-bit binary number.


Step two 01111111 00000000 00000000 00000001 = 2130706433 (host byte order)


Then you rearrange the four-part binary numbers above from right to left, which becomes:


Step three 00000001 00000000 00000000 01111111 = 16777343 (network byte order)

Then parsing the function mentioned above is much simpler, look at the following code:

Sockaddr_in addrsrv;

Addrsrv.sin_addr. S_un. S_addr=htonl (2130706433);

Addrsrv.sin_family=af_inet;

Addrsrv.sin_port=htons (6000);

First defines an IP address struct addrsrv, and then initializes its IP

Addrsrv.sin_addr. S_un. S_ADDR must be the network byte order of the IP address assigned,

The role of the HTONL function is to convert a host byte order to a network byte order,

That is, the second step in the conversion process to the third step of the role,

The host byte order of the 127.0.0.1 is 2130706433,

Converting host byte order 2130706433 to network byte order is htonl (2130706433) = 16777343,

So if you know that the network byte order is 16777343,

Addrsrv.sin_addr. S_un. S_addr=htonl (2130706433); and addrsrv.sin_addr. S_un. s_addr=16777343;

is exactly the same.

Addrsrv.sin_addr. S_un. S_addr=htonl (2130706433); This sentence can also be written as:

Addrsrv.sin_addr. S_un. S_ADDR=INET_ADDR ("127.0.0.1"); The result is exactly the same.

The transformation of the visible inet_addr function is the transition from the first step to the third step above.

The following is the conversion of the host byte order of the port to the network byte order.

Take Port 6000 as an example.

First step 00010111 01110000 = 6000 (host byte order)

The port number is actually the host byte order, first of all to write the port number as a 16-bit binary number, the first 8 bits and 8 bits.

Step two 01110000 00010111 = 28695 (network byte order)

Then, the first eight bits of the host byte order and the last eight positions are replaced by a new 16-bit binary number, the new 16-bit binary is the binary representation of the network byte order.

So, if you know that the network byte order of Port 6000 is 28695.


Addrsrv.sin_port=htons (6000); can be written directly as addrsrv.sin_port=28695; The result is the same,

The function of htons is to convert the port number host byte order to the network byte order.

and htonl,htons,inet_addr, the corresponding function is Ntohl,ntohs,inet_ntoa,

It is not hard to see, Ntohl,ntohs,inet_ntoa, that these three functions are actually performing opposite conversions to their counterpart functions,

This is not a detailed analysis here.

Article source http://www.cnblogs.com/fora/archive/2012/09/26/2704035.html

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.