I'm currently developing a client-server desktop application on C# for my assignment. I managed to retrieve the IP address of remote computer. Can I get the hostname of that remote computer by using IP Address?
Here is my code:
private static string GetMachineNameFromIPAddress(string ipAddress)
{
string machineName = string.Empty;
try
{
IPHostEntry hostEntry = Dns.GetHostEntry(ipAddress);
machineName = hostEntry.HostName;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
return machineName;
}
I put the retrieved IP address into a label
var socketip = ((IPEndPoint)client.Client.RemoteEndPoint).Address.ToString();
ipLabel.Text = socketip;
but it's not appearing when I tried to display it with this code
string CompName = GetMachineNameFromIPAddress(socketip);
listBox1.Items.Add("Computer Name: " + CompName);
Anyone know why?
It's possible that DNS may not have a hostname associated with that IP address. If you have access, try running the command below on the client and force the update with DNS. Here's more info on how you would do it: Renew DNS client registration using the ipconfig command
ipconfig /registerdns
Alternatively you could use Microsoft's ARP utility and just use the MAC address. Here's an article that would help with that route: Resolve IP Address And Host Name From MAC Address using C# and Windows ARP Utility
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.