Page 1 of 2

My local IP address

Posted: Thu Oct 05, 2017 2:14 pm
by guylouis
Hi all,

I use the well known code to get my local IP address: the hostNameToAddress of the hostName

This is working on Mac and PC, but always give 127.0.0.0 on Androïd.

An idea?

Thanks

Re: My local IP address

Posted: Thu Oct 05, 2017 4:06 pm
by jacque
Be sure that the Internet permission is selected in the Android pane in standalone settings. Also check the Inclusions pane to be sure internet is selected there too, and TSNet as well.

Edit: the hostName on my Android device always returns "localhost". I don't know if this is protected information on Android or a bug in LC.

Re: My local IP address

Posted: Thu Oct 05, 2017 5:16 pm
by FourthWorld
This comes up a lot, and depending on a wide range of factors can be non-trivial.

Let's explore the use-case to find the best solution: Why does your app need the device's local IP?

Re: My local IP address

Posted: Thu Oct 05, 2017 11:09 pm
by guylouis
Hi,

In fact, I don't need the local IP address but the IP address of my server (I can easily discover it from the first)..

I have an application which uses sockets to communicate with a server.

Here is a part of my code:

put "192.168.1.4" into ServerIP
put 9001 into bPort
put ServerIP & ":" & bPort into tSocket
open socket to tSocket
write something to socket tSocket with message "dataSent"
read from socket tSocket until theEnd

If my app runs on a PC or a Mac, I can use "hostNameToAddress of the hostName" to receive "192.168.1.4".

But on Androïd, I receive 127.0.0.0... And thus I must hardcode the IP address...

Best

Re: My local IP address

Posted: Thu Oct 05, 2017 11:20 pm
by guylouis
Hi Jacqueline,

The Internet permission is selected in the Androïd pane.

The inclusions field is empty. How do I fill it?
In the general panel, I see two Radio buttons "Search for required inclusions..." and "Select inclusions..."
The "Search..." is checked.
But the result is the same if I choose "Select".
How can I obtain "Internet" and "TSNet"?

Thanks

Re: My local IP address

Posted: Thu Oct 05, 2017 11:39 pm
by jacque
The "Select inclusions" radio button should enable the Extensions tab. In there you can choose all the libraries and widgets the app requires. In general I only use this manual method because searching doesn't always find everything.

Re: My local IP address

Posted: Thu Oct 05, 2017 11:43 pm
by FourthWorld
guylouis wrote:
Thu Oct 05, 2017 11:09 pm
Hi,

In fact, I don't need the local IP address but the IP address of my server (I can easily discover it from the first)..

I have an application which uses sockets to communicate with a server.
DNS is usually cached, so subsequent calls to a domain carry no penalty.

What prevents you from using the domain name?

Re: My local IP address

Posted: Fri Oct 06, 2017 12:39 pm
by guylouis
FourthWorld wrote:
Thu Oct 05, 2017 11:43 pm
What prevents you from using the domain name?
Sorry, but it works on my Mac and my PC but not in Androïd.

Mac:
answer the hostname -> iMac-de-Guy-2.lan
answer hostnametoadress(the hostname) -> 192.168.1.4 = my IP

PC:
answer the hostname -> Guy-PC
answer hostnametoadress(the hostname) -> 192.168.1.4 = my IP

Androïd:
answer the hostname -> localhost
answer hostnametoadress(the hostname) -> 127.0.0.0

Re: My local IP address

Posted: Fri Oct 06, 2017 3:37 pm
by MaxV
Android:

Code: Select all

put URL "https://wtfismyip.com/text" into temp
answer ("Your current IP is " & temp)
:mrgreen:

Re: My local IP address

Posted: Fri Oct 06, 2017 9:41 pm
by mrcoollion
One can also use tsNetGetSync for many rest API 's.
I used it e.g. for getting currency exchange information and stock information.

Make a button with the code below and make a field named "TheAnswer"
This rest API will get your internet known IP adres.

Read the dictionary for more information.

Code: Select all

