Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between GetHostEntry and GetHostByName?

On MSDN it mentions GetHostByName is obsolete. The replacement is GetHostEntry. What are their difference?

like image 258
KMC Avatar asked Feb 28 '12 05:02

KMC


People also ask

Does GetHostByName use DNS?

Remarks. The GetHostByName method queries the Internet DNS server for host information. If you pass an empty string as the host name, this method retrieves the standard host name for the local computer.

What is DNS GetHostEntry?

The GetHostEntry method queries a DNS server for the IP address that is associated with a host name or IP address. If an empty string is passed as the hostNameOrAddress argument, then this method returns the IPv4 and IPv6 addresses of the local host.


1 Answers

It looks like GetHostEntry does a little more error checking and also supports Network Tracing

GetHostByName Decompiled:

public static IPHostEntry GetHostByName(string hostName)
{
  if (hostName == null)
    throw new ArgumentNullException("hostName");
  Dns.s_DnsPermission.Demand();
  IPAddress address;
  if (IPAddress.TryParse(hostName, out address))
    return Dns.GetUnresolveAnswer(address);
  else
    return Dns.InternalGetHostByName(hostName, false);
}

GetHostEntry Decompiled:

public static IPHostEntry GetHostEntry(string hostNameOrAddress)
{
  if (Logging.On)
    Logging.Enter(Logging.Sockets, "DNS", "GetHostEntry", hostNameOrAddress);
  Dns.s_DnsPermission.Demand();
  if (hostNameOrAddress == null)
    throw new ArgumentNullException("hostNameOrAddress");
  IPAddress address;
  IPHostEntry ipHostEntry;
  if (IPAddress.TryParse(hostNameOrAddress, out address))
  {
    if (((object) address).Equals((object) IPAddress.Any) || ((object) address).Equals((object) IPAddress.IPv6Any))
      throw new ArgumentException(SR.GetString("net_invalid_ip_addr"), "hostNameOrAddress");
    ipHostEntry = Dns.InternalGetHostByAddress(address, true);
  }
  else
    ipHostEntry = Dns.InternalGetHostByName(hostNameOrAddress, true);
  if (Logging.On)
    Logging.Exit(Logging.Sockets, "DNS", "GetHostEntry", (object) ipHostEntry);
  return ipHostEntry;
}
like image 111
Faisal Mansoor Avatar answered Sep 23 '22 19:09

Faisal Mansoor