Code archives/Networking/What is my public IP?

This code has been declared by its author to be Public Domain code.

Download source code

What is my public IP? by ZJP2009
How to get your public IP on the net
;**********************************
;* Get Your Public IP
;**********************************

Print "My public IP is "+WanIP$()
WaitKey

Function WanIP$()
Local www,x$,ip$
    www=OpenTCPStream( "www.whatismyip.org",80 )
    If Not www Return "127.0.0.1"
    WriteLine www,"GET / HTTP/1.1"
    WriteLine www,"User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"
    WriteLine www,"Accept: */*"
    WriteLine www,""
    x$= ReadLine(www)
    x$= ReadLine(www)
    x$= ReadLine(www)
    ip$= ReadLine(www)
    CloseTCPStream www
    Return ip$
End Function

Comments

GIB3D2009
You made an error, I checked and it showed the IP on the 4th ReadLine(www) and not on ip$=ReadLine(www).

Print "My public IP is " + WanIP$()
WaitKey

Function WanIP$()
    www=OpenTCPStream( "www.whatismyip.org",80 )
    If Not www Return "127.0.0.1"
	
    WriteLine www,"GET / HTTP/1.1"
    WriteLine www,"User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"
    WriteLine www,"Accept: */*"
    WriteLine www,""
	
	For skip = 0 To 2
		ReadLine(www)
	Next
	
    ip$ = ReadLine(www)
	
    CloseTCPStream www
    Return ip$
End Function



_PJ_2009
Would I be right in thinking that using Directplay stuff built in to Blitz3D would end up giving just a local IP (192.168.xx.xx etc ) so this could be useful in getting around issues of multiplayer connections behind routers?


_Skully2009
Not really.. because you still need to configure your router to route the ports being used by the game the specific computers internal IP... Knowing your public IP is important yes.. but setting up the router properly is paramount.. of course you can always put the computer into the DMZ but that kind of defeats one of the main purposes of having a router.


xlsior2009
Would I be right in thinking that using Directplay stuff built in to Blitz3D would end up giving just a local IP (192.168.xx.xx etc ) so this could be useful in getting around issues of multiplayer connections behind routers?


Yes.

But you'll still need the steps mentioned in _Skully's post as well.


Yo! Wazzup?2009
Whatismyip.org stopped working for me recently :(
EDIT: Never mind, it's my virus scanner. It hates everything, including that website.


ZJP2009
Hi,
Another one ;)

;**********************************
;* Get Your Public IP
;**********************************

Print "My public IP is " + WanIP$()
WaitKey
End

Function WanIP$()
Local i,l$,ip$,c$
    www=OpenTCPStream( "checkip.dyndns.org",80 )
    If Not www Return "127.0.0.1"
	
    WriteLine www,"GET / HTTP/1.1"
    WriteLine www,"User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"
    WriteLine www,"Accept: */*"
    WriteLine www,""
	
	For i = 0 To 7
		ReadLine(www)
	Next
	l$=ReadLine(www)
	find=Instr(l$,"Current IP Address:",1)+19
	ip$=""
	For i=1 To 14
		c$=Mid$(l$,find+i,1)
		If c$<>"<" Then ip$=ip$+c$
		If c$="<" Then Exit
	Next
	
    CloseTCPStream www
    Return Trim$(ip$)
End Function



Nate the Great2009
is there a way to detect your local ip?


Luke1112009
'IPCONFIG' in Command Prompt.
'IFCONFIG' in UNIX.
ipchicken.com for Public.
Id also try 'TRACERT google.com' as well...
:)
LUKE


xlsior2009
That works, but ofd course that doesn't help your program... You really don't want to depend on external tools like that anyway.

The proper way to get your local IP address:
Function HostIps:Int[]( HostName$ ) 
Returns Array of host ips, or Null if host not found  
Description Get all ip addresses for a host name 


for hostname$, you can probably just use 'localhost'

Don't remember the format the IPs are returned in, you may also need to use DottedIP$() to convert it back to a human-readable IP address.


ZJP2009
>>>is there a way to detect your local ip?

;) From the manual.

; First call CountHostIPs (blank infers the local machine)
n = CountHostIPs("")
; n now contains the total number of known host machines.
; Obtain the internal id for the IP address
ip = HostIP(1)

; Convert it to human readable IP address
ipaddress$ = DottedIP$(ip)

Print "Dotted IP Test"
Print "=============="
Print ""
Print "Internal Host IP ID:" + ip
Print "Dotted IP Address:" + ipaddress$
Print ""
Print "Press any key to continue"

WaitKey()

End



Code Archives Forum