Question

I'm looking for a function that will give me my real IP, not my local IP. the function i currently have, returns the ip in network and sharing center which is 192.168.2.100 But if I go to whatismyip, then it gives my real IP.

How could I get this using VB.NET? thanks

Was it helpful?

Solution

There's no way to do this just with VB.Net. You need to find a website that will tell you (perhaps one of your own?) or you need to interface with your router.

If you have a web site that is capable of running any sort of web page applications you can create a web page that simply outputs the client's (as in the computer connecting to the page) IP address.

I have one of my own:

http://etoys.netortech.com/devpages/ip.asp

Though I can't guarantee it will always be there which is why you should make your own.

From there it's a simple matter of using a HttpWebRequest to download the content of the page.

OTHER TIPS

To Combine the answers above" Create a php file and paste this in it:

<?php
echo $_SERVER['REMOTE_ADDR'];
?>

save it as curip.php and upload it to your server.

In your VB.net project create a module. Declare the imports section at the very top

Imports System.Net
Imports System.IO

And create your function:

    Public Function GetIP() As String

    Dim uri_val As New Uri("http://yourdomain.com/curip.php")
    Dim request As HttpWebRequest = HttpWebRequest.Create(uri_val)

    request.Method = WebRequestMethods.Http.Get

    Dim response As HttpWebResponse = request.GetResponse()
    Dim reader As New StreamReader(response.GetResponseStream())
    Dim myIP As String = reader.ReadToEnd()

    response.Close()

    Return myIP
End Function

Now anywhere in your code you can issue

Dim myIP = GetIP()

as use the value from there as you wish.

I'm probably just being crotchety here, but I can't help but think that your "real" IP address is the one returned by ifconfig (ipconfig) on your local machine. 192.168.2.100 in your case. Whatever NAT translation or tunneling goes on between you and the remote endpoint shouldn't matter, and if it does, there's a good chance you're doing something that could make your application only work in your current environment.

I would use a publicly available site which returns your public IP address in response.

The key factors here are:

  1. Availability of the service. Running your own service guarantees full control over it and knowledge of when it is available and when it's not. But in some cases it might be just too much work for such a simple task.
  2. Minimizing additional clutter contained in the response. There are plenty sites allowing you to get your public IP address, but they often do that in the form of a HTML page. Extracting the small fragment of the page containing the IP address might need additional code.

Keeping in mind these two factors I would recommend this URL: http://wtfismyip.com/text It has the advantage of returning only the IP address in textual form. There are also versions for:

Pick the format that is easier for you to parse.

You DON'T need to use a php file just use a site that shows your ip like ip-adress.com and then get the ip from there with webrequest and then use GetBetween function.

have fun :)

Using PHP it can be done simply:

As "shaiss" shared the PHP code, this is the VB.net code:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim myip As HttpWebRequest = HttpWebRequest.Create("http://YourHosting.com/curip.php")
        Dim grab As HttpWebResponse = myip.GetResponse()
        Dim stream As Stream = grab.GetResponseStream

        Dim SR As New StreamReader(stream)
        TextBox1.Text = SR.ReadToEnd()
    End Sub
End Class

This is just an example with:

1 - Form

1 - TextBox

1 - Button(s)

Hope this helps!

I had the same question and searched a bit only to remember that I can do this with WebBrowser!

Private Sub getExtIP() Handles activeProjectsWB.DocumentCompleted
    If gotIP = False And populateProjectCollectionBLN = True Then
        If activeProjectsWB.ReadyState = WebBrowserReadyState.Interactive Or activeProjectsWB.ReadyState = WebBrowserReadyState.Complete Then
            Dim unformattedExtIP As String = activeProjectsWB.Document.GetElementsByTagName("title").Item(0).OuterHtml
            Dim onlyIPAddress As String = String.Empty
            For Each character In unformattedExtIP
                Dim result As Integer = 0
                If Not (Integer.TryParse(character, result) = 0) Or character = "." Then
                    onlyIPAddress = onlyIPAddress & character
                End If
            Next
            extIP = onlyIPAddress
            gotIP = True
        End If
    End If
End Sub

This subroutine only is triggered when you navigate to a webpage using WebBrowser.Navigate().

The gotIP boolean exists because I have another subroutine that activates on any document completion. I don't want it to be triggered more than once. If you are unfamiliar with WebBrowser, you check to make sure the web page is loaded enough with ReadyState. If you don't, you may get an exception (because content isn't loaded).

You can use whichever site you like. This site is good because it puts your IP address in the title. That's good because there's going to be one title tag. In the event you can't use this site (or one that has a unique tag with your content within it), use a For Each loop.

                For Each instance As HtmlElement In activeProjectsWB.Document.GetElementsByTagName("InsertTagHere")
                    'do something to find the tag that contains your IP address
                Next
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top