Ntohs, Ntohl, htons,htonl comparison and detailed explanation

Source: Internet
Author: User
Tags function definition string format htons
When you write a network program in C + +, you will often encounter the problem of byte network order and host order. It is possible to use htons (), Ntohl (), Ntohs (), htons () the conversion function between the 4 network byte order and the local byte order:

These functions are required because the computer data represents two byte orders: Nbo and HBO.

Network byte sequence Nbo (network byte order):
You can avoid compatibility problems by storing them in order from highest to lowest, using a uniform network byte order on the network.

Host byte sequence (hbo,host byte order):
Different machine HBO is not the same, and CPU design, the order of data is determined by the CPU, regardless of the operating system.
In the case of the Intel x86 structure, the short number 0x1234 is expressed as the number, and the int type 0x12345678 is 78 56 34 12.
In the case of the IBM Power PC structure, the short type 0x1234 is expressed as a number, and the int 0x12345678 is expressed as 12 34 56 78.

Because of this reason different architectures are unable to communicate between machines, so converting to a convention sequence, 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.


Under Linux system: Htonl (), htons (), Ntohl (), Ntohs () header file and function definition:
#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);
under Windows system : Htonl (), htons (), Ntohl (), Ntohs (), inet_addr () Use instructions ntohs ()

Briefly:
Converts an unsigned short integer number 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.
Comments:
This function converts a 16-digit number from network byte order to host byte order.
return value: Ntohs () returns a number expressed in host byte order.

Ntohl ()

Briefly:
Converts a number of unsigned long reshaping from network byte order to host byte order.
#include <winsock.h>
U_long PASCAL FAR Ntohl (U_long netlong);
Netlong: A 32-digit number expressed in network byte order.
Comments:
This function converts a 32-digit number from network byte order to host byte order.
return value:
Ntohl () Returns a number expressed in host byte order.

Htons ()
Briefly:
Converts the unsigned short integer number of hosts to network byte order. Converts an unsigned short-integer host byte sequence to a network byte order
#include <winsock.h>
U_short PASCAL FAR htons (u_short hostshort);
Hostshort: 16 Digits of host byte order expression.
Comments:
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.
To put it simply, htons () is the interchange of a number in a high or low position
(such as:--> 34 12)
vb says:
MsgBox Hex (htons (&h1234))
Display value of 3412

Htonl ()
Briefly:
Converts the unsigned long integer number of hosts to network byte order. Converts an unsigned long-integer network byte order to host byte order
#include <winsock.h>
U_long PASCAL FAR htonl (U_long hostlong);
Hostlong: 32 Digits of host byte order expression.
Comments:
This function converts a 32-digit number from host byte order to network byte order.
return value:
HTONL () returns the value of a network byte order.

INET_ADDR ()

       Brief:
       converts a point-interval address into a in_addr.  
#include <winsock.h>
unsigned long PASCAL FAR inet_addr (const struct far* cp);
CP: An Internet standard "." The string of intervals. For example 202.38.214.xx
       when IP address is 255.255.255.255 is considered invalid IP address. The
       This function interprets the string in the CP argument, which uses the Internet's "." The interval format represents the Internet address of a number.
    return value:
    An unsigned long integer number that can be used as an Internet address. All Internet addresses are returned in network byte order (bytes are arranged from left to right).

Inet_ntoa ()
Briefly:
Converts the network address to "." The dotted string format.
#include <winsock.h>
Char far* PASCAL FAR inet_ntoa (struct in_addr in);
In: A structure that represents the address of an Internet host.
Comments:
This function converts an Internet address structure represented by the in parameter to a string form such as "a.b.c.d" with the "." Interval. Note that the string returned by Inet_ntoa () is stored in the memory allocated by the Windows sleeve interface implementation. The application should not assume that the memory is allocated. Data is guaranteed to be valid until the next Windows socket interface is invoked on the same thread.
When the IP address is 255.255.255.255 is considered a valid IP address. This is the difference from the inet_addr ()
return value:
If no error occurs, Inet_ntoa () returns a character pointer. Otherwise, NULL is returned. The data should be copied before the next Windows socket interface is invoked.

Inet_aton ()
Contrary to the Inet_ntoa () effect.

Inet_pton ()
    Brief:
    This function converts dotted decimal to integer
      # Include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
int Inet_pton (int AF, const char *src, void *DST);
This function converts a string to a network address, the first parameter AF is the address family, the transition exists in DST
    Inet_pton is an extension of the inet_addr, and the supported multiple address families have the following:
AF = af_inet
Src is a character-oriented address, that is, the first address of an ASCII address (ddd.ddd.ddd.ddd format), the function converts that address
to a IN_ADDR structure, and copies the
AF =af_inet6
src in *dst to the point To the IPV6 address, the function converts the address to a IN6_ADDR structure and copies it in *DST
If a function error returns a negative value and the errno is set to Eafnosupport, the function returns 0 if the specified address family and SRC format are not correct for the parameter AF.
       #include <sys/types.h>
#include <sys/socket.h>
#   Include <arpa/inet.h>
const char *inet_ntop (int af, const void *SRC, char *dst, socklen_t cnt);
This function transforms the network binary structure to the ASCII type address, the function of the parameter is the same as above, just one more parameter socklen_t cnt, he is the

Point to the size of the buffer DST, avoid overflow, and if the buffer is too small to store the value of the address, return a null pointer and errno to ENOSPC

Atoi ()

Array to integer converts a string to an integer number



First, suppose you already have a SOCKADDR_IN structure ina, you have an IP address "132.241.5.10" to store in it, you need to use the function inet_addr (), the IP address from the number format to the unsigned long integer type. The
uses the following method:
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 do not need to call the function htonl ( )。
We now find that the code fragment above is not very complete because it has no error checking. Obviously, when inet_addr () has an error, return-1. Remember these binary digits. (unsigned number)-1 only matches the IP address 255.255.255.255. It's a broadcast address. Wrong Remember to check for errors first.
OK, now you can convert the IP address to an integral type. There is no other way to do it. It can output a IN_ADDR structure to a number of points format. In this case, 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 should be noted that Inet_ntoa () IN-ADDR the structure as an argument, not a long shaping. The same thing to note is that it returns a pointer to a character. It is a static, fixed pointer controlled by Inet_ntoa (), so each time the Inet_ntoa () is invoked, it overrides the IP address that was obtained at the time of 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
Adress 2:132.241.5.10
If you need to save this IP, use the strcopy () function to point to your own character pointer.

*************************************************************************************************************** The test code is as follows

#include
#include
#include
#include
#include
int main (int argc, 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 (&AMP;ADDR1, &AMP;L1, 4);
memcpy (&AMP;ADDR2, &AMP;L2, 4);

printf ("%s:%s", Inet_ntoa (ADDR1), Inet_ntoa (ADDR2)); Notice the result of the operation of this sentence

printf ("%s", Inet_ntoa (ADDR1));
printf ("%s", Inet_ntoa (ADDR2));
return 0;
}
The actual operating results are as follows:
192.168.0.74:192.168.0.74//From here you can see that the Inet_ntoa in printf runs only once.
192.168.0.74
211.100.21.179

Inet_ntoa returns a char *, and the space of this char * is statically allocated within the INET_NTOA, so the call after Inet_ntoa overwrites the last call. The result of the first printf can only show that the variable parameters in printf are evaluated from right to left, and that's it.

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.