socket.* (networking)

Type Library
Revision Current Public Release (2013.1076)
Keywords socket, luasocket, networking
Sample code /CoronaSDK/SampleCode/Networking/SimpleImageDownload
See also Official LuaSocket Documentation
network.request()
network.download()
network.canDetectNetworkStatusChanges
network.setStatusListener()

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

For Asynchronous HTTP functions (rather than the synchronous LuaSocket functions), see network.request() and network.download().

More Resources

Full LuaSocket documentation can be found here: LuaSocket Documentation

Also see the SimpleImageDownload sample code: /CoronaSDK/SampleCode/Networking/SimpleImageDownload/

 Gotchas

On Android, you must add the following permission to the "build.settings" file in order to use this API.

settings =
{
   android =
   {
      usesPermissions =
      {
         "android.permission.INTERNET",
      },
   },
}

Examples

How to retrieve devices IP Address & Port via Lua Socket

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: " 

Download image from remote URL:

-- 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://www.coronalabs.com/demo/hello.png", 
    sink = ltn12.sink.file(myFile),
}

-- Display local file
testImage = display.newImage("hello.png",system.DocumentsDirectory,60,50);