Resolve HostName to IP

string howtogeek = "www.howtogeek.com";
IPAddress[] addresslist = Dns.GetHostAddresses(howtogeek);

foreach (IPAddress theaddress in addresslist)
{
   Console.WriteLine(theaddress.ToString());
 }

from howtogeek


You can simply use the DNS class to do so:

IPHostEntry hostEntry;

hostEntry= Dns.GetHostEntry(host);

//you might get more than one ip for a hostname since 
//DNS supports more than one record

if (hostEntry.AddressList.Length > 0)
{
      var ip = hostEntry.AddressList[0];
      Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
      s.Connect(ip, 80);
}

Tags:

C#