LINUX平台
auto host = gethostbyname("www.bdjw.work");
if (host) {
std::cout << host->h_name << endl;
std::cout << host->h_length << endl;
if (AF_INET == host->h_addrtype)
std::cout << "AF_INET" << endl;
else
std::cout << host->h_addrtype << endl;
for (size_t i = 0; host->h_aliases[i]; i++)
std::cout << host->h_aliases[i] << endl;
for (size_t i = 0; host->h_addr_list[i]; i++)
std::cout << inet_ntoa(*(in_addr*)host->h_addr_list[i]) << endl;
}
sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_addr.s_addr = inet_addr("101.43.87.248");
host = gethostbyaddr((char*)&addr.sin_addr, 4, AF_INET);
if (host) {
std::cout << host->h_name << endl;
std::cout << host->h_length << endl;
if (AF_INET == host->h_addrtype)
std::cout << "AF_INET" << endl;
else
std::cout << host->h_addrtype << endl;
for (size_t i = 0; host->h_aliases[i]; i++)
std::cout << host->h_aliases[i] << endl;
for (size_t i = 0; host->h_addr_list[i]; i++)
std::cout << inet_ntoa(*(in_addr*)host->h_addr_list[i]) << endl;
}
Windows平台
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
cout << "WSAStartup() error" << endl;
auto host = gethostbyname("www.bdjw.work");
if (host) {
std::cout << host->h_name << endl;
std::cout << host->h_length << endl;
if (AF_INET == host->h_addrtype)
std::cout << "AF_INET" << endl;
else
std::cout << host->h_addrtype << endl;
for (size_t i = 0; host->h_aliases[i]; i++)
std::cout << host->h_aliases[i] << endl;
for (size_t i = 0; host->h_addr_list[i]; i++)
std::cout << inet_ntoa(*(in_addr*)host->h_addr_list[i]) << endl;
}
SOCKADDR_IN addr;
memset(&addr, 0, sizeof(addr));
addr.sin_addr.s_addr = inet_addr("101.43.87.248");
host = gethostbyaddr((char*)&addr.sin_addr, 4, AF_INET);
if (host) {
std::cout << host->h_name << endl;
std::cout << host->h_length << endl;
if (AF_INET == host->h_addrtype)
std::cout << "AF_INET" << endl;
else
std::cout << host->h_addrtype << endl;
for (size_t i = 0; host->h_aliases[i]; i++)
std::cout << host->h_aliases[i] << endl;
for (size_t i = 0; host->h_addr_list[i]; i++)
std::cout << inet_ntoa(*(in_addr*)host->h_addr_list[i]) << endl;
}
WSACleanup();