miltoncwb


Dear Experts,

How can I get the external IP from a vfp program just like I visit www.checkip.org

Thanks you in advance.

Milton




Re: Get external IP adress

Naomi Nosonovsky


For local IP address check http://www.kirtlandsys.com/misc/WhatsMyIPAddress.pdf

You can also determine your external IP address (search google for this, it returns several useful links).






Re: Get external IP adress

dni

I don't understand why you post if you don't have an answer ...





Re: Get external IP adress

Naomi Nosonovsky

It is a partial answer and still can be helpful at least to find the local IP. Also I suspect that external address for any web site cold not be retrieved easily, otherwise I would be able to find it in my searches. Though I found an opposite - construct country, city, etc. based on IP address.



Re: Get external IP adress

dni

To get local IP you may use:

oSock = CREATEOBJECT("MSWinsock.Winsock.1")
oSock.LocalIP


There are different; and if you have local ip address won't help to find externall address ...






Re: Get external IP adress

Naomi Nosonovsky

Yuri Rubinov gave me the following link http://www.urgentclick.com/ip_address_lookup.php to find IP address for any web-site.

And VFP code (from AlexSS) looks like this:

Code Snippet

lcHostName = "www.google.com"

lcCommand = "%comspec% /c ping " + lcHostName + " -n 1 > ping.log"
loWshShell = CREATEOBJECT("WScript.Shell")
loWshShell.RUN(lcCommand, 0, .T.)

lcPing = filetostr("ping.log")

** To view the result
=MESSAGEBOX(m.lcPing)

======================================

And then you simply parse output.





Re: Get external IP adress

Naomi Nosonovsky

Found the following class by Mikhail Drosdov to get IP address (local or by web address):

Code Snippet

*////////////////////////////////////////////////////////////////////////////
#DEFINE CLS_NAME "clsIpAddr"
LOCAL loIpAddr, lbRetVal
loIpAddr = NEWOBJECT(CLS_NAME)
IF VARTYPE(loIpAddr) = 'O'
LOCAL lcLocalHost
lcLocalHost = loIpAddr.GetLocalHostName()
IF !EMPTY(lcLocalHost)
LOCAL lcIpAddr
lcIpAddr = loIpAddr.GetHostIP(lcLocalHost)
IF !EMPTY(lcIpAddr)
CLEAR
"Local host: "+lcLocalHost
"IP address: "+lcIpAddr
lbRetVal = .T.
ELSE
MESSAGEBOX("Can not define IP address for host: "+lcLocalHost, 16, _SCREEN.CAPTION)
ENDIF
ELSE
MESSAGEBOX("Can not define local host.", 16, _SCREEN.CAPTION)
ENDIF
ELSE
MESSAGEBOX("Can not create '"+CLS_NAME+"' object", 16, _SCREEN.CAPTION)
ENDIF
RELEASE loIpAddr
CLEAR DLLS
RETURN lbRetVal

*////////////////////////////////////////////////////////////////////////////
#DEFINE WSADATA_SIZE 398
#DEFINE WS_VERSION 514 && 0x0202
#DEFINE HOSTENT_SIZE 16
#DEFINE SOCKET_ERROR -1
#DEFINE S_OK 0

DEFINE CLASS clsIpAddr AS SESSION
PROTECTED bIsDecl
PROTECTED bIsInitWinSock
PROTECTED pWSADATA

bIsDecl = .F.
bIsInitWinSock = .F.
pWSADATA = ""

PROCEDURE INIT
RETURN this.InitWinSock()
ENDPROC

PROCEDURE DESTROY
this.ClearWinSock()
ENDPROC

FUNCTION GetLocalHostName
* Returns the standard host name for the local machine
LOCAL lcBuffer, lnResult
lcBuffer = SPACE(250)
lnResult = gethostname(@lcBuffer, LENC(lcBuffer))
IF lnResult = S_OK
RETURN SUBSTRC(lcBuffer, 1, ATC(CHR(0), lcBuffer)-1)
ENDIF
this.ShowLastWinSockErr()
RETURN ""
ENDFUNC

FUNCTION GetHostIP
LPARAMETERS tcHostname
* Returns IP addres for the tcHostname
LOCAL lcHOSTENTptr, lcHOSTENT, lnAddrlistPtr
*
* address for the HOSTENT structure
lcHOSTENTptr = gethostbyname(tcHostname)
IF lcHOSTENTptr <> 0
lcHOSTENT = this.GetMemBuf(lcHOSTENTptr, HOSTENT_SIZE)
*
* a pointer to a null-terminated list of addresses
lnAddrlistPtr = this.buf2dword(SUBSTRC(lcHOSTENT, 13, 4))
RETURN this.GetIPfromHOSTENT(lnAddrlistPtr)
ENDIF
RETURN ""
ENDFUNC

PROTECTED PROCEDURE DECL
IF !this.bIsDecl
DECLARE INTEGER WSAStartup IN ws2_32 ;
INTEGER wVerRq, STRING @lpWSAData
DECLARE INTEGER WSACleanup IN ws2_32
DECLARE INTEGER WSAGetLastError IN ws2_32
DECLARE INTEGER gethostbyname IN ws2_32 ;
STRING HOSTNAME
DECLARE INTEGER gethostname IN ws2_32 ;
STRING @NAME, INTEGER namelen
DECLARE RtlMoveMemory IN kernel32 AS Heap2Str ;
STRING @DEST, INTEGER Src, INTEGER nLength
this.bIsDecl = .T.
ENDIF
RETURN this.bIsDecl
ENDPROC

PROTECTED FUNCTION InitWinSock
LOCAL lnInitResult
IF this.DECL() AND !this.bIsInitWinSock
LOCAL lpWSAData
lpWSAData = REPLICATE(CHR(0), WSADATA_SIZE)
lnInitResult = WSAStartup(WS_VERSION, @lpWSAData)
IF lnInitResult = S_OK
this.pWSADATA = lpWSAData
this.bIsInitWinSock = .T.
RETURN .T.
ENDIF
this.ShowLastWinSockErr()
ENDIF
RETURN .F.
ENDFUNC

PROTECTED FUNCTION ClearWinSock
IF this.bIsDecl AND this.bIsInitWinSock
IF WSACleanup() = S_OK
this.bIsInitWinSock = .F.
RETURN .T.
ELSE
this.ShowLastWinSockErr()
ENDIF
ENDIF
RETURN .F.
ENDFUNC

PROTECTED PROCEDURE ShowLastWinSockErr
LOCAL lnWinSockErr, lcMsg
lnWinSockErr = WSAGetLastError()
lcMsg = "Windows Sockets Error code: "+LTRIM(STR(lnWinSockErr))
= MESSAGEBOX(lcMsg, 16, _SCREEN.CAPTION)
ENDPROC

PROTECTED FUNCTION buf2dword
LPARAMETERS lcBuffer
RETURN ASC(SUBSTRC(lcBuffer, 1,1)) + ;
ASC(SUBSTRC(lcBuffer, 2,1)) * 256 +;
ASC(SUBSTRC(lcBuffer, 3,1)) * 65536 +;
ASC(SUBSTRC(lcBuffer, 4,1)) * 16777216
ENDFUNC

PROTECTED FUNCTION GetIPfromHOSTENT
LPARAMETERS tnAddrlistPtr
* Retrieving IP address from the HOSTENT structure
LOCAL lnDataAddress, lcResult
lnDataAddress = this.buf2dword(this.GetMemBuf(tnAddrlistPtr, 4))
RETURN IIF(lnDataAddress <> 0, this.GetIPAddress(this.GetMemBuf(lnDataAddress, 4)), "")
ENDFUNC

PROTECTED FUNCTION GetIPAddress
LPARAMETERS tcAddrBuf
* Converts 4-characters string buffer
* to the IP address string representation
LOCAL lcResult, lnCnt
lcResult = ""
FOR lnCnt=1 TO 4
lcResult = lcResult;
+ LTRIM(STR(ASC(SUBSTRC(tcAddrBuf, lnCnt, 1))));
+ IIF(lnCnt=4, "", ".")
ENDFOR
RETURN lcResult
ENDFUNC

PROTECTED FUNCTION GetMemBuf
LPARAMETERS tnAddr, tnBufsize
LOCAL lcBuffer
lcBuffer = REPLICATE(CHR(0), tnBufsize)
= Heap2Str(@lcBuffer, tnAddr, tnBufsize)
RETURN lcBuffer
ENDFUNC

ENDDEFINE





Re: Get external IP adress

Naomi Nosonovsky

One extra similar solution by Alex Roublev:

Code Snippet

* Winsock: Using a host name to retrieve its information from a host database

#Define HOSTENT_SIZE 16
Do Decl

If Not InitWinsock()
Return
Endif

Local lcSourceHost, lnHostAddr, lcHOSTENT, lnNamePtr, lnAddrlistPtr
*lcSourceHost = "localhost"
*lcSourceHost = "aol.com"
*lcSourceHost = "microsoft.com"
lcSourceHost = "google.com"

*lcSourceHost = "takeiteasy.com"
*lcSourceHost = "nevermind.com"
*lcSourceHost = "cheerup.com"

* retrieving an address for the HOSTENT structure
lnHostAddr = gethostbyname(lcSourceHost)

If lnHostAddr <> 0
* retrieving and displaying the HOSTENT data

* copying data from the structure to a VFP string
lcHOSTENT = GetMemBuf (lnHostAddr, HOSTENT_SIZE)

* a pointer to the host name
lnNamePtr = buf2dword(Substr(lcHOSTENT, 1,4))
"Host name:", GetMemStr(lnNamePtr)

* a pointer to a null-terminated list of addresses
lnAddrlistPtr = buf2dword(Substr(lcHOSTENT, 13,4))

* displaying IP addresses for this host
= DisplayIPs (lnAddrlistPtr)
Endif

* terminating use of the Ws2_32.dll
= WSACleanup()
* End of Main

Procedure DisplayIPs (lnAddrlistPtr)
* retrieving IP addresses from the list

Local lnElementPtr, lnIPcount, lcDataAddress, lnDataAddress,;
lcIPAddrBuf

lnIPcount = 0 && list elements retrieved
lnElementPtr = lnAddrlistPtr && first member on the list

* scanning the list one member by another
* until a null member found
Do While .T.

* the list member contains a DWORD address of its data

lcDataAddress = GetMemBuf (lnElementPtr, 4)
lnDataAddress = buf2dword(lcDataAddress)

* the result -- lnDataAddress -- is an address
* to DWORD IP address for this host

If lnDataAddress = 0
* the last member on this list is null
Exit
Endif

* retrieving a DWORD with an IP address
lcIPAddrBuf = GetMemBuf (lnDataAddress, 4)

lnIPcount = lnIPcount + 1
"IP " + Ltrim(Str(lnIPcount)) + ":",;
GetIPAddress(lcIPAddrBuf)

* shifting to the next element on the list
lnElementPtr = lnElementPtr + 4 && DWORD
Enddo
Return

Function InitWinsock()
* Initializing the Winsock service for the application
#Define WSADATA_SIZE 398
#Define WS_VERSION 514 && 0x0202
Local lcWSADATAln, lnInitResult
lcWSADATA = Repli(Chr(0), WSADATA_SIZE)
lnInitResult = WSAStartup (WS_VERSION, @lcWSADATA)
Return (lnInitResult = 0)

Function GetMemStr (lnAddr)
* returning data from a memory block as a VFP string
#Define MEMSTR_BUFSIZE 255
Local lcBuffer
lcBuffer = Space(MEMSTR_BUFSIZE)
= Heap2Str (@lcBuffer, lnAddr, MEMSTR_BUFSIZE)
Return Substr(lcBuffer, 1, At(Chr(0),lcBuffer)-1)

Function GetMemBuf (lnAddr, lnBufsize)
Local lcBuffer
lcBuffer = Repli(Chr(0), lnBufsize)
= Heap2Str (@lcBuffer, lnAddr, lnBufsize)
Return lcBuffer

Function GetIPAddress (lcAddrBuf)
* converting 4-characters string buffer
* to string representation of the IP address
Local lcResult, ii
lcResult = ""
For ii=1 To 4
lcResult = lcResult +;
LTRIM(Str(Asc(Substr(lcAddrBuf, ii,1)))) +;
Iif(ii=4, "",".")
Endfor
Return lcResult

Function buf2dword (lcBuffer)
Return Asc(Substr(lcBuffer, 1,1)) + ;
Asc(Substr(lcBuffer, 2,1)) * 256 +;
Asc(Substr(lcBuffer, 3,1)) * 65536 +;
Asc(Substr(lcBuffer, 4,1)) * 16777216

Procedure Decl
Declare Integer WSAStartup In ws2_32 Integer wVerRq, String @lpWSAData
Declare Integer WSACleanup In ws2_32
Declare Integer gethostbyname In ws2_32 String HostName

Declare RtlMoveMemory In kernel32 As Heap2Str;
STRING @Dest, Integer Src, Integer nLength