FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour internet ip address
Posts: 130
Joined: Sat Oct 08, 2005 09:38 PM
internet ip address
Posted: Fri Sep 17, 2010 05:01 PM
Hi all,
I can get my external ip address with the code below.
I wonder if there is another way to get the external ip without connecting to any site.

Birol Betoncu

Code (fw): Select all Collapse
#include "fivewin.ch"
function main()
LOCAL cRetIP:=''
   RUNURL("http://www.whatismyip.com/automation/n09230945.asp", @cRetIP)
   msginfo(cRetIP)
RETURN nil


FUNCTION RUNURL( cUrl, cErr )
LOCAL oUrl, oCli, lOk := .F.
    cErr = ""
    BEGIN SEQUENCE
        oUrl = TUrl():New( cUrl )
        IF EMPTY( oUrl ); BREAK; ENDIF
        oCli = TIPClientHttp():New( oUrl )
        IF EMPTY( oCli ); BREAK; ENDIF
        oCli:nConnTimeout = -1
        IF !oCli:Open(); BREAK; ENDIF
        cErr = CVALTOCHAR( oCli:ReadAll() )
        lOk = !EMPTY( oCli:cReply ) .AND. "OK" $ UPPER( oCli:cReply )
        oCli:Close()
    END SEQUENCE
RETURN lOk
Birol Betoncu
birol.betoncu@gmail.com
Using Harbour, FWH 19.05, BCC7
Posts: 1335
Joined: Fri Jun 13, 2008 11:04 AM
Re: internet ip address
Posted: Sat Sep 18, 2010 05:46 AM
Try this code
Code (fw): Select all Collapse
#include "FiveWin.Ch"
//--------------------------//
FUNCTION Main()

   LOCAL oHttp
   LOCAL cVar := ""

   TRY
      oHttp := CreateObject("winhttp.winhttprequest.5.1")
      oHttp:Open("GET","http://whatismyip.com/automation/n09230945.asp",.F.)
      oHttp:Send()
      cVar := oHttp:ResponseText()
   CATCH
   END

   If !empty(cVar)
        MsgInfo("Your External IP is : "+cVar)
   else
        MsgInfo("Unable to retrieve Your External IP")
   Endif
   
Return NIL


Birol Betoncu wrote:I wonder if there is another way to get the external ip without connecting to any site.


As far as I know there is no way for the computer to know its external IP address. Routers translate and hide the external address from each node connected to it. The only thing (easiest way) you can do is connect with a site that will relay the information to you, because it communicates with your 'external' IP address. But this is not a reliable solution because the service provider may not continue the service for long time, they may change the ResponseText() format, may change their URL etc.

The best way to do this is to put a small php code on your webserver which returns the IP address on a webpage, and all your applications can query this webpage to retrieve your external IP with a code similiar which I have posted above.

For eg. The .php code would be
Code (fw): Select all Collapse
$ip=@$REMOTE_ADDR;
echo "<b>IP Address= $ip</b>";


The above code will display this IP address= xxx.xxx.xxx.xxx

Regards

Anser
Posts: 130
Joined: Sat Oct 08, 2005 09:38 PM
Re: internet ip address
Posted: Sat Sep 18, 2010 07:46 PM

Thanks for your reply. It is very helpfull.

Birol Betoncu

Birol Betoncu
birol.betoncu@gmail.com
Using Harbour, FWH 19.05, BCC7

Continue the discussion