Ntohs, Ntohl, htons,htonl Inet_aton and other detailed

Source: Internet
Author: User
Tags htons

Ntohs =net to host Short int16-bit htons=host to Net Short int16-bit Ntohs=net to hostLong int32-bit htonl=host to NetLong int32-bit description: Converts an unsigned short shape number from network byte order to host byte order.     #include 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. Converts the unsigned short shape number of a host into a network byte order. #includeu_short PASCAL far htons (u_short hostshort); Hostshort: 16-bit number of host byte-order representations. 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, such as network byte toxx  onu_short A; How to directly correspond to the words a=0100; why? Since the host is from high byte to low byte, it should be converted after a=ntohs (0001); So a=0001First, suppose you already have a SOCKADDR_IN structure ina, you have an IP address"132.241.5.10"to store it, you will use the function inet_addr () to convert the IP address from the point format to the unsigned long integer. Here's how to use it: ina.sin_addr.s_addr= Inet_addr ("132.241.5.10");; Margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; Line-height: the%;"> 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. Obviously, when the inet_addr () error occurs, the return-1。 Remember these binary numbers? (unsigned number)-1 only and IP address 255.255.255. 255 matches! It's a broadcast address! Wrong Remember to check for errors first. OK, now you can convert the IP address to grow the integer type. Is there a way to do the opposite? It can output a IN_ADDR structure into a point format? In this case, you will need to use the function Inet_ntoa () ("Ntoa"The implication is"Network to ASCII"), just like this: printf ("%s", Inet_ntoa (INA.SIN_ADDR)); It will output an IP address. It is important to note that Inet_ntoa () takes the 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);p rintf ("Address 1:%s", A1);p rintf ("Address 2:%s", a2); the output is as follows: Address1:132.241.5.10Address2:132.241.5.10If you need to save this IP address, use the strcopy () function to point to your own character pointer. *************************************************************************************************************** ********************htonl () indicates that the 32-bit host byte order is converted to 32-bit network byte order htons (), which translates 16-bit host byte order to 16-bit network byte order (IP address is 32-bit port number 16-bit) Inet_ntoa () Description: Convert the network address to "."     The string format of the dots. #includeCharfar* PASCAL far Inet_ntoa (structIn_addrinch); inch: A structure that represents an Internet host address. Note: This function converts an Internet address structure represented by the 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 include#include#include#includep:0px; Margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; Line-height: the%;">int Main (int aargc, char* argv[]){         structin_addr ADDR1,ADDR2; ULONGL1,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 this sentenceprintf"%s", Inet_ntoa (ADDR1)); printf ("%s", Inet_ntoa (ADDR2)); return 0;} The actual operation results are as follows:192.168.0.74:192.168.0.74       //it can be seen from here that the Inet_ntoa in printf only ran once. 192.168.0.74211.100.21.179Inet_ntoa returns a char* and this char *the space is statically allocated in Inet_ntoa, so the call after Inet_ntoa overwrites 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. Inet_aton,inet_addr and Inet_ntoa in dotted decimal string (for example, "192.168.1.10") with his 32-bit network byte binary value before converting the IPV4 address, there are 2 more new functions Inet_pton and inet_ntop, which can be handled by 2 to IPV4 and IPV6 addresses#include <sys/socket.h>#include<netinet/inch.h>#include<arpa/inet.h>intInet_aton (Const Char*CP,structIN_ADDR *INP); in_addr_t inet_addr (Const Char*CP); Char*inet_ntoa (structIn_addrinch); Inet_aton () Converts the Internet host address CP fromThe standard numbers-and-dots notation into binary data and stores itinchThe struc‐ture that InP points to. Inet_aton () returns non-zeroifThe address isvalid, ZeroifNot.inet_aton () convert network host address CP is a binary value and is stored in a struct IN_ADDR structure, which is the second parameter*INP, the function returned non 0 indicates that the CP host is valid and returns 0 to indicate that the host address is invalid. The inet_addr () function converts the Internet host address CP fromnumbers-and-dots notation into binary datainchNetworkbyteorder. If the input isInvalid, inaddr_none (usually-1) isReturned. This isAn obsoleteInterfaceTo Inet_aton (), described immediately above; It isobsolete because-1  isA valid address (255.255.255.255), and Inet_aton () provides a cleaner to indicate errorreturnThe . inet_addr function converts a network host address, such as 192.168.1.Ten) is a network byte-order binary value, and if the argument char *CP is invalid, the function returns-1(inaddr_none), this function is handled at address 255.255.255.255 also returns-1,255.255.255. 255 is a valid address, but inet_addr cannot handle it; The Inet_ntoa () function converts the Internet host addressinchGiveninchNetworkbyteOrder to astring inchStandard numbers-and-dots notation. thestring  isReturnedincha statically allocated buffer, which Subse‐quent calls will Overwrite.inet_ntoa function to convert network byte sort addresses to standard ASCII with point-separated addresses, , the function returns a pointer to a point-separated string address, which is statically allocated, which means that the last call will be overridden (overwritten) when the function is called the second time, so if it is necessary to save the string at the end of the copy it is managed! Inet_aton and Inet_ntoa are now generally used to handle conversions between network bytes and host bytes; There are two updated functions Inet_pton and inet_ntop These 2 functions can handle IPv4 and IPv6, the prototype is as follows # #<sys/types.h>#include<sys/socket.h>#include<arpa/inet.h>intInet_pton (intAfConst Char*SRC,void*DST); This function converts the string to the network address, the first parameter AF is the address family, after the conversion exists in DST Inet_pton is the inet_addr extension, the supported multi-address family has the following: Af_inet SRC is the address that points to the character type, That is, the first address of the ASCII address (in DDD.DDD.DDD.DDD format), the function converts the address to the IN_ADDR structure, and copies the*in DST, Af_inet6 SRC is the address that points to IPV6, and the function converts the address to the IN6_ADDR structure and copies the*in DST, if a function error returns a negative value and the errno is set to Eafnosupport, the function returns 0 if the address family specified by the parameter AF is not the same as the SRC format. The function inet_ntop the reverse conversion prototype as follows # include<sys/types.h>#include<sys/socket.h>#include<arpa/inet.h>Const Char*inet_ntop (intAfConst void*SRC,Char*DST, socklen_t cnt); This function converts 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 size of the buffer DST that is pointed to, avoid overflow, If the buffer is too small to store the value of the address, a null pointer is returned and the errno is set to ENOSPC

Ntohs, Ntohl, htons,htonl Inet_aton, etc. (Turn)

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.