Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  .NET Framework  »  How to get IP addresses in .NET with a host name
How to get IP addresses in .NET with a host name
by John Spano | Published  12/01/2002 | .NET Framework | Rating:
John Spano

John Spano cofounder and CTO of NeoTekSystems, a Greenville, South Carolina technology consulting company. NeoTekSystems offers IT consulting, custom programming, web design and web hosting. We specialize in Microsoft .Net enterprise development and business design.

I have six years of experience in software architecture. My primary focus is on Microsoft technologies, and I have been involved in .NET since beta 1. I currently hold a MCSD certification, 2 MCTS's (Windows, Web) a MCPD in Distributed, 2 MCITP's, a Microsoft MVP, and have won the Helper of the Month contest for July 2002 in the devCity.NET forums.

Corporate URL: www.NeoTekSystems.com
Primary email: JSpano@NeoTekSystems.com
Alternate email: Jspano@devcity.net.

 

View all articles by John Spano...
How to get IP addresses in .NET with a host name

To get an IP address from a hostname you can use these functions:

You must first import/using System.Net

C#

Code:

//To get a host with a www address
IPHostEntry ip = Dns.GetHostByName ("www.vbcity.com");
IPAddress [] IpA = ipE.AddressList;
for (int i = 0; i < IpA.Length; i++)
{
    Console.WriteLine ("IP Address {0}: {1} ", i, IpA[i].ToString ());
}

//To get the local IP address
string sHostName = Dns.GetHostName ();
IPHostEntry ipE = Dns.GetHostByName (sHostName);
IPAddress [] IpA = ipE.AddressList;
for (int i = 0; i < IpA.Length; i++)
{
    Console.WriteLine ("IP Address {0}: {1} ", i, IpA[i].ToString ());
}


VB.NET

Code:
'To get a www address
Dim i As Integer
Dim ipE As IPHostEntry = Dns.GetHostByName("www.vbcity.com")
Dim IpA() As IPAddress = ipE.AddressList
For i = 0 To IpA.GetUpperBound(0)
    Console.Write("IP Address {0}: {1} ", i, IpA(i).ToString)
Next

'To get local address
Dim sHostName As String
Dim i As Integer
sHostName = Dns.GetHostName()
Dim ipE As IPHostEntry = Dns.GetHostByName(sHostName)
Dim IpA() As IPAddress = ipE.AddressList
For i = 0 To IpA.GetUpperBound(0)
    Console.Write("IP Address {0}: {1} ", i, IpA(i).ToString)
Next

This article was originally posted as devCity.NET Forums FAQ - http://www.devcity.net/forums/faq.asp?fid=30#TID4879

How would you rate the quality of this article?
1 2 3 4 5
Poor Excellent
Tell us why you rated this way (optional):

Article Rating
The average rating is: No-one else has rated this article yet.

Article rating:3.54687499999999 out of 5
 128 people have rated this page
Article Score59809
Sponsored Links