Code archives/Networking/Get PC's local IP address

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

Download source code

Get PC's local IP address by BlitzSupport2012
Just a quickie that returns a string containing your PC's local (LAN) IP address, eg. mine returns "192.168.0.2".
Function LocalIP:String ()
	Return DottedIP (HostIp ("", 0))
End Function

Print LocalIP ()

Comments

BlitzSupport2012
Updated to handle erroneous result when non-localhost domains have been redirected to 127.0.0.1 via HOSTS file; thanks to _PJ_!


Captain Wicker (crazy hillbilly)2012
Works on OSX 10.8.2
Returned my iMac/server IP address just fine. 192.168.1.100


_PJ_2012
James, Have you tried leaving the HostIP blank to default to the Local Machine?

(Sorry B3d code, I HOPE it should still operate for BMax, similar commands at least...
Function LocalIP$()
	n=CountHostIPs("")
	sIP$=HostIP(1)
	Return DottedIP(sIP)
End Function

Print LocalIP$() 




_PJ_2012
.


BlitzSupport2012
Thanks, PJ -- I had to edit that a little bit, but this seems to work on my XP install:

Function LocalIP:String ()
	ip = HostIp ("", 0)
	Return DottedIP (ip)
End Function

Print LocalIP$() 


I'll try it on my Windows 7 setup later and update the code it if works.


BlitzSupport2012
That works on my XP/Win7 PC and my separate Win7 PC, so updated main code.


_PJ_2012
Good to know! :)


Code Archives Forum