Comparison and explanation of ntohs, ntohl, htons, and htonl

Source: Internet
Author: User
Tags htons

Ntohs = net to host short int 16-bit

Htons = host to net short int 16-bit

Ntohs = net to host long int 32-bit

Htonl = host to net long int 32-bit

Brief description:
Converts an unsigned short integer from the network byte sequence to the host byte sequence.

# Include

U_short Pascal far ntohs (u_short netshort );

Netshort: A 16-digit number expressed in bytes of the network.

Note:
This function converts a 16-digit string from the network byte sequence to the host byte sequence.

Return Value:
Ntohs () returns the number of bytes expressed in the host sequence.

Converts the unsigned short integer count of the host to the network byte sequence.

# Include

U_short Pascal far htons (u_short hostshort );

Hostshort: The 16-digit host in bytes.

Note:
This function converts a 16-digit sequence from the host byte to the network byte sequence.

Return Value:
Htons () returns the value of a network byte sequence.

These two functions provide host byte sequence and network byte sequence conversion.

For example, the Network byte is 00 01.

U_short A; how to directly correspond to a = 0100; why? Because the host is from high byte to low byte, it should be converted

A = ntohs (0001); then a = 0001;

 

 

First, assume that you already have a sockaddr_in struct Ina, and you have an IP address "132.241.5.10" to store it, you need to use the inet_addr () function (), converts an IP address from a point to an unsigned long integer. The usage is as follows:
Ina. sin_addr.s_addr = inet_addr ("132.241.5.10 ");
Note that the address returned by inet_addr () is already in the network byte format, so you do not need to call the function htonl ().
We now find that the code snippet above is not very complete because it does not have an error check. Obviously,-1 is returned when an error occurs in inet_addr. Remember these binary numbers? (No.)-1 only matches the IP address 255.255.255.255! This is the broadcast address! Very wrong! Remember to perform the error check first.
Now you can convert the IP address to a new integer. Is there an alternative? Can it output an in_addr struct to the point format? In this case, you need to use the function inet_ntoa () ("ntoa" means "network to ASCII"), like this:
Printf ("% s", inet_ntoa (INA. sin_addr ));
It outputs the IP address. Note that inet_ntoa () uses the in-ADDR struct as a parameter, not a long integer. It also returns a pointer to a character. It is a static and fixed pointer controlled by inet_ntoa (), so every time you call inet_ntoa (), it will overwrite the IP address obtained during the last call. For example:
Char * A1, * A2;
.
.
A1 = inet_ntoa (ina1.sin _ ADDR);/* This is 198.92.129.1 */
A2 = inet_ntoa (ina2.sin _ ADDR);/* This is 132.241.5.10 */
Printf ("address 1: % s", A1 );
Printf ("address 2: % s", A2 );
The output is as follows:
Address 1: 132.241.5.10
Address 2: 132.241.5.10
If you need to save this IP address, use the strcopy () function to point to your own character pointer.

**************************************** **************************************** **************

Htonl () indicates converting 32-bit host bytes to 32-bit network bytes. htons () indicates converting 16-bit host bytes to 16-bit network bytes (the IP address is 32-bit and the port number is 16-bit)

Inet_ntoa ()
Brief description:
Converts a network address to a string format separated.

# Include

Char far * Pascal far inet_ntoa (struct in_addr in );

In: An Internet host address structure.

Note:
This function converts an Internet address structure represented by the In parameter to a string separated by ".", such as "A. B. C. D. Note that the strings returned by inet_ntoa () are stored in the memory allocated by the Windows interface. The application should not assume how the memory is allocated. Before calling the next Windows interface of the same thread, the data will be valid.

Return Value:
If no error occurs, inet_ntoa () returns a character pointer. Otherwise, nvll is returned. The data should be copied before the next Windows interface call.

See:
Inet_addr ().

The test code is as follows:
Include
# Include
# Include
# Include
# Include
Int main (INT primary GC, char * argv [])
{
Struct in_addr addr1, addr2;
Ulong L1, L2;
L1 = inet_addr ("192.168.0.74 ");
L2 = inet_addr ("211.100.21.179 ");
Memcpy (& addr1, & L1, 4 );
Memcpy (& addr2, & L2, 4 );

Printf ("% s: % s", inet_ntoa (addr1), inet_ntoa (addr2); // pay attention to the running result of this sentence

Printf ("% s", inet_ntoa (addr1 ));
Printf ("% s", inet_ntoa (addr2 ));
Return 0;
}
The actual running result is as follows:
192.168.0.74: 192.168.0.74 // You Can See That inet_ntoa in printf only runs once.
192.168.0.74
211.100.21.179

Inet_ntoa returns a char *, and the space of this char * Is statically allocated in inet_ntoa. Therefore, the call after inet_ntoa will overwrite the previous call. The result of the first printf statement can only indicate that the variable parameters in printf are evaluated from right to left.

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.