Page 1 of 3

internet ip address

Posted: Mon Nov 12, 2007 3:07 pm
by Richard Chidiak
Hello

Is there an esay way to know the INTERNET ip address from a computer ?

I think Enrico published solething on it some time ago, i did a search but did not find,

if anyone has a sample, thanks for the help

PS : I was previously using tip functions from xharbour but they do not work anymore, i plan to replace them with fwh functions,

Thanks for help,

Richard

Posted: Mon Nov 12, 2007 4:57 pm
by Silvio

Code: Select all

#include "FiveWin.ch"

function Main()
 
   local oSocket := TSocket():New( 2000 )
   local cIp := oSocket:cIPAddr 
   ?cIp
   oSocket:End() 
   
return NIL

Posted: Mon Nov 12, 2007 6:13 pm
by Antonio Linares
Richard,

This may help:
http://www.paulsadowski.com/sadowski/currentip.htm

Silvio,

Your code returns the local IP not the public IP on Internet

Posted: Mon Nov 12, 2007 6:22 pm
by Silvio
Antonio, sorry I not read good the text of Richard , sorry

Re: internet ip address

Posted: Mon Nov 12, 2007 9:23 pm
by Enrico Maria Giordano

Code: Select all

FUNCTION MAIN()

    LOCAL aHosts

    INETINIT()

    aHosts = INETGETHOSTS( NETNAME() )

    INETCLEANUP()

    ? aHosts[ 2 ]

    RETURN NIL
EMG

Posted: Mon Nov 12, 2007 9:38 pm
by Antonio Linares
Enrico,

I get this error:

Code: Select all

   Error description: Error BASE/1132  Bound error: array access
   Args:
     [   1] = A   { ... }
     [   2] = N   2
Using [ 1 ] then I get the local IP

Posted: Tue Nov 13, 2007 11:01 am
by Enrico Maria Giordano
Works fine here. Are you using Harbour or xHarbour?

EMG

Posted: Tue Nov 13, 2007 11:20 am
by Antonio Linares
Enrico,

xHarbour, as Harbour does not provide those functions afaik

Posted: Tue Nov 13, 2007 11:24 am
by Rossine
Hello,

I use like this:

Code: Select all


#include "fivewin.ch"

function MAIN

msgstop( getip() )

return NIL

Function GetIP()

Local cVar1

WsaStartUp()

cVar1 := GetHostByName( GetHostName() )

WsaCleanUp()

return cVar1
...or like this:

Code: Select all


FUNCTION MAIN() 

    LOCAL aHosts 

    INETINIT() 

    aHosts = INETGETHOSTS( NETNAME() ) 

    INETCLEANUP() 

*   ? aHosts[ 2 ] 
    ? valtoprg( aHosts )
Regards,

Rossine.

Posted: Tue Nov 13, 2007 5:13 pm
by driessen
I tried the several possibilities.

By using the example of Enrico, I got the same error as Antonio.

The first example of Rossine returns my local IP.

The second example returns something like this :
M->__ValToPrg_Array := Array(1)
M->__ValToPrg_Array[1] := "192.168.1.101"
which is my local IP too.

Posted: Tue Nov 13, 2007 6:21 pm
by Enrico Maria Giordano
driessen wrote:By using the example of Enrico, I got the same error as Antonio.
I don't understand. Do you want my EXE to test it there?

EMG

Posted: Wed Nov 14, 2007 3:15 am
by jose_murugosa
EnricoMaria wrote:
driessen wrote:By using the example of Enrico, I got the same error as Antonio.
I don't understand. Do you want my EXE to test it there?

EMG

Code: Select all

FUNCTION MAIN() 

   LOCAL aHosts 

   INETINIT() 
   aHosts = INETGETHOSTS( NETNAME() ) 
   INETCLEANUP() 
   MsgInfo( aHosts[ 2 ] )     <<<< aqui el cambio

RETURN NIL
Enrico I prove your sample and change ? with MsgInfo and then it works perfectly

Posted: Wed Nov 14, 2007 6:49 am
by Antonio Linares
José,

Code: Select all

   Error description: Error BASE/1132  Bound error: array access
   Args:
     [   1] = A   { ... }
     [   2] = N   2

Posted: Wed Nov 14, 2007 8:41 am
by Richard Chidiak
Antonio,

Unfortunately all the samples above retreive the Local ip code not the internet one

I have a function that retreives a html page and reads it, the problem it is not a 100% secure function as the page might change....

This is why i was looking for a replacement,

here is the function anyway,

function getmyip()
LOCAL aEol := { Chr(13) + Chr(10), Chr(10) }, ;
ctr := 0, ;
lok := .f., ;
myip := space(80), ;
sLine,cfile

cfile := CURDRIVE() + ":\" + CURDIR() + "\myip.txt"

IF FILE(CFILE)
ERASE (CFILE)
ENDIF
oUrl := tURL():New( "http://www.adresseip.com/" )
IF Empty( OUrl )
MSGINFO("URL NOT FOUND")
RETURN NIL
ENDIF

oclient:=tipclienthttp():new(oUrl)

IF Empty( oClient )
MSGINFO("Invalid url ")
RETURN NIL
ENDIF

oClient:nConnTimeout := 20000
IF oClient:Open( oUrl )
IF ! oClient:ReadToFile( cFile )
MSGINFO("Generic error in writing." + cFile)
ENDIF
oClient:Close()
endif

IF FILE(CFILE)
hFile := FOPEN(CFILE)
WHILE HB_FReadLine( hFile, @sLine, aEol ) == 0
ctr++
IF subs(ALLTRIM(sLine),1,8) == "<SPAN ID" // ip is next line
ctr := 1
lok := .t.
ENDIF
IF CTR = 2 .AND. lok
myip := sline
ENDIF
END
fclose(hFile)
erase (cfile)
ENDIF
return ALLTRIM(myip)

Richard

Posted: Wed Nov 14, 2007 9:10 am
by Luca
func GetMyIP( cURL )
#include "tip.ch"
local cMyIP := ""
local oUrl, oClient
cURL := if( empty(cURL), "http://www.whatismyip.org/", cURL )
oUrl := tURL():New( cURL )
if !empty( oUrl )
oClient := tIPclient():new( oUrl )
if !empty( oClient )
oClient:nConnTimeOut := 20000
if oClient:Open( oUrl )
cMyIP := oClient:Read(80)
oClient:Close()
endif
endif
endif
return cMyIP