使用gethostbyname函数,然后通过inet_ntoa得到ip地址,inet_ntoa函数功能将一个IP转换成一个互联网标准点分格式的字符串;
或者使用gethostbyname_r函数;
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
int main(int argc, char *argv[])
{
struct hostent *h;
char hostname[40];
printf("请输入域名/n");
scanf("%s",hostname);
getchar();
if((h=gethostbyname(hostname))==NULL)
{
fprintf(stderr,"不能得到IP/n");
exit(1);
}
printf("HostName :%s/n",h->h_name);
printf("IP Address :%s/n",inet_ntoa(*((struct in_addr *)h->h_addr)));
return EXIT_SUCCESS;
}
//
使用gethostbyname_r 注意的地方,如果name是xxx.xxx.xxx.xxx,这样的格式,返回值为0,不一定代表转换的result正确;当name本身不是一个合格的ip地址后,根本没有调用dns,也就是说我们用的时候,如果name是ip地址,那么必须保证它是正确的,在判断返回为0而且result!=NULL才表示函数调用成功!
关于gethostbyname_r的返回值
man手册是这么说的
/* GNU extensions */
struct hostent *gethostbyname2(const char *name, int af);
int gethostbyname_r (const char *name,
struct hostent *ret, char *buf, size_t buflen,
struct hostent **result, int *h_errnop);
int gethostbyname2_r (const char *name, int af,
struct hostent *ret, char *buf, size_t buflen,
struct hostent **result, int *h_errnop);
......
The functions gethostbyname() and gethostbyaddr() may return pointers
to static data, which may be overwritten by later calls. Copying the
struct hostent does not suffice, since it contains pointers - a deep
copy is required.
Glibc2 also has a gethostbyname2() that works like gethostbyname(),
but permits to specify the address family to which the address must
belong.
Glibc2 also has reentrant versions gethostbyname_r() and gethostby-
name2_r(). These return 0 on success and nonzero on error. The result
of the call is now stored in the struct with address ret. After the
call, *result will be NULL on error or point to the result on success.
Auxiliary data is stored in the buffer buf of length buflen. (If the
buffer is too small, these functions will return ERANGE.) No global
variable h_errno is modified, but the address of a variable in which
to store error numbers is passed in h_errnop.
按照man的说法
gethostbyname_r()调用成功时返回0
*result指向解析成功的数据结构, *result如果为NULL则表示解析出错
man手册是这么说的
/* GNU extensions */
struct hostent *gethostbyname2(const char *name, int af);
int gethostbyname_r (const char *name,
struct hostent *ret, char *buf, size_t buflen,
struct hostent **result, int *h_errnop);
int gethostbyname2_r (const char *name, int af,
struct hostent *ret, char *buf, size_t buflen,
struct hostent **result, int *h_errnop);
......
The functions gethostbyname() and gethostbyaddr() may return pointers
to static data, which may be overwritten by later calls. Copying the
struct hostent does not suffice, since it contains pointers - a deep
copy is required.
Glibc2 also has a gethostbyname2() that works like gethostbyname(),
but permits to specify the address family to which the address must
belong.
Glibc2 also has reentrant versions gethostbyname_r() and gethostby-
name2_r(). These return 0 on success and nonzero on error. The result
of the call is now stored in the struct with address ret. After the
call, *result will be NULL on error or point to the result on success.
Auxiliary data is stored in the buffer buf of length buflen. (If the
buffer is too small, these functions will return ERANGE.) No global
variable h_errno is modified, but the address of a variable in which
to store error numbers is passed in h_errnop.
按照man的说法
gethostbyname_r()调用成功时返回0
*result指向解析成功的数据结构, *result如果为NULL则表示解析出错
如果被解析的字符串是类似"xxx.xxx.xxx.xxx"的 数字+点 字符串
那么gethostbyname_r并没有发出DNS请求
而是直接去计算了
192.1468.80.38显然不是合法的IP地址, 但gethostbyname_r()返回0
此时*result是NULL
那么gethostbyname_r并没有发出DNS请求
而是直接去计算了
192.1468.80.38显然不是合法的IP地址, 但gethostbyname_r()返回0
此时*result是NULL
转一篇文章: