IP地址转换函数有两组,它们可以在字符串和网络字节序的二进制值之间转换IP地址
适用于IPv4地址的函数
#include <arpa/inet.h> /* Convert Internet host address from numbers-and-dots notation in CP into binary data and store the result in the structure INP. */ int inet_aton (const char *__cp, struct in_addr *__inp); /* Convert Internet host address from numbers-and-dots notation in CP into binary data in network byte order. */ in_addr_t inet_addr (const char *__cp); /* Convert Internet number in IN to ASCII representation. The return value is a pointer to an internal array containing the string. */ char *inet_ntoa (struct in_addr __in);
以上三个函数在点分十进制数串(如“127.0.0.1")和32位网络字节序二进制值之间转换IPv4地址。
inet_aton将__cp指向的字符串转成网络序的地址存在__inp指向的地址结构。成功返回1,否则返回0。(据书中所说,如果__inp指针为空,那么该函数仍然对输入字符串进行有效性检查但是不存储任何结果)
inet_addr功能和inet_aton类似,但是inet_addr出错时返回INADDR_NONE常值(通常是32位均为1的值),这就意味着至少有一个IPv4的地址(通常为广播地址255.255.255.255)不能由该函数处理。建议使用inet_aton代替inet_addr。
inet_ntoa将网络序二进制IPv4地址转换成点分十进制数串。该函数的返回值所指向的字符串驻留在静态内存中。这意味着该函数是不可重入的。同时我们也该注意到该函数以一个结构体为参数而不是常见的以一个结构体指针作为参数。
对IPv4地址和IPv6地址同时都适用的函数
#include <arpa/inet.h> /* Convert from presentation format of an Internet number in buffer starting at CP to the binary network format and store result for interface type AF in buffer starting at BUF. */ int inet_pton (int __af, const char *__restrict __cp, void *__restrict __buf); /* Convert a Internet address in binary network format for interface type AF in buffer starting at CP to presentation form and place result in buffer of length LEN astarting at BUF. */ const char *inet_ntop (int __af, const void *__restrict __cp, char *__restrict __buf, socklen_t __len);
这两个函数的__af参数既可以是AF_INET也可以是AF_INET6。如果以不支持的地址族作为参数就会返回一个错误,并将errno置为EAFNOSUPPORT。
inet_pton将字串转成对应的网络序二进制值,inet_ntop做相反的事情,其中__len参数指定目标缓冲区的大小,在8f7320c50073ea7d8f7ba47e57577b49头文件中定义了
1 #define INET_ADDRSTRLEN 16 /* for IPv4 dotted-decimal */ 2 #define INET6_ADDRSTRLEN 46 /* for IPv6 hex string */
如果给的__len太小那么会返回一个空指针,并置errno为ENOSPC。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment