My local IP address

The place to discuss anything and everything about running your LiveCode on Android

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9801
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: My local IP address

Post by FourthWorld » Sat Oct 07, 2017 8:57 pm

guylouis wrote:
Sat Oct 07, 2017 12:05 pm
FourthWorld wrote:
Thu Oct 05, 2017 11:43 pm
I think I didn't pose my question clearly: why use the IP address at all? Why not make the server calls using the domain name?
I think you don't really understand my problem...
Yes, that much is clear. :)

After reading your post it seems we could rephrase the question as:

"How can I implement auto-discovery of other devices on the local network?"

Is that correct?

If so, the answer is not straightforward. Indeed, this is why discovery protocols like Bonjour were established, to account for the many factors that can make discovery difficult.

localHost is designed for loopback, allowing for socket connections between processes on the same machine. In some cases (as apparently with your Mac and Windows boxes) the localHost address may be the same as the local network address. But in other cases it may differ. For example, on my Linux box here localHost gives me the loopback address ("127.0.0.1").

In short, localHost is not the answer. In fact, if there's a bug to report it may be that Mac and Win versions of LC may not be properly reporting localhost, since 127.0.0.1 is most common.

For discovery, in some cases you may be able to make a UDP broadcast. For that to work you would need to make sure the tablet is using wifi rather than a external cellular network, and that the wifi router is configured to allow UDP, and that software firewalls on the PCs will allow UDP.

This pair of stacks may help with implementing a UDP broadcast solution:
http://www.tweedly.org/showpage.lc?page=samples

However, apparently not all systems allow UDP broadcast:
viewtopic.php?f=11&t=8176#p38711

Most commonly this problem is solved with an intermediary server to facilitate dynamic DNS, but since your environment doesn't have Internet access that's a no-go.

Maybe the simplest solution can be based on that constraint: because there is no Internet all devices use DHCP from one router, so you may be able to loop through a fairly small number of addresses based on common router defaults. Most will use 192.168.1-254 (255 is usually reserved), with fewer defaulting to 192.168.0-254, and a very small number that default to 10.1.1/24 or 10.0.0/24.

If this is a system you manage you can set the range in your router, though yours is apparently already using the most common range.

If this is a system you want to deliver for others to use with their own routers, and that customer base is not expected to be able to know how to set their router's DHCP range, you may extend your loop to try the other ranges in succession.

But I'd wager you'll get 99% of devices found easily just looping in one range, from 192.168.1.0 to 192.168.1.254.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

mrcoollion
Posts: 709
Joined: Thu Sep 11, 2014 1:49 pm
Location: The Netherlands

Re: My local IP address

Post by mrcoollion » Sun Oct 08, 2017 1:39 pm

FYI

I just reported this issue as a bug (20530).
Also added command 'the Address' because this should also give the device name in the first item but instead it states 'android'.

Regards,

Paul

LCMark
Livecode Staff Member
Livecode Staff Member
Posts: 1206
Joined: Thu Apr 11, 2013 11:27 am

Re: My local IP address

Post by LCMark » Mon Oct 09, 2017 7:45 am

I suggest trying 'the networkInterfaces' global property - this gives you the IP address of each network interface present on the local device/system.

Desktop machines typically (but only if configured to - which Mac / Win do by default) add a DNS entry locally which maps the name you have configured for the machine in your settings to the 'default' network interface on the machine. This is why you can resolve the hostname locally.

In contrast, mobile devices do not have a host name typically - and rarely have a default network interface as it could be the wifi network or cellular network. Therefore the default behavior (as it is on Linux in this case - if the system has not been configured) is to return 'localhost' for the host name.

In terms of 'the address' - on Windows, we use the system call 'gethostname()' and on Mac/Linux we use 'uname()' - on the mobile devices it is a fixed string. We could change it to use uname() / gethostname() on iOS/Android - but you would still get 'localhost' - as that is what you get with the hostName function (which also uses these calls). Therefore, I'd try 'the networkInterfaces' approach :)

mrcoollion
Posts: 709
Joined: Thu Sep 11, 2014 1:49 pm
Location: The Netherlands

Re: My local IP address

Post by mrcoollion » Mon Oct 09, 2017 9:39 am

The only thing i got working is getting the device IP address.
It does assume that there are only two interface addresses in the list :?

Code: Select all

on mouseUp pMouseButton
    get the networkInterfaces
    put it into tInterfacesList
    answer "IP Addresses are: " & tInterfacesList
    put the number lines in tInterfacesList into tNumberIpAdresses
    repeat with tLineNbr=1 to tNumberIpAdresses
        put line tLineNbr of tInterfacesList into tIPAddress
        if tIPAddress is not "127.0.0.1" 
       then 
           put tIPAddress into tDeviceIP
          exit repeat
        end if
    end repeat
    answer "Device IP Address is probably: " & tDeviceIP
end mouseUp
It at least could give an IP range for an open socket loop ? (I did not work with sockets yet)
Resolving any IP to DNS name or DNS name to IP does not work (on android).


Can LiveCode 9.x help?
However I do think in LiveCode 9 it should be possible (the Infinite LiveCode project) to get more information with help of the following information.
https://developer.android.com/training/ ... y/nsd.html
https://developer.android.com/reference ... nager.html
But because I am a new-bee on LiveCode and do not know any Java this is to far reached for me :( .


The answer on my bug report from Mark is as follows (thank you LCMark).
------------------------------------------------
Comment # 1 on bug 20530 from Mark Waddingham
Hi Paul,

As mentioned no the forum post - you can use 'the networkInterfaces' global
property to get the list of network addresses that the device has.

The hostName of mobile devices doesn't typically get set by the system as it
does on desktop machines (as far as I'm aware) - so it returns the BSD socket
default which is localhost. (i.e. Unlike a desktop machine, mobile devices
don't add a hostname entry to the local DNS resolution process).

Apps like 'Whats my IP' probably use a request to a remote server, and report
back the IP which they were sent as sender - either that or they just return
the IP address(es) for each network interface, which is the information
provided by 'the networkInterfaces'.

Warmest Regards,

Mark.
------------------------------------------------

guylouis
Posts: 26
Joined: Mon Feb 01, 2016 11:45 pm

Re: My local IP address

Post by guylouis » Mon Oct 09, 2017 6:27 pm

mrcoollion wrote:
Sat Oct 07, 2017 6:41 pm
I would suggest you submit a bug report because in my opinion these are very important commands for network usage or they should at least give us another way to get the device IP-Address and hostname and the abillity resolve other hostnames on android (and ios).

let us know the results...
Where do you want I submit this bug report? Difficult to imagine nobydy had this problem since the many years Androïd exist... :D

Best, Guy

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9801
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: My local IP address

Post by FourthWorld » Mon Oct 09, 2017 6:59 pm

guylouis wrote:
Mon Oct 09, 2017 6:27 pm
mrcoollion wrote:
Sat Oct 07, 2017 6:41 pm
I would suggest you submit a bug report because in my opinion these are very important commands for network usage or they should at least give us another way to get the device IP-Address and hostname and the abillity resolve other hostnames on android (and ios).

let us know the results...
Where do you want I submit this bug report? Difficult to imagine nobydy had this problem since the many years Androïd exist... :D
Already resolved - look one post up. The networkInterfaces will provide what the OP is looking for.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

guylouis
Posts: 26
Joined: Mon Feb 01, 2016 11:45 pm

Re: My local IP address

Post by guylouis » Fri Oct 20, 2017 4:03 am

Hi,

The networkInterfaces solution is good on Windows and OS X.

But not on Androïd...

answer the networkInterfaces gives empty!

Best, Guy

mrcoollion
Posts: 709
Joined: Thu Sep 11, 2014 1:49 pm
Location: The Netherlands

Re: My local IP address

Post by mrcoollion » Fri Oct 20, 2017 11:02 am

Hi Guy,

That is strange because on my android device it gives me 2 IP addresses. 127.0.0.1 and the IP address my phone got from my DHCP router.

I will attach my test stack (zipped) .
TestAndroidStuffV01.zip
Test network interfaces
(3.12 KiB) Downloaded 255 times
press button Test the Interfaces to get the IP addresses I mentioned.
Make sure that you add the inclusions: internet, JSON Library , tsNet, Answer Dialog, Ask Dialog.

I use LiveCode 8.1.6.

Good luck and let us know if it still not works.

Paul

TorstenHolmer
Posts: 57
Joined: Mon Oct 28, 2013 1:23 pm
Location: Dresden, Germany

Re: My local IP address

Post by TorstenHolmer » Sat May 15, 2021 5:45 pm

Hi,

I had the same problem for my project and I discovered the ways to find the internal (WLAN) IP for the Livecode device on Windows, MacOS and Android. Here ist the tested handler for them: put get_internal_IP()

Code: Select all

function get_internal_IP
   
   put the platform into tPlatform
   switch tPlatform
      
      case "MacOS"
         
         put "ifconfig -a | grep" && quote & "inet " & quote into tCommand
         put shell(tCommand) into tIP
         put word 2 of line 2 of tIP into tInternal_IP
         break
         
      case "Win32"
         
         set the hideconsolewindows to true
         put shell("ipconfig") into tData
         put line lineoffset("IPv4",tData) of tData into tData
         put the last word of tData into tInternal_IP
         break
         
      case "android"
         
         get the networkInterfaces
         put it into tInterfacesList
         put the number lines in tInterfacesList into tNumberIpAdresses
         repeat with tLineNbr=1 to tNumberIpAdresses
            put line tLineNbr of tInterfacesList into tIPAddress
            if tIPAddress is not "127.0.0.1" then 
               put tIPAddress into tInternal_IP
               exit repeat
            end if
         end repeat
         break    
   
   end switch
   return tInternal_IP

end get_internal_IP
Cheers
Torsten

TorstenHolmer
Posts: 57
Joined: Mon Oct 28, 2013 1:23 pm
Location: Dresden, Germany

Re: My local IP address

Post by TorstenHolmer » Sat May 15, 2021 10:09 pm

I discovered that Livecode does not need the shell commands, you can get all relevant information with networkInterfaces :D

Code: Select all

function get_internal_IP
   put the networkInterfaces into tInterfacesList
   repeat for each line tInternal_IP in tInterfacesList
      if tInternal_IP is "127.0.0.1" then 
         next repeat
      else if tInternal_IP is "0.0.0.0" then
         next repeat
      else
         return tInternal_IP
      end if
   end repeat
end get_internal_IP
Cheers
Torsten

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7210
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: My local IP address

Post by jacque » Mon May 17, 2021 7:34 pm

I found this in my script collection:

Code: Select all

function localIP -- returns IP address(es) of local computer
  set the itemDel to colon
  return hostNameToAddress(item 1 of the address)
end localIP
Pretty simple.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

guylouis
Posts: 26
Joined: Mon Feb 01, 2016 11:45 pm

Re: My local IP address

Post by guylouis » Mon Dec 13, 2021 12:37 pm

Hi Torsten and Jacqueline,

I just read your last posts. Everything is working fine. Thank you very much…

Best, Guy

trevix
Posts: 950
Joined: Sat Feb 24, 2007 11:25 pm
Location: Italy
Contact:

Re: My local IP address

Post by trevix » Wed Sep 06, 2023 4:06 pm

I resume this old conversation, because I have some doubts.
Running this on the Mac,connected both to wifi and ethernet:

Code: Select all

on mouseUp pButtonNumber
     answer "networkInterface:" & cr & the networkinterfaces & cr & "JacqueFunct:" & cr & localIP()
end mouseUp
--Jacque  local ip function
function localIP -- returns IP address(es) of local computer
     set the itemDel to colon
     return hostNameToAddress(item 1 of the address)
end localIP
Somehow the Jacque function returns all doubled IPs, like this
192.168.1.100 --ethernet
192.168.1.100 --ethernet
192.168.1.115 --Wifi
192.168.1.115 --Wifi
This ain't a problem of course, but
QUESTION 1= I wonder how to distinguish the local ethernet IP from WiFi IP.
So much because if I also connect the iPhone to the Mac with the USB, the same code on the Mac retuns this:
networkInterface:
127.0.0.1
192.168.1.115
192.168.1.100
169.254.102.194 --?
169.254.52.177 --?
JacqueFunct:
127.0.0.1
169.254.102.194
169.254.52.177
192.168.1.115
192.168.1.100
192.168.1.115
192.168.1.100
So, now on the Mac I get:
- the usual self 127.
- the ethernet IP
- the WifiIP
- 2x new IPs (.194 and .177)
QUESTION 2= what are those 2 new IP represent? USB what?

Now, I install the code on a hardware iPhone (remote debug disabled). On the iPhone the returns is:
networkInterface:
127.0.0.1
10.76.204.89 --cell data?
192.168.1.106 --I know this is WiFI, because I looked at the OS wifi setting
169.254.152.128 --? Does it refer somehow to the USB connection?
JacqueFunct:
--nothing
QUESTION 3= the iOS knows the local wifi IP of the connection. Is there a way so that also LC can get it?

Now I enable the remote debugger and install the standalone again:
...sorry, remote debugger, for a change, doesn't work.
I am sure anyway that it would have added another IP...

If they didn't want me to find out my local IP, they just had to tell me... :D
Trevix
OSX 14.3.1 xCode 15 LC 10 DP7 iOS 15> Android 7>

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7210
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: My local IP address

Post by jacque » Wed Sep 06, 2023 6:07 pm

I just tried it on my Android phone and my function doesn't return anything there either. I don't know why.

The network interfaces just tell you what's available but not which one is in use.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Post Reply

Return to “Android Deployment”