您的位置:首页 > 运维架构 > Linux

linux C:将域名转化为IP地址

2013-11-18 15:54 260 查看
如题,这个功能挺常用的,记录一下方便以后使用

#include <stdio.h>
#include <string.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdlib.h>

/*
作用:将网络地址转化为IP
参数:ipbuf是输出缓冲区, host是要转化的域名, maxlen是缓冲区大小
返回值:返回-1是失败,0是成功
*/
int get_ip_from_host(char *ipbuf, const char *host, int maxlen)
{
struct sockaddr_in sa;
sa.sin_family = AF_INET;
if (inet_aton(host, &sa.sin_addr) == 0)
{
struct hostent *he;
he = gethostbyname(host);
if (he == NULL)
return -1;
memcpy(&sa.sin_addr, he->h_addr, sizeof(struct in_addr));
}
strncpy(ipbuf, inet_ntoa(sa.sin_addr), maxlen);
return 0;
}

//测试例子
int main()
{
char ipbuf[128];
get_ip_from_host(ipbuf, "www.baidu.com", 128);
printf("ip: %s\n", ipbuf);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: