Loading Revolution ...
Z Talk - Beta
Talk Revolution ...
G-TalkYahoo!MSN
Discussion Forum:
    Forum Overview
    Thread Administration
    Topic Administration
    Hit it Now ...
    View Topics/Threads

General Forums:
    Fast Forwards
    IT Forum
    My Voice ...

Technical Forums:
    MSDN:C# forum
    Asp.Net Forum
    c#.Net Forum
    VB.Net Forum
    VB6 Forum
    MS-SQL Forum
    SUN Java/Jsp
    C/C++/Linux/Unix
   You are Here:   Skip Navigation LinksHome > Brainstorming Forums > > VB.Net Forum        Make This My Home Page
 
No messages were posted in the selected interval. You might like to contact Administrator to start a new blog/thread/post. Or, try looking @ Archives .Here is the last post:

Tuesday, March 11

Knowing your public IP for the machine  @ 06:38 PM   

A number of times I have found people trying to find out what their machine's public ip is. Here is a small code which describes how to achieve this. Even though it depends upon an external resource, you might find it helpful.
For details you might like to check MSDN forum as well on this topic.

Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Friend Class Form1
Inherits System.Windows.Forms.Form
Private Const ERROR_SUCCESS As Integer = 0
Private Const MAX_ADAPTER_NAME_LENGTH As Integer = 256
Private Const MAX_ADAPTER_DESCRIPTION_LENGTH As Integer = 128
Private Const MAX_ADAPTER_ADDRESS_LENGTH As Integer = 8

Private Structure IP_ADDRESS_STRING
Dim IpAddr() As Byte

Public Sub Initialize()
ReDim IpAddr(15)
End Sub
End Structure

Private Structure IP_MASK_STRING
Dim IpMask() As Byte

Public Sub Initialize()
ReDim IpMask(15)
End Sub
End Structure

Private Structure IP_ADDR_STRING
Dim dwNext As Integer
Dim IpAddress As IP_ADDRESS_STRING
Dim IpMask As IP_MASK_STRING
Dim dwContext As Integer

Public Sub Initialize()
IpAddress.Initialize()
IpMask.Initialize()
End Sub
End Structure

Private Structure IP_ADAPTER_INFO
Dim dwNext As Integer
Dim ComboIndex As Integer 'reserved
Dim sAdapterName() As Byte

Dim sDescription() As Byte
Dim dwAddressLength As Integer

Dim sIPAddress() As Byte
Dim dwIndex As Integer
Dim uType As Integer
Dim uDhcpEnabled As Integer
Dim CurrentIpAddress As Integer
Dim IpAddressList As IP_ADDR_STRING
Dim GatewayList As IP_ADDR_STRING
Dim DhcpServer As IP_ADDR_STRING
Dim bHaveWins As Integer
Dim PrimaryWinsServer As IP_ADDR_STRING
Dim SecondaryWinsServer As IP_ADDR_STRING
Dim LeaseObtained As Integer
Dim LeaseExpires As Integer

Public Sub Initialize()
ReDim sAdapterName((MAX_ADAPTER_NAME_LENGTH + 3))
ReDim sDescription((MAX_ADAPTER_DESCRIPTION_LENGTH + 3))
ReDim sIPAddress((MAX_ADAPTER_ADDRESS_LENGTH - 1))
IpAddressList.Initialize()
GatewayList.Initialize()
DhcpServer.Initialize()
PrimaryWinsServer.Initialize()
SecondaryWinsServer.Initialize()
End Sub
End Structure

Private Declare Function GetAdaptersInfo Lib "iphlpapi.dll" _
(ByRef pTcpTable As Long, ByRef pdwSize As Integer) As Integer

Private Declare Sub CopyMemory Lib "kernel32" _
Alias "RtlMoveMemory" _
(ByRef dst As Long, ByRef src As Long, ByVal bcount As Integer)

Private Declare Function URLDownloadToFile Lib "urlmon" _
Alias "URLDownloadToFileA" (ByVal pCaller As Integer, _
ByVal szURL As String, _
ByVal szFileName As String, ByVal dwReserved As Integer, _
ByVal lpfnCB As Integer) As Integer

Private Declare Function DeleteUrlCacheEntry Lib "Wininet.dll" _
Alias "DeleteUrlCacheEntryA" _
(ByVal lpszUrlName As String) As Integer

Private Declare Function lstrlenW Lib "kernel32" _
(ByVal lpString As Integer) As Integer



Private Sub Form1_Load(ByVal eventSender As System.Object, _
ByVal eventArgs As System.EventArgs) Handles MyBase.Load

Command1.Text = "Get Public IP"

Text2.Text = ""

End Sub


Private Sub Command1_Click(ByVal eventSender As System.Object, _
ByVal eventArgs As System.EventArgs) Handles Command1.Click
Text2.Text = GetPublicIP()

End Sub


Private Function GetPublicIP() As Object

Dim sSourceUrl As String
Dim sLocalFile As String
Dim hfile As Integer
Dim buff As String
Dim pos1 As Integer
Dim pos2 As Integer

'site returning IP address
sSourceUrl = _
"http://vbnet.mvps.org/resources/tools/getpublicip.shtml"
sLocalFile = "c:\ip.txt"

