1.利用域名获取IP地址
使用以下函数可以通过传递字符串格式的域名获取IP地址。
#include <netdb.h>
struct hostent * gethostbyname(const char * hostname); (成功返回 hostent结构体地址,失败时返回NULL指针)。
只要传递域名字符串,就会返回域名对应的IP地址。只是返回时,地址信息装入hostent结构体。此结构体如下。
struct hostent
{
char * h_name; //official name
char ** h_aliases; //alias list
int h_addrtype; //host address type
int h_length; //address length
char **h_addr_list; //address list
}
从上述结构体定义可以看出,不只返回IP信息,同时还连带着其他信息。各位不用想得太过复杂。域名转IP时只需