Question

I'm trying to get the ip address of my local PC, and one one of my other PCs it gets the v4 address fine, but on this one the code:

Dns.GetHostEntry(Dns.GetHostName).AddressList(0).ToString()

returns what I guess is a IPv6 address:

fe80::9c09:e2e:4736:4c62%11

How do I get the IPv4 address?

Was it helpful?

Solution

Disclaimer- I don't have IPv6 installed and there is probably a much better way to do this, but what does the following return:

Dns.GetHostEntry(Dns.GetHostName()).AddressList
    .Where(a => !a.IsIPv6LinkLocal && !a.IsIPv6Multicast && !a.IsIPv6SiteLocal)
    .First()
    .ToString();

Edit - didn't notice you were asking in VB, so I've tried translating it to:

Dim s As String = Dns.GetHostEntry(Dns.GetHostName()).AddressList _
    .Where(Function(a As IPAddress) Not a.IsIPv6LinkLocal AndAlso Not a.IsIPv6Multicast AndAlso Not a.IsIPv6SiteLocal) _
    .First() _
    .ToString()

This may blow up, so don't treat it as production code.

OTHER TIPS

Here's my solution for getting a routable IPv4 IP without using an external service:

  Function GetLocalIP() As String
    Dim IPList As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName)

    For Each IPaddress In IPList.AddressList
      'Only return IPv4 routable IPs
      If (IPaddress.AddressFamily = Sockets.AddressFamily.InterNetwork) AndAlso (Not IsPrivateIP(IPaddress.ToString)) Then
        Return IPaddress.ToString
      End If
    Next
    Return ""
  End Function

  Function IsPrivateIP(ByVal CheckIP As String) As Boolean
    Dim Quad1, Quad2 As Integer

    Quad1 = CInt(CheckIP.Substring(0, CheckIP.IndexOf(".")))
    Quad2 = CInt(CheckIP.Substring(CheckIP.IndexOf(".") + 1).Substring(0, CheckIP.IndexOf(".")))
    Select Case Quad1
      Case 10
        Return True
      Case 172
        If Quad2 >= 16 And Quad2 <= 31 Then Return True
      Case 192
        If Quad2 = 168 Then Return True
    End Select
    Return False
  End Function

Note that my code is also verifying that the range is routable (IsPrivateIP). You can remove or modify that part if you are looking for something else.

I think you should use this:

 Dim tmpHostName As String = System.Net.Dns.GetHostName()
 myIPaddress = System.Net.Dns.GetHostByName(tmpHostName).AddressList(0).ToString()

GetHostByName is obsolete but this is the way to get the IPv4. Why? Because the getbyhostname function is created before IPv6 so the function get only the IPv4 connection, not the fe80::9c09:e2e:4736:4c62%11.

Something maybe fun is this little function that'll show all IP addresses on your computer:

Public Function getOwnIp() As String
        Dim hostIP As IPHostEntry = Dns.GetHostEntry(Dns.GetHostName())
        Dim position As Integer = 0
        Dim ip As String = Nothing
        While ipList < hostIP.AddressList.Length
            ip += hostIP.AddressList(position).ToString & vbCrLf
            position += 1
        End While`enter code here`
        Return ip
    End Function

I was looking for the answer to this question myself and I could not find one suitable to my needs. I managed to experiment with various answers across the net until I came up with this (works great!). Just thought I would share since this post is the top result via Google.

    ''''Routine to fetch IPv4 Network addresses for all local network interfaces.
    Dim adapters As NetworkInterface() = NetworkInterface.GetAllNetworkInterfaces()
    Dim adapter As NetworkInterface
        For Each adapter In adapters
            Dim properties As IPInterfaceProperties = adapter.GetIPProperties()
            If properties.UnicastAddresses.Count > 0 Then
                For Each unicastadress As UnicastIPAddressInformation In properties.UnicastAddresses
                    Dim ip As IPAddress = unicastadress.Address
                    If ip.AddressFamily = AddressFamily.InterNetwork Then
                        ComboBox1.Items.Add(ip.ToString)
                    End If
                Next unicastadress
            End If
        Next adapter

You first need to import the system namespace into your application and then create an instance of the System.Net.NetworkInformation.IPAddressInformation and use it as such

Example

  Imports system.data.sqlclient
    imports system

Public class Form1
   Dim IPAdd As System.Net.NetworkInformation.IPAddressInformation

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

 MsgBox("My IP Address is " & IPAdd.Address.ToString)
    End Sub
End Class
 Dim localIp As IPHostEntry = Dns.GetHostEntry(Dns.GetHostName())
 txtLocal.Text = localIp.AddressList(1).ToString

Notice that I changed the (0) index to (1).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top