"Network Programming" inet_addr, Inet_ntoa, Inet_aton, Inet_ntop, and Inet_pton distinctions

Source: Internet
Author: User

First, a picture.

1. Convert the IP address to a binary value for network transmission

int Inet_aton (const char *CP, struct in_addr *inp);

Inet_aton () Converts a network host address IP (such as 192.168.1.10) to a binary value, stored in a struct IN_ADDR structure, that is, the second parameter *INP, the function returns a non-0 indicating that the CP host is valid, and returning 0 indicates that the host address is invalid. (This cannot be used for network transport after this conversion, and you need to call the Htons or htonl function to convert host byte order to network byte order)

in_addr_t inet_addr (const char *CP);

The INET_ADDR function converts a network host address (such as 192.168.1.10) to a network byte-order binary value, and if the argument char *CP is invalid, the function returns-1 (Inaddr_none), which returns 1 when the address is 255.255.255.255. , 255.255.255.255 is a valid address, but inet_addr cannot handle it;


2. Convert the binary value of the network transmission into a dot-decimal IP address

Char *inet_ntoa (struct in_addr in);

The Inet_ntoa function converts the address of the network byte sort to a point-separated address of the standard ASCII, which returns a pointer to a point-separated string address (such as 192.168.1.10), which is statically allocated, meaning that when the function is called for the second time, The last call will be rewritten (overwritten), so if you need to save the string finally copy it out of your own management!

How do we output a dot-decimal IP? Let's take a look at the following procedure:

#include <stdio.h>#include<sys/socket.h>#include<netinet/inch.h>#include<arpa/inet.h>#include<string.h>intMain () {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\n", Inet_ntoa (ADDR1), Inet_ntoa (ADDR2));//Notice the result of this sentenceprintf"%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.

3. New network address conversion function Inet_pton and Inet_ntop
These functions are functions that occur with IPV6, and are applicable for both IPV4 and IPV6 addresses, where P and n represent representations (presentation) and values (numeric), respectively. The expression format of an address is usually an ASCII string, and the numeric format is a binary value that is stored in the socket address structure.

 #include <arpe/inet.h>int  Inet_pton ( int  family, char  *strptr, //  Converts a dotted-decimal IP address into a numeric format for network transmission 
// return value: 1 If successful, if the input is not a valid expression is 0, if the error is-1 const char * inet_ntop (int family, " Span style= "color: #0000ff;" >const void *addrptr, char * StrPtr, size_t len);
// convert numeric format to dotted decimal IP address format // Return value: A pointer to a struct if successful, or null if an error occurs

(1) The family parameters of these two functions can be either af_inet (IPv4) or Af_inet6 (IPv6). If, with an unsupported address family as the family parameter, both functions return an error and the errno is set to Eafnosupport.

(2) The first function attempts to convert the string pointed to by the StrPtr pointer and holds the binary result through the addrptr pointer, or 1 if successful, otherwise the return value is 0 if the input string is not a valid expression format for the specified family.

(3) Inet_ntop to convert from a numeric format (addrptr) to an expression (strptr). The StrPtr parameter of the Inet_ntop function cannot be a null pointer. The caller must allocate memory for the target storage unit and specify its size, which is the return value of the function when the call succeeds. The Len parameter is the size of the destination storage unit so that the function overflows its caller's buffer. If Len is too small to hold the expression result, return a null pointer and set errno to ENOSPC.

Inet_pton (Af_inet, IP, &foo.sin_addr);   
// instead of FOO.SIN_ADDR.S_ADDR=INET_ADDR (IP); Char Str[inet_addrstrlen]; Char sizeof (str));
// instead of ptr = Inet_ntoa (foo.sin_addr)

"Network Programming" inet_addr, Inet_ntoa, Inet_aton, Inet_ntop, and Inet_pton distinctions

Related Article

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.