Arul
Arul

Reputation: 141

Get ip address in vb.net

I want to get current internet ip address in vb.net. I don't want localhost ip address. like as (http://www.ipchicken.com/ website return ip address)

Upvotes: 1

Views: 4670

Answers (6)

user3624191
user3624191

Reputation: 1

Function GetIP() As String
    Dim IP As New WebClient
    Return IP.DownloadString("http://icanhazip.com/")
End Function

Upvotes: 0

John Cruz
John Cruz

Reputation: 1592

Here's what you're looking for. I wrote a simple PHP script to return the IP address when called, then wrote this VB.Net code to call it at any time:

   Public Function jnWhatIsMyExternalIP() As String
      Dim strURL As String = "http://www.mycompanywebsite/jnNetworkTools/jnCheckIP.php"

      Dim Request As System.Net.WebRequest = System.Net.WebRequest.Create(strURL)
      Dim Response As System.Net.WebResponse = Request.GetResponse()

      Dim Reader As New System.IO.StreamReader(Response.GetResponseStream())
      Dim strMyIP As String = Reader.ReadToEnd()

      Return strMyIP
   End Function

Here's the PHP code that is in jnCheckIP.php:

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

Calling the VB.Net function causes the users machine to call the PHP script on your server which then returns the users IP address.

Upvotes: 1

dbasnett
dbasnett

Reputation: 11773

Something like this should get your current public IP.

Public Class Form1

    Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
        AddHandler wb.DocumentCompleted, AddressOf wb_DocumentCompleted
    End Sub

    Public WithEvents wb As New WebBrowser
    'before using wb add
    'AddHandler wb.DocumentCompleted, AddressOf wb_DocumentCompleted
    Private Sub GetPubIP()
        Try
            'the site returns a string like "Current IP Address: 69.59.196.211"
            wb.Navigate(New Uri("http://checkip.dyndns.org"))
        Catch ex As Exception
            'add error checking
        End Try
    End Sub

    Private Sub wb_DocumentCompleted(ByVal sender As Object, _
                                     ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs)
        'parse the reply
        Dim parts() As String = wb.Document.Body.InnerText.Split(":"c)
        Debug.WriteLine(parts(1).Trim)
    End Sub

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

Upvotes: 1

m.edmondson
m.edmondson

Reputation: 30922

I understand what you are wanting is the IP address as you appear on the internet and NOT your internal network IP. I've got some code somewhere I'll dig out for you but it basically comprised of this (from memory):

Like I say I'll try and dig out the code but this should be more than enough information for you to work on :-)

Upvotes: 2

ololo
ololo

Reputation: 108

You can know real ip address only if your host (pc) have static internet ip address, not like 194.x.x.x or 10.x.x.x and other http://en.wikipedia.org/wiki/Private_network Otherwise try to send http request to service like www.ipchicken.com or other http://www.google.ru/search?hl=en&q=my+ip+address to know your ip address

Upvotes: 0

Related Questions