0

Right now i have the requirement to find some PC's Hostname by his IP Address and i solved this problem with this function:

/// <summary>
/// Returns the DNS Name of IP Address without the Domain.
/// </summary>
public string GetDNSNameFromReverseLookUp(string ip)
{
    IPAddress hostIPAddress = IPAddress.Parse(ip);
    IPHostEntry hostInfo = Dns.GetHostEntry(hostIPAddress);
    return hostInfo.HostName.Substring(0, hostInfo.HostName.IndexOf('.'));
}

But i'm not happy with this substring() solution of the problem.

Is it allowed to use this function this way? Or do i have to fear some network issues in the future? Are there better (more secure) ways to get to the hostname without domain?

I'm looking for an more network secure solution, not another way to handle strings. As mentioned in the comments I fear a little bit, that not every OS returns the Hostname in the format:[Hostname].[Domainname].

2 Answers2

0

You will get an error if pc are not joined to a domain. Since the (dot) is not found, and so the use of split function is the most reasonable. I've already checked.

Adil
  • 124
  • 7
-2

Try this

public string GetDNSNameFromReverseLookUp(string ip)
{
    IPAddress hostIPAddress = IPAddress.Parse(ip);
    IPHostEntry hostInfo = Dns.GetHostEntry(hostIPAddress);
    return hostInfo.HostName.Split('.')[0];
}
Adil
  • 124
  • 7