Skip to content

Instantly share code, notes, and snippets.

@corvofeng
Created March 29, 2018 01:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save corvofeng/f885ae287880eaf636d4fdf60d29ec0b to your computer and use it in GitHub Desktop.
Save corvofeng/f885ae287880eaf636d4fdf60d29ec0b to your computer and use it in GitHub Desktop.
最近在Linux上编程, inet_ntoa,和inet_aton之间的转换和使用
#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