In a C# program, take an IPv4 address provided by the user, and return the hostn
ID: 3870861 • Letter: I
Question
In a C# program, take an IPv4 address provided by the user, and return the hostname for that IPv4 address. I have come a decent way but my code will only return: System.Net.IPHostEntry. Here is a copy of my code:
string ip;
Console.WriteLine("Please enter an IPv4 address: ");
ip = Console.ReadLine();
IPAddress address = IPAddress.Parse(ip);
IPHostEntry hostInfo = Dns.GetHostEntry(address);
Console.WriteLine(hostInfo);
Like I said, I need the code to essential accept an IP address and return the hostname for that address. So if the user enters: 74.125.136.102, the program should display: google.com.
Explanation / Answer
You can try this:
IPHostEntry hostEntry = Dns.GetHostEntry(ipaddress);
Console.WriteLine("DNS name : " + hostEntry.HostName);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.