5

I've been trying to find the correct situtation when to use GetHostAddresses or GetHostEntry. I understand by reading this article (http://msdn.microsoft.com/en-us/library/ms143998(v=vs.110).aspx) that GetHostEntry will do the reverse dns lookup and GetHostAddresses does not..

However under what exact scenario you need to use GetHostEntry rather than GetHostAddresses? also, what is the primary reason for GetHostEntry method to perform reverse DNS lookup?

user384080
  • 4,576
  • 15
  • 64
  • 96

2 Answers2

3
  • GetHostEntry(hostNameOrAddress)

The GetHostEntry method queries a DNS server for the IP address that is associated with a host name or IP address.The method assumes that if an IP literal string is passed in the hostNameOrAddress parameter that the application wants an IPHostEntry instance returned with all of the properties set. These properties include the AddressList, Aliases, and HostName. This method can be used if you want to find the hostname associated with an IP address.

Reverse DNS is mostly for such things as tracking where a web-site visitor came from, or where an e-mail message originated etc. It is typically not as critical in as forward DNS - visitors will still reach your web-site just fine without any reverse DNS for your web-server IP or the visitor's IP. Reverse DNS is important for one particular application.Many e-mail servers on the Internet are configured to reject incoming e-mails from any IP address which does not have reverse DNS.

  • GetHostAddresses(hostNameOrAddress)

But the GetHostAddresses method queries a DNS server for the IP addresses associated with a host name. If hostNameOrAddress is an IP address, this address is returned without querying the DNS server. This query will return all the IP addresses related to the hostname you provide.

The difference between GetHostEntry and GetHostAddresses is that whenever you give an IP address to GetHostEntry it will query the DNS server and try to get the hostname for that IP address and then get all the addresses associated with it.If the data for a successfull reverse resolve is not in your DNS server, this will fail.

Vishnuraj V
  • 2,819
  • 3
  • 19
  • 23
  • 2
    mate you are not answering my question.. the question really is in what scenario you need to use GetHostEntry to perform reverse DNS lookup ? why do you need to perform reverse DNS lookup while what you care is to get the ip address on a given host name ? – user384080 Apr 21 '14 at 04:40
  • 1
    to know the hostname associated with an ip address GetHostEntry can be used. If you just want the ip then it doesn't matter which one u use. :) – Vishnuraj V Apr 21 '14 at 04:47
  • 2
    wrong! it does matter! the msdn advised that the GetHostEntry will perform reverse DNS lookup which potentially causing 'No such host is known' exception but not it's not the case with GetHostAddresses. Now the question really is what the primary reason people using GetHostEntry method? – user384080 Apr 21 '14 at 05:04
  • MSDN says: If the host name could not be found, the SocketException exception is returned with a value of 11001 (Windows Sockets error WSAHOST_NOT_FOUND). This exception can be returned if the DNS server does not respond. This exception can also be returned if the name is not an official host name or alias, or it cannot be found in the database(s) being queried. – Vishnuraj V Apr 21 '14 at 05:09
  • This may not be the case always. Besides gethostaddresses is enough most of the time. – Vishnuraj V Apr 21 '14 at 05:10
3

One scenario where I see it useful as opposed to GetHostAddresses would be if you already know one IP address of a host and want to find the other IP Addresses. If an IP is specified as parameter, GetHostEntry would return all addresses while the GetHostAddresses will return only one (when IP is specified as parameter).

Kosmo
  • 402
  • 7
  • 21