Ntohs, Ntohl, htons,htonl comparison and explanation

Source: Internet
Author: User
Tags htons

Ntohs =net to host short int 16 bit
Htons=host to net short int 16 bit
Ntohl =net to host long int 32 bit
Htonl=host to net long int 32 bit

NET byte order Nbo (network byte orders)

Store in order from high to low, use the same network byte order on the network to avoid compatibility problems;

Host byte order HBO (host byte order)

Different machine HBO is not the same, with the CPU design, the data order is determined by the CPU, and the operating system is independent;

such as the Intel x86 structure, the short type number 0x1234 is expressed as the 12,int type 0x12345678 expressed as 78 56 34 12;

such as the IBM Power PC structure, the short type number 0x1234 is expressed as the 34,int type number 0x12345678 represented as 12 34 56 78.

For this reason, there is no direct communication between machines of different architectures, so converting to a contract order, that is, the network byte order, is in fact the order of the Power PC. In PC development, there are ntohl and HTONL functions that can be used to convert network bytes and host bytes.

In the Linx system, the header files and functions of htonl () htons () Ntohl () Ntohs () are defined as follows:

#include <arpa/inet.h>

uint32_t htonl (uint32_t hostlong);

uint16_t htons (uint16_t hostshort);

uint32_t Ntohl (uint32_t netlong);

uint16_t Ntohs (uint16_t netshort);

Instructions for using htonl () htons () Ntohl () Ntohs () under Windows:

Ntohs ()

Briefly:

Converts an unsigned short number of shapes from network byte order to host byte order.

#include <winsock.h>
U_short PASCAL far Ntohs (U_short netshort);
Netshort: A 16-digit number expressed in network byte order.
Note: This function converts a 16-bit number from network byte order to host byte order.
return value:Ntohs () returns a number expressed in host byte order.
  

Htons ()

Description: converts the unsigned short shape number of a host into a network byte order.
#include <winsock.h>
U_short PASCAL far htons (U_short hostshort);
Hostshort: 16 Digits of the host byte order expression.
Note: This function converts a 16-digit number from host byte order to network byte order.
return value: Htons () returns the value of a network byte order.
  

These 2 functions provide a translation of host byte order and network byte order
For example, the network byte is 00 01
U_short A; How to directly correspond to the words a=0100; Why is it? Since the host is from high byte to low byte, it should be converted after A=ntohs (0001); Such a=0001;

Htonl () indicates that the 32-bit host byte order is converted to 32-bit network byte order htons () to convert 16-bit host byte order to 16-bit network byte order (IP address is 32-bit port number 16-bit)

Transform IP address to grow integer: First , assuming you already have a SOCKADDR_IN structure ina, you have an IP address "132.241.5.10" to store in which you will need to use the function inet_addr () to convert the IP address from the point format to the unsigned long integer. The method of use 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 network byte format, so you no longer have to call function htonl ().
We now find that the code snippet above is not quite complete because it has no error checking. It is obvious that inet_addr () returns 1 when an error occurs. Remember these binary numbers? (unsigned number)-1 only matches the IP address 255.255.255.255! But it's a broadcast address! So, remember to check for errors first.

How do I output a in_addr struct to a point format? You need to use the function Inet_ntoa () ("Ntoa" means "network to ASCII"), just like this: printf ("%s", Inet_ntoa (INA.SIN_ADDR)); It will output the IP address. It is important to note that Inet_ntoa () takes a struct in_addr as a parameter, not a long shape. It is also important to note that it returns a pointer to a character. It is a static fixed pointer controlled by Inet_ntoa (), so each time you call Inet_ntoa (), it overwrites the IP address that was obtained from the last call. For example:
Char *a1, *A2;
.
.
A1 = Inet_ntoa (INA1.SIN_ADDR);
A2 = Inet_ntoa (INA2.SIN_ADDR);
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.

Inet_ntoa ()
Briefly:
Convert the network address to "." The string format of the dots.
#include <winsock.h>
Char far* PASCAL far inet_ntoa (struct in_addr in);
In: A structure that represents an Internet host address.
Comments:
This function converts an Internet address structure represented by an in parameter into a string of "." intervals, such as "a.b.c.d". Note that the string returned by Inet_ntoa () is stored in the memory allocated by the Windows socket implementation. The application should not assume how the memory is allocated. The data is guaranteed to be valid before the next Windows socket interface call on the same thread.
return value:
If no error occurs, Inet_ntoa () returns a character pointer. Otherwise, return to NVLL. The data should be copied before the next Windows socket interface call.
See also: inet_addr ().


The test code is as follows
#pragma comment (lib, "Ws2_32.lib")

//noths.obj:error lnk2001:unresolved external ymbol [email protected"
#include <winsock.h>
#include <iostream.h>
#include <stdio.h>
int main (int aargc, char* argv[])
{
          struct in_addr ADDR1,ADDR2;
         unsigned long 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 \ n", Inet_ntoa (ADDR1), Inet_ntoa (ADDR2)); Notice the result of this sentence

printf ("%s \ n", Inet_ntoa (ADDR1));
printf ("%s \ n", Inet_ntoa (ADDR2));
return 0;
}
The actual operation results are as follows:
192.168.0.74:192.168.0.74//From here you can see that the Inet_ntoa in printf only ran once.
192.168.0.74
211.100.21.179
Inet_ntoa returns a char *, and the space for this char * is statically allocated within the INET_NTOA, so the call after Inet_ntoa overrides the last call. The first sentence of printf only shows that the evaluation of mutable parameters in printf is right-to-left, and that's all.

Ntohs, Ntohl, htons,htonl comparison and explanation

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.