Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

How to get the public IP address of device?
Started by Icy Spark Jul 09 2013 11:57 AM

- - - - -
5 replies to this topic
socket network

Best Answer Stephen Lewis , 09 July 2013 - 06:14 PM

Right, aukStudios jogged my memory.  Here's what ended up working for me.  Lua code to network.request the ip:
 
 

function findClientIPAddress()
    
    local getipScriptURL = "http://xxxxxxxxxxxxxxxx/getip.php"  --php script on my server
    local DeviceIP
    
    function ipListener(event)
        if not event.isError and event.response ~= "" then
            DeviceIP = event.response 
            print("DeviceIP:"..DeviceIP)
        end
    end
    
    network.request(getipScriptURL,"GET",ipListener) 
    
end
 
findClientIPAddress()

 
 
And here is the entirety of the php script on my server:
 
<?php
   echo $_SERVER["REMOTE_ADDR"];
?>

[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

Icy Spark

[GLOBAL: userInfoPane.html]
Icy Spark
  • Contributor

  • 268 posts
  • Corona SDK

Hi,

 

I am using an api and need to pass it the devices public ip address.

 

Does anyone know how to do this?

 

I have seen some examples using luasocket but unfortunately they only seem to cover the local ip address eg 192.168.0.101 that is connected to the router via wifi etc.

 

 



[TOPIC: post.html]
#2

Stephen Lewis

[GLOBAL: userInfoPane.html]
Stephen Lewis
  • Contributor

  • 716 posts
  • Enterprise

I found this code somewhere on this forum a long time ago.  Not even sure it works to be honest but I recall testing it and it seemed to.

 

        require("socket")
        local someRandomIP = "192.168.1.122" --This address you make up
        local someRandomPort = "3102" --This port you make up  
        local mySocket = socket.udp() --Create a UDP socket like normal
        --This is the weird part, we need to set the peer for some reason
        mySocket:setpeername(someRandomIP,someRandomPort) 
        --I believe this binds the socket
        --Then we can obtain the correct ip address and port
        local myDevicesIpAddress, somePortChosenByTheOS = mySocket:getsockname()-- returns IP and Port 


[TOPIC: post.html]
#3

Icy Spark

[GLOBAL: userInfoPane.html]
Icy Spark
  • Contributor

  • 268 posts
  • Corona SDK

Thanks for that,  though I just tested it and it returns my private router IP address ie 192.168.0.103 via wifi rather than my public IP address.



[TOPIC: post.html]
#4

aukStudios

[GLOBAL: userInfoPane.html]
aukStudios
  • Corona Geek

  • 1,003 posts
  • Corona SDK

One option is to put a simple php script on a web server that echo's back the IP address of any request that pings it.  You could then do a network.request() to that script, and the response will have the IP address.

 

- Andrew



[TOPIC: post.html]
#5

Stephen Lewis

[GLOBAL: userInfoPane.html]
Stephen Lewis
  • Contributor

  • 716 posts
  • Enterprise

  Best Answer

Right, aukStudios jogged my memory.  Here's what ended up working for me.  Lua code to network.request the ip:
 
 

function findClientIPAddress()
    
    local getipScriptURL = "http://xxxxxxxxxxxxxxxx/getip.php"  --php script on my server
    local DeviceIP
    
    function ipListener(event)
        if not event.isError and event.response ~= "" then
            DeviceIP = event.response 
            print("DeviceIP:"..DeviceIP)
        end
    end
    
    network.request(getipScriptURL,"GET",ipListener) 
    
end
 
findClientIPAddress()

 
 
And here is the entirety of the php script on my server:
 
<?php
   echo $_SERVER["REMOTE_ADDR"];
?>


[TOPIC: post.html]
#6

Icy Spark

[GLOBAL: userInfoPane.html]
Icy Spark
  • Contributor

  • 268 posts
  • Corona SDK

Right, aukStudios jogged my memory.  Here's what ended up working for me.  Lua code to network.request the ip:
 
 

function findClientIPAddress()
    
    local getipScriptURL = "http://xxxxxxxxxxxxxxxx/getip.php"  --php script on my server
    local DeviceIP
    
    function ipListener(event)
        if not event.isError and event.response ~= "" then
            DeviceIP = event.response 
            print("DeviceIP:"..DeviceIP)
        end
    end
    
    network.request(getipScriptURL,"GET",ipListener) 
    
end
 
findClientIPAddress()

 
 
And here is the entirety of the php script on my server:
 

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

 

Thank you very much.  Worked Perfect!!!

 

Though I didn't need to add my own remote server php I just called:

 

local getipScriptURL = "http://myip.dnsomatic.com/"

 

as they do exactly what you mentioned by echoing the IP address back. 

 

Thank you very much for the solution!




[topic_controls]
 
[/topic_controls]