Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resolve HostName to IP

Tags:

c#

I have been through a lot of googling for this, I found a lot of examples none of which was working for me. This is a simple issue which I feel has a simple answer without defining new classes\modules etc...

My code is this :

Console.WriteLine ("Please enter an IP address or hostname"); string host = Console.ReadLine (); ***IP = resolved "host"***  Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);     s.Connect (IP, 80); s.close(); 

How do I actually resolve the IP variable?

like image 940
Ba7a7chy Avatar asked Nov 06 '12 10:11

Ba7a7chy


People also ask

Which command will show us the resolve hostname to IP?

In an open command line, type ping followed by the hostname (for example, ping dotcom-monitor.com). and press Enter. The command line will show the IP address of the requested web resource in the response.

What does it mean to resolve hostname?

Hostname Resolution refers to the process through which an assigned hostname is converted or resolved to its mapped IP Address so that networked hosts can communicate with each other. This process can either be achieved locally on the host itself or remotely through a designated host configured to serve that purpose.


2 Answers

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); } 
like image 167
Kolja Avatar answered Sep 21 '22 17:09

Kolja


string howtogeek = "www.howtogeek.com"; IPAddress[] addresslist = Dns.GetHostAddresses(howtogeek);  foreach (IPAddress theaddress in addresslist) {    Console.WriteLine(theaddress.ToString());  } 

from howtogeek

like image 20
indiPy Avatar answered Sep 21 '22 17:09

indiPy