Code archives/Networking/GetLocalIP function
This code has been declared by its author to be Public Domain code.
Download source code
| |||||
A cross-platform version of LocalIP here http://www.blitzmax.com/codearcs/codearcs.php?code=3004 I couldn't find this anywhere when I searched. Intended to be used with the gnet lobby, see here http://www.blitzmax.com/codearcs/codearcs.php?code=2818 or here http://www.blitzmax.com/codearcs/codearcs.php?code=1413 The idea is you send your pc's local ip name or address (not localhost/127.0.0.1) to the server_name slot on the gnet lobby. If you send the ip name you can convert it back to ip address by HostIps, as in the example here. This method is a substitute for 'real' broadcasting (message sent to all lan pcs). Having read a bit about "leaking" of ip addresses, I believe the external ip is the one that is most dangerous to share but this changes with every connection so I don't think it's really important to encrypt any ips sent to the gnet lobby. | |||||
' Get pc's local (lan) ip, win/mac/linux SuperStrict Framework Brl.EventQueue Import Brl.StandardIO Import pub.freeprocess Import brl.socket ' example Local ip:String=GetLocalIP() Print "host name:"+HostName(HostIp(ip)) ' get pc's local name from ip Local ips:Int[] = HostIps(HostName(HostIp(ip))) ' get pc's local ip from name For Local i:Int = EachIn ips If i=HostIp(ip) Then Print "host ip:"+DottedIP(i) Next Function GetLocalIP:String() ?Win32 Return DottedIP(HostIp("",0)) ?Not Win32 Local proc:TProcess=TProcess.Create("ifconfig",0) Local ps:String,is:String,spi:Int,ssi:Int,sci:Int While proc.Status() ' get ifconfig results If proc.pipe.ReadAvail() ps=proc.pipe.ReadString(proc.pipe.ReadAvail()) EndIf Wend While spi>-1 ' get pc's local ip spi=ps.Find("inet ",spi+1) For sci=spi+5 To spi+15 If ps[sci..sci+1]>="0" And ps[sci..sci+1]<="9" Then Exit Next ssi=ps.Find(" ",sci) is=ps[sci..ssi] If is<>"127.0.0.1" And is<>"" Then Exit Wend Return is ? End Function |
Comments
None.
Code Archives Forum