×
A new build of Corona SDK is now available to subscribers. Not a subscriber? Subscribe now.
CoronaSDK 2012.821 | Released: 23 May 2012, 2:01am | What's New | Download Now

Network

Overview

Corona currently includes the version 2.02 of the LuaSocket libraries. These Lua modules implement common network protocols such as SMTP (sending e-mails), HTTP (WWW access) and FTP (uploading and downloading files). Also included are features to support MIME (common encodings), URL manipulation and LTN12 (transferring and filtering data).

Starting with build #268, Asynchronous HTTP functions were added. These functions can be used to replace the Synchronous LuaSocket functions.

Starting in build #496 network status/reachability APIs were introduced for iOS and Mac. These APIs let you monitor if a network address is reachable and how it is reachable (e.g. through WiFi or cellular).

More Information

LuaSocket

I will provide more LuaSocket examples as I make them.

1) How to retrive devices IP Address & Port via Lua Socket

1
2
3
4
5
6
7
8
9
10
local socket = require("socket")
 
--Connect to the client
local client = socket.connect("www.google.com",  80)
--Get IP and Port from client
local ip, port = client:getsockname()
 
--Print the ip address and port to the terminal
print("IP Address: " .. ip)
print("Port: " .. port)

Full LuaSocket documentation can be found here:

http://www.tecgraf.puc-rio.br/~diego/professional/luasocket/reference.html

Sample Programs Using LuaSocket API
/Networking/SimpleImageDownload

Note: You will need to use the new asynchronous network APIs for asynchronous transfers or secured HTTPS calls.

Network Status

These APIs will let you listen for network status changes on a specific host basis. These will trigger a callback when network status conditions change. This is important for mobile devices because you may gain or lose a network connection at any time. This is also important if you generate large amounts of traffic because you may want to avoid sending large amounts of traffic over cellular connections.

A good 3rd-party documentation on how network status/reachability works on iOS/Mac can be found at http://blog.ddg.com/?p=24.

From the above documentation, you generally want to ask/answer the following questions:

For a specific host of interest:


  • Is the host reachable? (event.isReachable)
  • Is the connection up? (event.isConnectionRequired)

    • Will it come up automatically? (event.isConnectionOnDemand)
    • Will the user have to enter a password? (event.isInteractionRequired)
  • Is the connection slow? (event.isReachableViaCellular)
  • Is the connection fast? (event.isReachableViaWiFi)

Example for LuaSocket

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
-- Load the relevant LuaSocket modules
local http = require("socket.http")
local ltn12 = require("ltn12")
 
-- Create local file for saving data
local path = system.pathForFile( "hello.png", system.DocumentsDirectory )
myFile = io.open( path, "w+b" ) 
 
-- Request remote file and save data to local file
http.request{
    url = "http://developer.anscamobile.com/demo/hello.png", 
    sink = ltn12.sink.file(myFile),
}
 
-- Display local file
testImage = display.newImage("hello.png",system.DocumentsDirectory,60,50);

Example for Reachability

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
function MyNetworkReachabilityListener(event)
        print( "address", event.address )
        print( "isReachable", event.isReachable )
        print("isConnectionRequired", event.isConnectionRequired)
        print("isConnectionOnDemand", event.isConnectionOnDemand)
        print("IsInteractionRequired", event.isInteractionRequired)
        print("IsReachableViaCellular", event.isReachableViaCellular)
        print("IsReachableViaWiFi", event.isReachableViaWiFi)   
 
--[[ If you want to remove the listener, call network.setStatusListener("www.apple.com", nil)   
        g_Counter = g_Counter + 1
        if g_Counter > 3 then
                print("removing event listener")
                network.setStatusListener( "www.apple.com", nil)
        end
--]]
 
end
 
if network.canDetectNetworkStatusChanges then
        network.setStatusListener( "www.apple.com", MyNetworkReachabilityListener )
else
        print("network reachability not supported on this platform")
end