on mouseUp
    put "" into  fld "TheAnswer"
    tsNetInit
    tsNetSetTimeouts 5,5000, 5000, 5000, 0, 0
    put "https://api.ipify.org?format=json" into tGetString
    put tsNetGetSync(tGetString, tHeaders, tRecvHeaders, tResult, tBytes) into tTheAnswer
    tsNetClose
    put tTheAnswer into fld "TheAnswer"
end mouseUp
Have fun,

Paul

Re: My local IP address

Posted: Fri Oct 06, 2017 9:42 pm
by guylouis
MaxV wrote:
Fri Oct 06, 2017 3:37 pm
Android:

Code: Select all

put URL "https://wtfismyip.com/text" into temp
answer ("Your current IP is " & temp)
:mrgreen:
I knew that. But my application runs in an environment where some users have only a simple WIFI router. Thus, no access to Internet.

Furthemore, this code gives the external IP address (i.e. 253.128.166.xxx) and not the internal IP address (i.e. 192.168.1.4).

Thanks

Re: My local IP address

Posted: Fri Oct 06, 2017 9:51 pm
by guylouis
mrcoollion wrote:
Fri Oct 06, 2017 9:41 pm
One can also use tsNetGetSync for many rest API 's.

Code: Select all

on mouseUp
    put "" into  fld "TheAnswer"
    tsNetInit
    tsNetSetTimeouts 5,5000, 5000, 5000, 0, 0
    put "https://api.ipify.org?format=json" into tGetString
    put tsNetGetSync(tGetString, tHeaders, tRecvHeaders, tResult, tBytes) into tTheAnswer
    tsNetClose
    put tTheAnswer into fld "TheAnswer"
end mouseUp
Have fun,

Paul
Thanks Paul, but I make the same reply as before: some of my users have only a WIFI router and not access to Internet.

Re: My local IP address

Posted: Fri Oct 06, 2017 11:49 pm
by FourthWorld
guylouis wrote:
Fri Oct 06, 2017 12:39 pm
FourthWorld wrote:
Thu Oct 05, 2017 11:43 pm
What prevents you from using the domain name?
Sorry, but it works on my Mac and my PC but not in Androïd.
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?

Re: My local IP address

Posted: Sat Oct 07, 2017 12:05 pm
by guylouis
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...

The server is always on a PC or a Mac.
The clients are always on an Androïd tablet.
The communication protocol uses sockets.
Imagine the local address of the server is "192.168.1.4".
Thus, on the clients, I must use the code: "open socket to serverIP address+ port", i.e. open socket "192.168.1.4:9001"

1) If the clients runs on a PC or a Mac, there is no problem.

First I must use hostNameToAddress of the hostName to discover the IP address of the client, it gives me for example "192.168.1.8".
Then, I make a loop of open sockets to "192.168.1.x" for x=1...255 and I will discover the address of the server...
No problem to communicate...

2) Now, if the client runs on Androïd, the problem arises with the same code.

The hostname() gives me "localhost" and hostNameToAddress of the hostName gives me "127.0.0.1".

No open socket loop works to discover "192.168.1.4"... This is my problem!

Since a long time, I use this solution to the problem:

The first time the program runs, it asks the user to give the address of the server. And save it somewhere.
Then all works fine.
The next times, the programs loads this address and all works fine too.

But... It means the PC/Mac user must fix the IP address.
And, if the server address changes, the client must then ask the new IP address.

It would be fine, if I can find a solution which works on Mac/PC and Androïd.

There are theoretically shell solutions, but shell is not available on mobiles.

Is it now clear?

Tanks

Re: My local IP address

Posted: Sat Oct 07, 2017 6:41 pm
by mrcoollion
Hi gl,

I now understand what you need (sorry, i am also very bad in reading manuals too :oops: ) and have been testing on my android phone.

You Are correct that :
'put the hostname into tThisDeviceHostName' gives 'localhost' which should be the the actual device name of the mobile device
then of-course 'put the hostNameToAddress of tThisDeviceHostName into tHostIPAddress' gives the localhost adres 127.0.0.0 (which is not what we are looking for).

I have tried to resolve a DNS name of one of my servers at home ' put the hostNameToAddress of "MYHOMESERVER_DNSNAME" ' which works fine in windows but gives no answer in android.
In my opinion this is also wrong.

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...

Kind regards,

Paul