'ensure this file does not exist in the cache
Call DeleteUrlCacheEntry(sSourceUrl)

'download the public IP file,
'read into a buffer and delete
If DownloadFile(sSourceUrl, sLocalFile) Then

hfile = FreeFile
FileOpen(hfile, sLocalFile, OpenMode.Input)
buff = InputString(hfile, LOF(hfile))
FileClose(hfile)


'look for the IP line
pos1 = InStr(buff, "var ip =")

'if found,
If pos1 Then

'get position of first and last single
'quotes around address (e.g. '11.22.33.44')
pos1 = InStr(pos1 + 1, buff, "'", CompareMethod.Text) + 1
pos2 = InStr(pos1 + 1, buff, "'", CompareMethod.Text) '- 1

'return the IP address
GetPublicIP = Mid(buff, pos1, pos2 - pos1)

Else

GetPublicIP = "(unable to parse IP)"

End If 'pos1

Kill(sLocalFile)

Else

GetPublicIP = "(unable to access shtml page)"

End If 'DownloadFile

End Function


Private Function DownloadFile(ByVal sURL As String, _
ByVal sLocalFile As String) As Boolean

DownloadFile = URLDownloadToFile(0, sURL, sLocalFile, 0, 0) = _
ERROR_SUCCESS

End Function

End Class

Post your own comment
0 comment(s):-


Copyright © 2007-08 Zhatak.com.All Rights Reserved. Hit counter
Select interval

Select a single day, a week or the entire month:

<October 2008>
>>SuMoTuWeThFrSa
>2829301234
>567891011
>12131415161718
>19202122232425
>2627282930311
>2345678

(Set the focus to the textbox to show the calendar > Click on arrow or month or year to select dates)
Other interval:
From:
To:
Archives
2008 - March (1)
2008 - February (1)
2007 - November (2)
  •  
  • Maharashtra to legalise live-in relationships
  •  
  • No need to hit panic button: Counsellors
  •  
  • You are in queue, software majors tell new recruits
  •  
  • Economic slowdown to change campus placement trends
  •  
  • 38 days later, Orissa govt admits nun`s rape
  •  
  • After banks and nations, Iceland close to bankruptcy
  •  
  • Death of the American Dream: Indian kills family of five, self in LA
  •  
  • India top spam originator in Asia
  •  
  • Gmail gets Google Goggles
  •  
  • Gmail gets Google Goggles
  •  
  • TCS picks up Citigroup`s BPO arm for $505m
  •  
  • Sweet revenge for Gujarat
  •  
  • Bush signs Indo-US nuclear bill
  •  
  • Hospital ordered to pay Rs 1 lakh for needless tests
  •  
  • Do not forget the real issues
  •  
  • Indian-American given job of saving Wall Street
  •  
  • Making A Mockery Of Jihad
  •  
  • Mansoor Peerbhoy: An unlikely jihadi, he shows no remorse
  •  
  • When everyone lost
  •  
  • Pains of a slowing economy
  •  
  • Indian IT giants bidding for British tech consultancy: Report
  •  
  • Part-time vegetarianism takes root ..Flexitarians ??
  •  
  • Microsoft reviews hiring, to add jobs
  •  
  • BJP wants good ties with US: Advani to Rice
  •  
  • Nano - which state will host?
  •  
  • Day after pullout, trouble breaks out in Singur
  •  
  • Speak out and say yes to unity
  •  
  • 4.2 million new `green` jobs possible
  •  
  • English-speaking beggars on prowl in Delhi streets
  •  
  • Finally Done
  •  
  • Smoky Debate
  •  
  • Terror, inflation may dampen N-celebrations
  •  
  • Nuclear deal to be a win-win for India, US
  •  
  • Soumya murder has working women jittery
  •  
  • Google unveils $4.4 trillion "Clean Power by 2030" plan
  •  
  • Break It Up
  •  
  • Home truths about `those homos`
  •  
  • The Pirate Threat
  •  
  • Losing my religion
  •  
  • Ten Best tech Employers
  •  
  • Financial planning for India and indians
  •  
  • Stay Hungry Stay Foolish: 25 IIM-A entrepreneurs profiled by Rashmi Bansal
  •  
  • Is India ready for my.barackobama like political platform ?
  •  
  • Are you appalled by Preity Zinta endorsing BSNL
  •  
  • Power guzzling yellow lighting is going away in Haryana
  •  
  • Missing in Action
  •  
  • Indian democracy is doing fine
  •  
  • Now, TCS postpones promotions
  •  
  • Nuke deal caught in US financial storm
  •  
  • Dismissed employees beat CEO to death
  •  
  • India ranks 77th in Economic Freedom Index: Report
  •  
  • Is America becoming socialist?
  •  
  • Fragrance of fire
  •  
  • Free education for all up to XII: Raje
  •  
  • TCS, Satyam defer joining dates
  •  
  • Indian developers
  •  
  • Wanted: Housewives for banking, IT, retail jobs
  •  
  • RETAIL RIP-OFF: Happens Only In India
  •  
  • What is a B-school grad to do when Wall Street stops hiring?
  •  
  • They All Fall Down :First Lehman, then Merrill Lynch. What is next?
  •  
  • IT Pink slips age is back!!!!