tags:

views:

2123

answers:

4

Hello,

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

+4  A: 

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.

Spencer Ruport
A: 

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.

shaiss
Not familiar with php, but does the script you use depend on the HTTP headers to retrieve the IP? If so, it is possible for the user to manipulate these to specify whatever IP address they want. It may not be a huge concern, but it's something to take into consideration.
Kevin Pang
That's a good point. However, the only way to do so that I can think of since the vb.net app is calling the php file is the user would have to be using a proxy server.But yes, the particular case where this is used should be taken into account.
shaiss
+1  A: 

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.

A: 

There has to be an easier way than that -- Why is this functionality not available in VB.NET?

Brandon