inet_aton,inet_addr和inet_ntoa在点分十进制数串(如,“192.168.1.10")与他的32位网络字节二进制值之前转换IPV4地址,有2个比较新的函数inet_pton和inet_ntop,这2个对IPV4和IPV6地址都能处理
#include
#include
#include
int inet_aton(const char *cp, struct
in_addr *inp);
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 structure that inp points to. inet_aton()
returns non-zero if the address is valid, zero if not.
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.
inet_addr函数转换网络主机地址(如192.168.1.10)为网络字节序二进制值,如果参数char *cp无效,函数返回-1(INADDR_NONE),这个函数在处理地址为255.255.255.255时也返回-1,255.255.255.255是一个有效的地址,不过inet_addr无法处理;
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 subsequent calls will overwrite.
inet_ntoa 函数转换网络字节排序的地址为标准的ASCII以点分开的地址,,该函数返回指向点分开的字符串地址的指针,该字符串的空间为静态分配的,这意味着在第二次调用该函数时,上一次调用将会被重写(复盖),所以如果需要保存该串最后复制出来自己管理!
现在一般使用inet_aton和inet_ntoa来处理网络字节和主机字节之间的转换;
有两个更新的函数inet_pton和inet_ntop这2个函数能够处理ipv4和ipv6,原型如下
#include
#include
#include
int inet_pton(int af, const char *src, void *dst);