C++中IPv4字符串与u_int32_t的转换
inet_pton函数简介
IP地址转换函数,可以在将IP地址在“点分十进制”和“二进制整数”之间转换
需要引用的头文件:
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
函数形式:
int inet_pton(int af, const char *src, void *dst);
- af确定转换类型,取值为AF_INET时,转换iPv4地址;取值为AF_INET6时,转换IPv6地址
- src为想要转换的地址
- dst为转换后地址的存储结构,根据af的取值决定为struct in_addr或struct in6_addr
struct in_addr简介:
in_addr是一个结构体,可以用来表示一个32位的IPv4地址
#include <arpa/inet.h>
struct in_addr {