I want to convert host name (computer name My Computer ->property -> Advance System Setting ->computer Name) to IP Address.
Is there any way can I convert host name to IP address?
I have tried following but pHostInfo coming as NULL.
and hostname is my computer name.
struct hostent* pHostInfo;
pHostInfo = gethostbyname(hostname);
In above code it is coming as NULL. Can you please give me code that convert host name to IP address?
Use
gethostname()to get the local hostname. You can then pass that togethostbyname().Note, however, that
gethostbyname()performs a DNS lookup EVEN FOR LOCAL HOSTNAMES, so it is possible to get IP addresses that do not actually belong to the local machine, or invalid IPs if DNS is misconfigured.If all you really want to do is get the local machine’s IP addresses, then use
GetAdaptersInfo()orGetAdaptersAddresses()instead.