lang/lua/ LuaSocketsExample001


Send

-- change here to the host an port you want to contact
local host, port = "localhost", 9000
-- load namespace
local socket = require("socket")
-- convert host name to ip address
local ip = assert(socket.dns.toip(host))
-- create a new UDP object
local udp = assert(socket.udp())
-- contact daytime host
assert(udp:sendto("anything", ip, port))

Receive

-- change here to the host an port you want to contact
local host, port = "*", 9000
-- load namespace
local socket = require("socket")
-- create a new UDP object
local udp = assert(socket.udp())
udp:settimeout(5)
assert(udp:setsockname(host,port))
data, msg_or_ip, port_or_nil = udp:receivefrom()
if data then
  print(data)
else
  print("no data")
end