views:

131

answers:

1

Hi,

I have the following code:

Dim ipAdd As IPAddress = Dns.GetHostEntry(strHostname).AddressList(0)
Dim strIP As String = ipAdd.ToString()

When I convert to String instead of an IPv4 address like 192.168.1.0 or similar I get the IPv6 version: fd80::5dbe:5d89:e51b:d313 address.

Is there a way I can return the IPv4 address from IPAddress type?

Thanks

+1  A: 

Instead of unconditionally taking the first element of the AddressList, you could take the first IPv4 address:

var address = Dns.GetHostEntry(strHostname)
                 .AddressList
                 .First(ip => ip.AddressFamily == AddressFamily.InterNetwork);
dtb
Here's the VB version of that:Dim ipAdd As IPAddress = Dns.GetHostEntry("aol.com").AddressList.First(Function(f) f.AddressFamily = Sockets.AddressFamily.InterNetwork)
Chris Haas