GiANTOnFire
GiANTOnFire

Reputation: 193

Returning IPv6 instead of IPv4 in VB

I have this function which is querying the IP address.

Dim strHostName As String
Dim strIPAddress As String 

Public Function ipconfig()
  strHostName = System.Net.Dns.GetHostName()
  strIPAddress = System.Net.Dns.GetHostEntry(strHostName).AddressList(0).ToString()

  rtb_Output.Text = rtb_Output.Text + "Computer Name: " & strHostName + Environment.NewLine + "IP Address: " &strIpAddress
End Function

It works absolutely fine on my Windows 7 desktop wired connection, and returns the IPv4 address as expected. When I run it on my Windows 8 tablet, either wired or wirelessly, it returns the IPv6 address instead, whereas I need the IPv4 address. Do you have any ideas why, or how I can get it to return the IPv4 address instead?

Upvotes: 1

Views: 4987

Answers (4)

andy
andy

Reputation: 1

 Public Function GetIPv4Address()
        GetIPv4Address = String.Empty
        Dim strmachine As String = System.Net.Dns.GetHostName()
        Dim iphe As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry(strmachine)

        For Each ipheal As System.Net.IPAddress In iphe.AddressList
            If ipheal.AddressFamily = System.Net.Sockets.AddressFamily.InterNetwork Then
                GetIPv4Address = ipheal
               ' MsgBox(ipheal.ToString, MsgBoxStyle.Critical, "ERROR")

           End If
        Next

    End Function

Upvotes: 0

LWChris
LWChris

Reputation: 4201

You can use LINQ to filter the results:

Dim ipHostEntry = Dns.GetHostEntry(Dns.GetHostName)
Dim ipAddress = ipHostEntry.AddressList.FirstOrDefault(Function(ip) ip.AddressFamily = AddressFamily.InterNetwork)
If ipAdress IsNot Nothing Then
  ' Output ipAdress.ToString()
Else
  ' No IPv4 address could be retrieved
End If

Explanation: IPHostAddress.AddressList returns an Array(Of IPAddress) which implements the IEnumerable interface and can therefore be enumerated by LINQ expressions.

FirstOrDefault will return the first element from the AddressList array that matched the predicate lambda function that is submitted as first and only parameter of FirstOrDefault. The predicate function has to be written to return a boolean.

The array is iterated from the first to the last element, and for each element the lambda function is evaluated, where its parameter ip is the current iteration item. With ip.AddressFamily = AddressFamily.InterNetwork we determine whether the current item is an IPv4 address. If so, the expression evaluates true and the item is returned by FirstOrDefault. If it evaluates to false, the next item from the array is checked and so on. If no element matches the predicate, FirstOrDefault returns the default value, in this case Nothing (N. B.: the extension First works the same way but throws an exception if no item matches the predicate; both First and FirstOrDefault can be called without any arguments, they return the first element of the sequence then).

I prefer the extension methods based notation as above, but you can use the original From In Where Select LINQ notation as well if you prefer:

Dim ipAddress = (From ip In ipHostEntry.AddressList
                 Where ip.AddressFamily = AddressFamily.InterNetwork
                 Select ip)(0)

Note that (0) is the argument for another extension method ElementAtOrDefault(index As Integer) in this case.

Upvotes: 0

cstick
cstick

Reputation: 374

It seems unlikely, but certainly not impossible, that your Windows 8 machine has only IPv6 addresses. However, it is not uncommon to turn off IPv6 support on network adapters for any OS as it is not commonly supported yet. Check the adapters for each computer to verify the addresses and compare to your result.

Regardless, you would be advised to assume that your query will return multiple addresses of type IPv6, IPv4 and more with multiples of any type as well. If you are looking for specific types then use the AddressFamily property to identify type.

For example, for IPv4 only:

Dim hostEntry = Dns.GetHostEntry(hostNameOrAddress)

    Dim addressList As New List(Of IPAddress)

    For Each address In hostEntry.AddressList
        If address.AddressFamily = Sockets.AddressFamily.InterNetwork Then
            addressList.Add(address)
        End If
    Next

    Return addressList

Upvotes: 2

CJaimyProductie
CJaimyProductie

Reputation: 11

You can use this:

System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName).AddressList.First.ToString()

Upvotes: 0

Related Questions