Trying to find my WAN IPaddress

Poppa Mintin

Well-known member
Joined
Jan 4, 2018
Messages
45
Programming Experience
10+
Hi,

I'm trying to find my WAN IP address. I think my code finds four results, the last of which I know in my LAN address.

The first three are reputed to be addresses in Hex. format but when I (think I) convert these to decimal, they don't resemble any IP address.

VB.NET:
Imports System.IO
Imports System.Net
Imports System.Net.Sockets


Public Class Form1


    Private Sub Form1_Load() Handles MyBase.Load
        Button1.Text = "Exit"
        Label1.Text = ""
        WhoAmI()
    End Sub


    Private Sub Button1_Click() Handles Button1.Click
        Me.Close()
    End Sub

    Private Sub WhoAmI()
        Dim ad1, ad2, hostName As String
        Dim i As Int32          '   (I use i after the loop is closed.)
        Dim ipAddr() As IPAddress


        hostName = System.Net.Dns.GetHostName()
        ipAddr = Dns.GetHostAddresses(hostName)


        For i = 0 To ipAddr.Count - 2
            Label1.Text += vbCrLf & ipAddr(i).ToString & vbCrLf
            ad1 = ipAddr(i).ToString
            Dim splt() As String = Split(ad1, ":")
            For j = 0 To splt.Count - 1
                ad2 = splt(j)
                ' This next line is to remove any %(+) from Ad2.
                ' But it makes no difference to the result.
                If Len(ad2) > 3 Then ad2 = ad2.Substring(0, 4)
                ad2 = Val("&H" & ad2).ToString
                Label1.Text += ad2 & " : "
            Next
            Label1.Text += vbCrLf
        Next
        Label1.Text += vbCrLf & "My LAN address is:   " & ipAddr(i).ToString
    End Sub

End Class

Where am I going wrong ?

(Try it out: zip attached)

Windows i7 machine. VS2017 latest update. (15.5.4.)


Poppa.

Oh! Do I have to apply a mask ?

Ok, I thought I might need to mask the result so I've tried:
VB.NET:
                ad2 = Val(CInt(CInt(Val("&H" & ad2)) And 255)).ToString

And instead if the 'And' I've tried 'Or' and 'Xor', I can't find how to use Nand or Nor. I also tried 256.
None of those have actually worked, but the results look more likely, in most cases there were no minus numbers for example.

Pop.

I tried to delete my next post... couldn't find how to do that. Pop.
 
Last edited by a moderator:
Thanks John,

Actually to be honest I didn't find anything in there that helped. However after a couple of days on the search engine I found an answer.

Very neat and to the point.

The code above has been pared down to the bare necessities and the new code added.
I'll not attach anything to this post, the form is much the same, although at one point I wanted to be able to copy and paste from the results so I changed the Label for a TextBox. Otherwise it's just dimensions which have changed, and they're all fine on my 1920x1080 screen.
VB.NET:
Imports System
Imports System.Net
Imports System.Net.Sockets

' Form1.    400x300
' TextBox1. 380x210
'   Location:   1, 1
'   Microsoft Sans Serif 18pt
'   TextAlign:  Center
'   Multiline:  True
'   Margins:    All 3
' Button1.  100x30
'   Location:   141, 218
'   Microsoft Sans Serif 12pt
'   TextAlign:  MiddleCenter
'   Margins:    All 0

Public Class Form1

    Private Sub Form1_Load() Handles MyBase.Load
        Button1.Text = "Exit"
        WhoAmI()
        PublicIP_Async()
    End Sub

    Private Sub Button1_Click() Handles Button1.Click
        Me.Close()
    End Sub

    Private Sub WhoAmI()
        Dim HostName As String = Dns.GetHostName()
        Dim host As IPHostEntry = Dns.GetHostEntry(HostName)
        Dim ip As IPAddress() = host.AddressList

        TextBox1.Text = "My name is " & HostName & vbCrLf & vbCrLf
        TextBox1.Text += "My private IP address is: " & vbCrLf & ip(ip.Length - 1).ToString & vbCrLf
    End Sub ' Get my name and private address.

    Private Async Sub PublicIP_Async()
        Dim httpClient As New System.Net.Http.HttpClient
        Dim ipad As String = Await httpClient.GetStringAsync("https://api.ipify.org")
        TextBox1.Text += vbCrLf & "My public IP address is: " & vbCrLf & ipad.ToString
    End Sub ' Get my public address.


End Class

With any luck this will help anyone else with a similar problem, and I've seen plenty of folk asking very similar questions.


Poppa.

Can't find how to mark this thread as resolved.
 
Last edited:
GetHostAddresses and AddressList return same information, it should be a coincidence that your last address is the private ipv4 one.
Something like this could be used if you want only private ip v4 address:
Dim ipv4 = (From addr In host.AddressList Where addr.AddressFamily = Net.Sockets.AddressFamily.InterNetwork And Not Net.IPAddress.IsLoopback(addr)).First

I recall getting ipv4 loopback here also, but do not currently, therefore threw in the IsLoopback check for measure.
 
Back
Top