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]
.