I am trying to resolve a NoIP hostname to an IP address so I can connect to it. Below is the code I am currently using, however e->h_name just returns the hostname string I provided to it, so it does not work. In Python, the gethostbyname function does it successfully so I am confused why it wouldn't work in C++.
void ResolveServerDNS(char* hostname) {
WSADATA wsa;
WSAStartup(MAKEWORD(2, 2), &wsa);
hostent* e = gethostbyname(hostname);
std::cout << e->h_name << '\n';
}
To get the IP address, you need to use this instead:
This is the
hostentstruct from https://learn.microsoft.com/en-us/windows/win32/api/winsock/ns-winsock-hostentIn your function, it should be:
Let's test with this code : https://godbolt.org/z/zbTx7x3M7
Note: above is an example in case of
gethostbyname()returning ok -> you need to take care ifhostis NULL & maybe DNS returns more IPs for one domain -> let's use aforloop to get all the IPs: