Skip to content

Instantly share code, notes, and snippets.

@corny
Created July 4, 2016 16:08
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save corny/5c0a3d426ccce85bd1589d3fa1508efe to your computer and use it in GitHub Desktop.
Save corny/5c0a3d426ccce85bd1589d3fa1508efe to your computer and use it in GitHub Desktop.
Lua UDP example
#!/usr/bin/env lua5.2
--
-- apt install lua5.2 lua-socket
--
local socket = require("socket")
local udp = assert(socket.udp())
local data
udp:settimeout(1)
assert(udp:setsockname("*",0))
assert(udp:setpeername("example.com",1234))
for i = 0, 2, 1 do
assert(udp:send("ping"))
data = udp:receive()
if data then
break
end
end
if data == nil then
print("timeout")
else
print(data)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment