Created
March 29, 2018 01:43
-
-
Save corvofeng/f885ae287880eaf636d4fdf60d29ec0b to your computer and use it in GitHub Desktop.
最近在Linux上编程, inet_ntoa,和inet_aton之间的转换和使用
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <netinet/in.h> | |
#include <netinet/ip.h> | |
#include <string.h> | |
#include <arpa/inet.h> // 如果不添加这个头文件, inet_ntoa获取的字符串也无法读取 | |
int main(int argc, char *argv[]) | |
{ | |
struct in_addr ip_src; // 32bits 源IP地址 | |
ip_src.s_addr = 0; | |
int ret = inet_aton("", &ip_src); | |
printf("%d\n", ret); | |
char* after_parse = inet_ntoa( (struct in_addr) ip_src); | |
int len = strlen(after_parse); | |
if(after_parse != NULL) | |
printf("%s\n", after_parse); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment