Getting real IP of mobile device not working but...

Bringing the internet highway into your project? Building FTP, HTTP, email, chat or other client solutions?

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

tonymac
Posts: 23
Joined: Thu Jan 05, 2012 9:17 pm

Getting real IP of mobile device not working but...

Post by tonymac » Mon Apr 18, 2016 2:07 pm

Scoured the forums and the internet for many many too many days looking for a bullet proof way for LC 8 to give me the real IP address of a mobile device running the app. The real IP address of my android phone is "10.xxx.x.xxx". Both of these handlers below return the correct IP addresses on desktop computers. Not so on mobile devices.

////////////////////////
-- ON MOBILE DEVICES, THIS PUTS "127.0.0.1" INTO FIELD "MyIP" BUT THIS WORKS ON DESKTOPS
on mouseup
put "My IP Address: " & the hostNameToAddress of (the hostname) into field "MyIP"
end mouseup

////////////////////////
-- ON MOBILE DEVICES, THIS PUTS "socket not open" IINTO FIELD"MyIP" BUT WORKS ON DESKTOPS
on getLocalIPAddress
put "255.255.255.255:6000" into tCheckAddress
open datagram socket to tCheckAddress
put the hostAddress of tCheckAddress into tMyIP
close socket tCheckAddress
put tMyIP into field "MyIP"
end getLocalIPAddress


////////////////////////
-- HAVE TRIED ALL THE CODE EXAMPLES OF USING PHP TO GET THE IP ADDRESS AS WELL.
-- I WAS WARNED BY MY UNIVERSITY NETWORKING EXPERTS THAT TRYING TO ACCESS
-- THIS USING PHP WAS RISKY AS ROUTERS CAN BE TEMPERMENTAL AND SOMETIMES
-- WITH FIREWALLS AND UPDATES, THINGS THAT USED TO WORK MIGHT NOT ANYMORE.
-- THIS IS WHAT I FOUND OUT TO BE TRUE AFTER MUCH TESTING AND I WAS NO LONGER
-- GETTING AN ACCURATE REAL IP ADDRESS FROM RUNNING PHP.

<?php
echo $_SERVER['REMOTE_ADDR'];
?>


Correct me if I'm wrong but in order for "chat" socket communication to happen between two mobile devices for example, an accurate real IP address of the "server" must be known. Otherwise when the client tries to open a connection, it will not work (by-the-way... both mobile devices are on the same network). When I manually enter the real server IP for the client to connect to on mobile devices, it works. But getting the real IP address of the mobile device is the conundrum.

And now the interesting part about this... The Chat server has been started up and accepting connections on a port on one of the mobile devices.

accept connections on port 1957 with message ServerClientConnected

A chat client is on the other mobile device. So there's no question, I manually enter the real IP address of the server onto the client. After running this below on the client mobile device to connect the client to the server,

open socket field "host" & ":1000" with message "ClientChatConnected"

The server instantly receives the connection request from the client through the "ServerClientConnected" handler on the server mobile device.

And the real thrill is that it correctly gives the REAL IP address of the client which is received by and stored on the server. I could be wrong but it appears that the "open socket" command does some sort of magic and is actually retrieving the real IP address of the client before it is sent and received by the server script accepting connections. So for after hundreds of test runs, the client has accurately returned the correct IP address EVERY time.

So the logical question is, "how come we don't have a command that will return the real IP address of mobile devices?", and by the way, I think the code is already there in the "open socket" command to do just that.

Hoping some networking expert at LC will investigate this. I don't know enough about viewing and trying to figure where code might be located in the open source version of LC. And even if I saw it, I probably would not understand it.

Sorry for kind of venting this out but not having a bulletproof way of getting the real IP address of a mobile device kills all of the "socket communication" mobile apps I want to create.

AxWald
Posts: 578
Joined: Thu Mar 06, 2014 2:57 pm

Re: Getting real IP of mobile device not working but...

Post by AxWald » Mon Apr 18, 2016 3:51 pm

Hi,
tonymac wrote:[...] I could be wrong but it appears that the "open socket" command does some sort of magic and is actually retrieving the real IP address of the client before it is sent [...]
Nope, it doesn't. The server sees where the packet comes from - and this hasn't to be your phone! There may as well be a NAT device between ...

For instance, atm "ipconfig" says my computer has 192.168.1.100.
MyIP.net though says I'd have 93.104.10.224 - nevertheless I see the site ;-)

With your mobile, you may be connected via 3G/4G or WiFi (different providers, different IPs). For your own device, fetching the HTML of, for instance, MyIP.net, and extracting your IP is easy.

Another way would be to create an account at a free DynDNS service, No-IP etc.
Have your app update the dyndns record at startup, this may be the best way.
Socket connections work fine with dyndns.

Have fun!
All code published by me here was created with Community Editions of LC (thus is GPLv3).
If you use it in closed source projects, or for the Apple AppStore, or with XCode
you'll violate some license terms - read your relevant EULAs & Licenses!

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

Re: Getting real IP of mobile device not working but...

Post by FourthWorld » Mon Apr 18, 2016 4:33 pm

If you're using a server for chat sessions and your server is already able to obtain what it needs to accept connections, why do your clients need to know their addresses? Just keep using the server that's already working and you should have a good solution that'll work well with most good firewalls.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

monte
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1564
Joined: Fri Jan 13, 2012 1:47 am
Contact:

Re: Getting real IP of mobile device not working but...

Post by monte » Thu Apr 28, 2016 10:54 pm

Hi Tony

I think there may be a case of XY problem here. I think it would help if you specified clearly what your app needs to do rather than what you feel is missing from LiveCode that is stopping you from doing what you want to do. For example, I have been confused on the point about whether the client and server are on the same LAN. That's a critical point because if they aren't and you aren't in a position to port forward to the server then you really need a completely different solution such as both parties connecting to a known intermediary to shuttle data between them. If the devices are all on the same LAN then as I said privately the standard way to deal with service discovery is bonjour/zeroconf.

For what it's worth I *think* what you are looking for is the networkInterfaces but with localhost (127.0.0.1) filtered out. If there's more than one line then you might tell clients to attempt to connect on all the others starting at the closest matching IP in their networkInterfaces.

Cheers

Monte
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

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

Re: Getting real IP of mobile device not working but...

Post by FourthWorld » Thu Apr 28, 2016 11:42 pm

@Monte: One addition to LC that would help certain types of P2P apps is UDP hole punching:
http://quality.livecode.com/show_bug.cgi?id=5097

@tonymac: is your app client-server or peer-to-peer? With client-server the client does not normally need to knows its public IP. What does your app do with that?

FWIW, here's a complete example of chat service with both client and server scripts:
http://lessons.livecode.com/m/4071/l/12 ... ng-sockets
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

monte
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1564
Joined: Fri Jan 13, 2012 1:47 am
Contact:

Re: Getting real IP of mobile device not working but...

Post by monte » Fri Apr 29, 2016 10:36 am

@FourthWorld adding support for binding to a specific local port doesn't look overly complicated. It is essentially just adding the syntax then adding another instance variable to MCSocket then changing `t_bind_addr.sin_port = 0;` to `t_bind_addr.sin_port = p_socket->local_port;`

It would probably be a good candidate for a community contribution I think considering my suspicion it's relatively unlikely that it would make its way to the top of the TODO list based on business value/number of users it would help etc.
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

Wevil
Posts: 1
Joined: Tue Oct 11, 2016 4:06 pm

Re: Getting real IP of mobile device not working but...

Post by Wevil » Tue Oct 11, 2016 4:08 pm

Had the same problem when using proxy servers. Due to the change ip server (advanced.name) was performing incorrect queries.

simon.schvartzman
Posts: 638
Joined: Tue Jul 29, 2014 12:52 am
Location: Brazil

Re: Getting real IP of mobile device not working but...

Post by simon.schvartzman » Thu Jul 20, 2017 5:24 pm

@FourthWorld, can you please elaborate on this:
If you're using a server for chat sessions and your server is already able to obtain what it needs to accept connections, why do your clients need to know their addresses? Just keep using the server that's already working and you should have a good solution that'll work well with most good firewalls.
How would the client know to which host to send the request if they do not know the server's IP address?

@all, any recommendations as how to get the IP address of the server taking into account it is going to be an iPhone connected via 3G/4G?

My idea is to have the server upload its IP address to a given file on Dropbox and then having the client retrieve the address in order to be able to establish the connection.

Any other ideas are welcomed.

Thanks
Simon
________________________________________
To ";" or not to ";" that is the question

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

Re: Getting real IP of mobile device not working but...

Post by mrcoollion » Thu Jul 20, 2017 6:35 pm

Just some Idea's Simon.

Normally a client first contacts a central host based upon the central hosts its DNS name which resolves the hosts IP address.
The client sends it own IP address and port number and reference name to the host which keeps it for communication purposes.
After the client has been connected to the host it can pick up an active clients IP address-list with reference names from this central host.
Because the client communicates to the internet first through a possible firewall the NAT setting of this firewall will let it through and normally also lets return traffic through to the same client IP and port.
So if you have a central host somewhere that can keep the connection information for all (allowed) clients. you should be able to communicate between those clients and the host.

In your case you need the host to be a mobile device (phone). I guess there are two options:
1) You use an app that assigns a fixed domain name to your Android phone even though the IP address can change. Use this DNS name in other devices for communication.
https://play.google.com/store/apps/deta ... com.dyndns
http://www.freedyn.it/
2) You can put a small server online that functions as a IP mediator which has or a fixed IP adres or also uses a DynDNS solution.
You could test this with an old PC or Maybe even a raspberry PI running on Livecode :D and put it into the DMZ of your personal firewall at home. Be aware that a computer in a DMZ (De-Militarized Zone) fully relies on it's own protection from internet evil!

This is what I would try. if it works I do not know ? Give it a try and let me know.
If it works make sure that you encrypt any data going over the internet.

Regards Paul.

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

Re: Getting real IP of mobile device not working but...

Post by FourthWorld » Thu Jul 20, 2017 8:09 pm

simon.schvartzman wrote:@FourthWorld, can you please elaborate on this:
If you're using a server for chat sessions and your server is already able to obtain what it needs to accept connections, why do your clients need to know their addresses? Just keep using the server that's already working and you should have a good solution that'll work well with most good firewalls.
How would the client know to which host to send the request if they do not know the server's IP address?
The scenario I described is a more traditional client-server setup, where the server is an external third device and has a domain name or fixed IP.
@all, any recommendations as how to get the IP address of the server taking into account it is going to be an iPhone connected via 3G/4G?

My idea is to have the server upload its IP address to a given file on Dropbox and then having the client retrieve the address in order to be able to establish the connection.
That sort of dynamic DNS solution works where you have control over the router's NAT, and the paitence to wade through the manual to set it up. Some of the more serious gamers are comfortable setting up port forwarding on their routers, but most consumers don't even know what NAT is. And in any well-managed business environment, all incoming connections will be blocked and beyond the control of any user.

Reliance on cell towers may mitigate some of this, provided the user is made of aware of the need to, and remembers to, turn off wifi when using your app.

But even then an app that binds to a port will remain beholden to the security constraints of the host OS and any firewalls that may come into play.

All that said, those constrains reflect my experience with desktop development using TCP. There may be options available for mobile which are both safer and easier for the user that I'm not yet familiar with.

While researching this question I came across this page on non-traditional networking using mesh networks over Bluetooth and Wifi radios - hopefully some of this may be useful for your project:
http://www.disruptivetelephony.com/2015 ... hones.html

Personally (and admitting up front a bias to stick with what I'm familiar with when security and/or usability may be at risk), for systems exposed to the open Internet I would be inclined to use a more traditional client-server setup, where devices communicate through a domain-based relay server, such as Paul described in his option #2 above. Outgoing connections are widely allowed, REST APIs relatively easy to define, and the latency unlikely to be much longer than with a more exotic solution.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

simon.schvartzman
Posts: 638
Joined: Tue Jul 29, 2014 12:52 am
Location: Brazil

Re: Getting real IP of mobile device not working but...

Post by simon.schvartzman » Thu Jul 20, 2017 8:26 pm

@Paul & @FourthWorld, many thanks for your hints and suggestions.

Just one more try: what if the connection of the client to the server is done via the server's Personal Hotspot capabilities. Will it help in any way?

Just for you to understand what I'm trying to achieve is the following:

- an iphoneA (the server) will be running full time taking pictures by itself one per hour (sharp) and uploading them to dropbox (thanks LC for Dropbox API V2 !)
- since this iphoneA will be in a somehow difficult to reach location (hanging from the roof of a room for instance) I want to be able to remotely, with iPhoneB, send an order to take a picture whenever I want to (being on the same room of course).

Thoughts?
Simon
________________________________________
To ";" or not to ";" that is the question

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

Re: Getting real IP of mobile device not working but...

Post by FourthWorld » Fri Jul 21, 2017 12:50 am

Does phoneA need to be a phone? If it's just a device with a camera mounted to a wall you can save a few hundred dollars on hardware using a Raspberry Pi Zero W, and have a much more flexible OS as well.
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: Getting real IP of mobile device not working but...

Post by mrcoollion » Fri Jul 21, 2017 11:53 am

Hi Simon,

I am in no means a LiveCode expert but I still think your best bets are:
1) Use a DNS service on both phone and communicate via Socket Communications in LiveCode.
Dictionary search for socket: https://livecode.com/resources/api/#livecode_script/%24
or
http://livecode.byu.edu/internet/IntroToSockets.php

2) Use your dropbox idea and poll dropbox every x seconds.

I also though about SMS but LiveCode has (as far as I know) no means to see if an SMS has arrived and do things based upon such an event.

Regards,

Paul

simon.schvartzman
Posts: 638
Joined: Tue Jul 29, 2014 12:52 am
Location: Brazil

Re: Getting real IP of mobile device not working but...

Post by simon.schvartzman » Fri Jul 21, 2017 1:39 pm

@FourthWorld using a Rp Pi is indeed very appealing the point is that I will need to add the camera and the modem (I do have to send the pictures do dropbox without wifi) and I get all of this "for free" on an old cheap cellular (could be iPhone or Android)...

Well I guess it ends up to decide between:

A - an external third device with fixed IP or Domain name acting as a server
B - a DNS service
C - communicate via file sharing on Dropbox.

Many thanks for all of you for your help and suggestions.

Regards
Simon
________________________________________
To ";" or not to ";" that is the question

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

Re: Getting real IP of mobile device not working but...

Post by FourthWorld » Fri Jul 21, 2017 3:41 pm

Are the two phones on the same local subnet?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Post Reply

Return to “Internet”