Blame samples/echosrvr.lua
|
Packit |
b53373 |
-----------------------------------------------------------------------------
|
|
Packit |
b53373 |
-- UDP sample: echo protocol server
|
|
Packit |
b53373 |
-- LuaSocket sample files
|
|
Packit |
b53373 |
-- Author: Diego Nehab
|
|
Packit |
b53373 |
-----------------------------------------------------------------------------
|
|
Packit |
b53373 |
local socket = require("socket")
|
|
Packit |
b53373 |
host = host or "127.0.0.1"
|
|
Packit |
b53373 |
port = port or 7
|
|
Packit |
b53373 |
if arg then
|
|
Packit |
b53373 |
host = arg[1] or host
|
|
Packit |
b53373 |
port = arg[2] or port
|
|
Packit |
b53373 |
end
|
|
Packit |
b53373 |
print("Binding to host '" ..host.. "' and port " ..port.. "...")
|
|
Packit |
b53373 |
udp = assert(socket.udp())
|
|
Packit |
b53373 |
assert(udp:setsockname(host, port))
|
|
Packit |
b53373 |
assert(udp:settimeout(5))
|
|
Packit |
b53373 |
ip, port = udp:getsockname()
|
|
Packit |
b53373 |
assert(ip, port)
|
|
Packit |
b53373 |
print("Waiting packets on " .. ip .. ":" .. port .. "...")
|
|
Packit |
b53373 |
while 1 do
|
|
Packit |
b53373 |
dgram, ip, port = udp:receivefrom()
|
|
Packit |
b53373 |
if dgram then
|
|
Packit |
b53373 |
print("Echoing '" .. dgram .. "' to " .. ip .. ":" .. port)
|
|
Packit |
b53373 |
udp:sendto(dgram, ip, port)
|
|
Packit |
b53373 |
else
|
|
Packit |
b53373 |
print(ip)
|
|
Packit |
b53373 |
end
|
|
Packit |
b53373 |
end
|