- 使用gethostbyname,返回值的结构如下:
struct hostent
{
char *h_name;
char **h_aliases;
int h_addrtype;
int h_length;
char **h_addr_list;
#ifdef __USE_MISC
# define h_addr h_addr_list[0]
#endif
};
#include// gethostbyname #include #include #include #include #include #include #include using namespace std; int main() { struct hostent* phost = gethostbyname("www.baidu.com"); char* ip; if (NULL == phost) { cout << "gethostbyname error : " << errno << " : " << strerror(errno) << endl; return -1; } //inet_ntop(phost->h_addrtype, phost->h_addr, ip, 17); for(int i=0; phost->h_addr_list[i]; i++) { ip = inet_ntoa( *(struct in_addr*)phost->h_addr_list[0] ); cout << ip << endl; } return 0; }



