1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
|
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/socket.h> #include <netinet/in.h> int main (void) { char IPdotdec[20]; //存放点分十进制IP地址 struct in_addr s; // IPv4地址结构体 // 输入IP地址 printf("Please input IP address: "); scanf("%s", IPdotdec); // 转换 inet_pton(AF_INET, IPdotdec, (void *)&s); printf("inet_pton: 0x%x\n", s.s_addr); // 注意得到的字节序 // 反转换 inet_ntop(AF_INET, (void *)&s, IPdotdec, 16); printf("inet_ntop: %s\n", IPdotdec); }
/* 控制台:
Please input IP address: 192.168.0.1 inet_pton: 0x100a8c0 inet_ntop: 192.168.0.1 */
|