Difference between GetHostEntry and GetHostByName?

On MSDN, he mentions that he is GetHostByNamedeprecated. Replacement GetHostEntry. What is their difference?

+7
source share
2 answers

It seems that GetHostEntry does a bit 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;
}
+8
source

First, it’s important to understand that these are shells of the UNIX socket library that provides functions inet_aton(equivalent IPAddress.Parse), gethostbyname(wrapped Dns.GetHostByName), and gethostbyaddr(wrapped Dns.GetHostByAddress). Subsequently, Microsoft added to their Dns.GetHostEntryutility function Dns.GetHostEntry.

Dns.GetHostByName Dns.GetHostByName Dns.GetHostEntry, , Microsoft , API, DNS, DNS.

UNIX gethostbyname IP-, . IP-, , . IPv4. getaddrinfo, , , , , IPv4.

Microsoft . - GetHostByName , , , , DNS, . , , , GetHostEntry , DNS, GetHostEntry , . , GetHostEntry, DNS, IP- GetHostEntry, DNS. , DNS-, , , , , - .

, , GetHostEntry , , IP-, , , IP-, , , . GetHostByName - , , Microsoft . , , " ", @Faisai Mansoor GetHostByName:

// Microsoft internal code for GetHostByName:
if (IPAddress.TryParse(hostName, out address))
  return Dns.GetUnresolveAnswer(address);
else
  return Dns.InternalGetHostByName(hostName, false);

Dns Dns, , :

if (!IPAddress.TryParse(userInput, out var addressToWhichToConnect))
  addressToWhichToConnect = Dns.GetHostEntry(userInput).AddressList.First();
0

All Articles