I'm trying to complete a project for school that involves me prompting a user for an IP address, then displaying the hostname associated with it. I'm new at C#.
This is my code that I have so far:
Console.WriteLine("Enter an IP address:"); //prompt user to input IP address
string host = Console.ReadLine();
IPHostEntry hostEntry;
hostEntry = NewMethod(host);
if (hostEntry.AddressList.Length > 0)
{
var ip = hostEntry.AddressList[0];
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
s.Connect(ip, 80);
}
}
private static IPHostEntry NewMethod(string host)
{
return Dns.GetHostEntry(host); // NEED FIX
}
}
The part that has "Need fix" is where the error is showing up. Thanks in advance!
EDIT: How would I be able to display the results in this format: "Host name of x.x.x.x is: hhhhhh", where 'x.x.x.x' is the IP address user entered and 'hhhhh' is the host name of the IP?
EDIT(for StackOverflow): My question is more specific for this instance and is not a general question regarding the NullException issue.