Go to home page mail me! RSS Feed

Get IP address of host

Thursday, March 22, 2007 12:13 AM

Grabbing the Internet Protocol (IP) of a given host name has never been easier, it so easy - it can be done with a single line of code. Anyway, a host name may return one of more Internet Protocol (IP) addresses. I was messing around with some DNS stuff today and I figured I'll share this snippet since there are a bunch of posts out there on how to do DNS queries such as getting the Internet Protocol (IP) address given a host name.

 

    private void GetIPFromHostName(string hostNameOrIPAddress)
{
System.Net.IPAddress[]
ipAddresses = System.Net.Dns.GetHostAddresses(hostNameOrIPAddress);

foreach (System.Net.IPAddress Internet Protocol (IP) in ipAddresses)
{
// Use Internet Protocol (IP) here
}
}
 
To use call:
 
GetIPFromHostName("dotnetclr.com");

Your Comments.

No comments posted yet.

Your Reply.

Comment Form.

Fields denoted with a "*" are required.

You may also like to leave your email or website.

 
Please add 3 and 4 and type the answer here:

Preview Your Comment.

 
Next entries »