Network byte conversion inet_aton & inet_ntoa & inet_addr and inet_pton & inet_ntop

Source: Internet
Author: User

Reprinted please indicate Source: http://hi.baidu.com/phps

Inet_aton, inet_addr, and inet_ntoa convert the IPv4 address before the binary value of the 32-bit network bytes in the dotted decimal number string (for example, "192.168.1.10, there are two relatively new functions inet_ton and inet_ntop, both of which can handle both IPv4 and IPv6 addresses.
# Include <sys/socket. h>
# Include <netinet/in. h>
# Include <ARPA/inet. h>

Int inet_aton (const char * CP, struct in_addr * indium );

In_addr_t inet_addr (const char * CP );

Char * inet_ntoa (struct in_addr in );

Inet_aton () converts the Internet host address CP from the standard
Numbers-and-dots notation into binary data and stores it in the struc ‐
Ture that indium points to. inet_aton () returns non-zero if the address is
Valid, zero if not.

Inet_aton () converts the network host address CP to a binary value, and stores it in the struct in_addr structure, that is, the second parameter * indium. If the function returns a non-0 value, the CP host is effectively effective, if the return value is 0, the host address is invalid.

the inet_addr () function converts the Internet host address CP from
numbers-and-dots notation into binary data in network byte order. if
the input is invalid, inaddr_none (usually-1) is returned. this is an
obsolete interface to inet_aton (), described immediately above; it is
obsolete because-1 is a valid address (255.255.255.255 ), and
inet_aton () provides a cleaner way to indicate error return.
the inet_addr function converts the network host address (for example, 192.168.1.10) to the network byte binary value. If the char parameter
* CP is invalid, the function returns-1 (inaddr_none ), when the processing address is 255.255.255.255.255, this function also returns
-1, 255.255.255.255.255 is a valid address, but inet_addr cannot be processed.

The inet_ntoa () function converts the Internet host address in given in
Network byte order to a string in standard numbers-and-dots notation.
The string is returned in a statically allocated buffer, which subse ‐
Quent callwill overwrite.
The inet_ntoa function converts the network byte sorting address to the standard ASCII address separated by vertices. This function returns a pointer to the string address separated by vertices. the space of the string is statically allocated, this means that when this function is called for the second time, the last call will be overwritten (overwritten). Therefore, if you need to save the string and finally copy it for management!

Currently, inet_aton and inet_ntoa are generally used to process the conversion between network bytes and host bytes;

There are two updated functions, inet_ton and inet_ntop, which can process IPv4 and IPv6. The prototype is as follows:
# Include <sys/types. h>
# Include <sys/socket. h>
# Include <ARPA/inet. h>
Int inet_ton (int af, const char * SRC, void * DST );

This function is used to convert the string to the network address. The first parameter AF is the address family, which exists in DST after conversion.
Inet_ton is an extension of inet_addr. The following multi-address families are supported:

Af_inet
SRC is the first address of the ASCII address (in the format of DDD. DDD. DDD ).
Convert to the struct of in_addr and copy it to * DST.

Af_inet6
SRC is the IPv6 address.
Convert the struct to in6_addr and copy it to * DST.
If a function error occurs, a negative value is returned and errno is set to eafnosupport. If the address family specified by the AF parameter and SRC format are incorrect, the function returns 0.

The inverse conversion prototype of the function inet_ntop is as follows:
# 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 is used to convert the binary structure of the network to an ASCII address. The parameter serves the same purpose as the preceding one. It only adds the socklen_t CNT parameter, which indicates the DST size of the cache to avoid overflow, if the cache is too small to store the address value, a null pointer is returned and errno is set to enospc.

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.