0

I am developping a phonegap android app, that needs to connect to a server on the same wifi network.
the app works fine if I provide the server ip, but in case the router shutdown , the ip changes !. I am using a XAMPP server on my laptop. and the app is going to be installed in about 20 devices. Meaning that the manual approach is painful !!

  • So, is there anyway to reconnect automaticly without knowing the new ip?
  • and, How to do so?
MontaWiso
  • 99
  • 1
  • 1
  • 7

3 Answers3

0

No sorry, this problem is not related to phonegap, simply a frontend service (like phonegap), always needs a IP or DNS to connect to a server. Check about static IP to configure into your server, so it will have the same local IP even if the reouter reboots. if you will have the server public, you should check about DNS providers.

Good luck!

Víctor
  • 3,029
  • 3
  • 28
  • 43
0

The client looks at its own ip and so knows what the ip of the server looks like. Then it just tries ip addresses around its own ip. Set a timeout of one second. Whithin a few seconds the right ip is choosen.

greenapps
  • 11,154
  • 2
  • 16
  • 19
  • So, how exactly I can get the private ip of the device? I am asking because so far (by googling) this is done by a server side request :( !!! – MontaWiso May 23 '16 at 21:09
0

My answer is based on what @Greenapps suggested .
I simply looped through the possible ip address and Eureka !!!

CLIENT

<html>
<head>
<script>
var i = 0;
window.setTimeout(testip,1000);
while(i<=255){
       testip(i++);
}
function testip(j){
 var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                if(xmlhttp.responseText.indexOf("192.168.1")>-1)
                window.location.href ="http://"+xmlhttp.responseText+":8080/rest/server/categorie.php";
            }
        };
        xmlhttp.open("GET", "http://192.168.1."+j+":8080/rest/server/getip.php", true);
        xmlhttp.send();

}

</script>
</head>
</html>  

SERVER (getip.php)

<?php
    header('Access-Control-Allow-Origin: *'); 
     echo getHostByName(getHostName());
?>
MontaWiso
  • 99
  • 1
  • 1
  • 7