summaryrefslogtreecommitdiff
path: root/ThirdParty/luasocket/samples
diff options
context:
space:
mode:
Diffstat (limited to 'ThirdParty/luasocket/samples')
-rw-r--r--ThirdParty/luasocket/samples/README50
-rw-r--r--ThirdParty/luasocket/samples/cddb.lua46
-rw-r--r--ThirdParty/luasocket/samples/daytimeclnt.lua22
-rw-r--r--ThirdParty/luasocket/samples/echoclnt.lua23
-rw-r--r--ThirdParty/luasocket/samples/echosrvr.lua28
-rw-r--r--ThirdParty/luasocket/samples/listener.lua25
-rw-r--r--ThirdParty/luasocket/samples/lpr.lua51
-rw-r--r--ThirdParty/luasocket/samples/mclisten.lua18
-rw-r--r--ThirdParty/luasocket/samples/mcsend.lua20
-rw-r--r--ThirdParty/luasocket/samples/talker.lua20
-rw-r--r--ThirdParty/luasocket/samples/tinyirc.lua89
11 files changed, 392 insertions, 0 deletions
diff --git a/ThirdParty/luasocket/samples/README b/ThirdParty/luasocket/samples/README
new file mode 100644
index 0000000..e63a6f5
--- /dev/null
+++ b/ThirdParty/luasocket/samples/README
@@ -0,0 +1,50 @@
+This directory contains some sample programs using
+LuaSocket. This code is not supported.
+
+ listener.lua -- socket to stdout
+ talker.lua -- stdin to socket
+
+listener.lua and talker.lua are about the simplest
+applications you can write using LuaSocket. Run
+
+ 'lua listener.lua' and 'lua talker.lua'
+
+on different terminals. Whatever you type on talk.lua will
+be printed by listen.lua.
+
+ lpr.lua -- lpr client
+
+This is a cool program written by David Burgess to print
+files using the Line Printer Daemon protocol, widely used in
+Unix machines. It uses the lp.lua implementation, in the
+etc directory. Just run 'lua lpr.lua <filename>
+queue=<printername>' and the file will print!
+
+ cddb.lua -- CDDB client
+
+This is the first try on a simple CDDB client. Not really
+useful, but one day it might become a module.
+
+ daytimeclnt.lua -- day time client
+
+Just run the program to retrieve the hour and date in
+readable form from any server running an UDP daytime daemon.
+
+ echoclnt.lua -- UDP echo client
+ echosrvr.lua -- UDP echo server
+
+These are a UDP echo client/server pair. They work with
+other client and servers as well.
+
+ tinyirc.lua -- irc like broadcast server
+
+This is a simple server that waits simultaneously on two
+server sockets for telnet connections. Everything it
+receives from the telnet clients is broadcasted to every
+other connected client. It tests the select function and
+shows how to create a simple server whith LuaSocket. Just
+run tinyirc.lua and then open as many telnet connections
+as you want to ports 8080 and 8081.
+
+Good luck,
+Diego.
diff --git a/ThirdParty/luasocket/samples/cddb.lua b/ThirdParty/luasocket/samples/cddb.lua
new file mode 100644
index 0000000..49a1871
--- /dev/null
+++ b/ThirdParty/luasocket/samples/cddb.lua
@@ -0,0 +1,46 @@
+local socket = require("socket")
+local http = require("socket.http")
+
+if not arg or not arg[1] or not arg[2] then
+ print("luasocket cddb.lua <category> <disc-id> [<server>]")
+ os.exit(1)
+end
+
+local server = arg[3] or "http://freedb.freedb.org/~cddb/cddb.cgi"
+
+function parse(body)
+ local lines = string.gfind(body, "(.-)\r\n")
+ local status = lines()
+ local code, message = socket.skip(2, string.find(status, "(%d%d%d) (.*)"))
+ if tonumber(code) ~= 210 then
+ return nil, code, message
+ end
+ local data = {}
+ for l in lines do
+ local c = string.sub(l, 1, 1)
+ if c ~= '#' and c ~= '.' then
+ local key, value = socket.skip(2, string.find(l, "(.-)=(.*)"))
+ value = string.gsub(value, "\\n", "\n")
+ value = string.gsub(value, "\\\\", "\\")
+ value = string.gsub(value, "\\t", "\t")
+ data[key] = value
+ end
+ end
+ return data, code, message
+end
+
+local host = socket.dns.gethostname()
+local query = "%s?cmd=cddb+read+%s+%s&hello=LuaSocket+%s+LuaSocket+2.0&proto=6"
+local url = string.format(query, server, arg[1], arg[2], host)
+local body, headers, code = http.request(url)
+
+if code == 200 then
+ local data, code, error = parse(body)
+ if not data then
+ print(error or code)
+ else
+ for i,v in pairs(data) do
+ io.write(i, ': ', v, '\n')
+ end
+ end
+else print(error) end
diff --git a/ThirdParty/luasocket/samples/daytimeclnt.lua b/ThirdParty/luasocket/samples/daytimeclnt.lua
new file mode 100644
index 0000000..f81e37c
--- /dev/null
+++ b/ThirdParty/luasocket/samples/daytimeclnt.lua
@@ -0,0 +1,22 @@
+-----------------------------------------------------------------------------
+-- UDP sample: daytime protocol client
+-- LuaSocket sample files
+-- Author: Diego Nehab
+-----------------------------------------------------------------------------
+local socket = require"socket"
+host = host or "127.0.0.1"
+port = port or 13
+if arg then
+ host = arg[1] or host
+ port = arg[2] or port
+end
+host = socket.dns.toip(host)
+udp = socket.udp()
+print("Using host '" ..host.. "' and port " ..port.. "...")
+udp:setpeername(host, port)
+udp:settimeout(3)
+sent, err = udp:send("anything")
+if err then print(err) os.exit() end
+dgram, err = udp:receive()
+if not dgram then print(err) os.exit() end
+io.write(dgram)
diff --git a/ThirdParty/luasocket/samples/echoclnt.lua b/ThirdParty/luasocket/samples/echoclnt.lua
new file mode 100644
index 0000000..bb22557
--- /dev/null
+++ b/ThirdParty/luasocket/samples/echoclnt.lua
@@ -0,0 +1,23 @@
+-----------------------------------------------------------------------------
+-- UDP sample: echo protocol client
+-- LuaSocket sample files
+-- Author: Diego Nehab
+-----------------------------------------------------------------------------
+local socket = require("socket")
+host = host or "localhost"
+port = port or 7
+if arg then
+ host = arg[1] or host
+ port = arg[2] or port
+end
+host = socket.dns.toip(host)
+udp = assert(socket.udp())
+assert(udp:setpeername(host, port))
+print("Using remote host '" ..host.. "' and port " .. port .. "...")
+while 1 do
+ line = io.read()
+ if not line or line == "" then os.exit() end
+ assert(udp:send(line))
+ dgram = assert(udp:receive())
+ print(dgram)
+end
diff --git a/ThirdParty/luasocket/samples/echosrvr.lua b/ThirdParty/luasocket/samples/echosrvr.lua
new file mode 100644
index 0000000..ea564e2
--- /dev/null
+++ b/ThirdParty/luasocket/samples/echosrvr.lua
@@ -0,0 +1,28 @@
+-----------------------------------------------------------------------------
+-- UDP sample: echo protocol server
+-- LuaSocket sample files
+-- Author: Diego Nehab
+-----------------------------------------------------------------------------
+local socket = require("socket")
+host = host or "127.0.0.1"
+port = port or 7
+if arg then
+ host = arg[1] or host
+ port = arg[2] or port
+end
+print("Binding to host '" ..host.. "' and port " ..port.. "...")
+udp = assert(socket.udp())
+assert(udp:setsockname(host, port))
+assert(udp:settimeout(5))
+ip, port = udp:getsockname()
+assert(ip, port)
+print("Waiting packets on " .. ip .. ":" .. port .. "...")
+while 1 do
+ dgram, ip, port = udp:receivefrom()
+ if dgram then
+ print("Echoing '" .. dgram .. "' to " .. ip .. ":" .. port)
+ udp:sendto(dgram, ip, port)
+ else
+ print(ip)
+ end
+end
diff --git a/ThirdParty/luasocket/samples/listener.lua b/ThirdParty/luasocket/samples/listener.lua
new file mode 100644
index 0000000..77db2d5
--- /dev/null
+++ b/ThirdParty/luasocket/samples/listener.lua
@@ -0,0 +1,25 @@
+-----------------------------------------------------------------------------
+-- TCP sample: Little program to dump lines received at a given port
+-- LuaSocket sample files
+-- Author: Diego Nehab
+-----------------------------------------------------------------------------
+local socket = require("socket")
+host = host or "*"
+port = port or 8080
+if arg then
+ host = arg[1] or host
+ port = arg[2] or port
+end
+print("Binding to host '" ..host.. "' and port " ..port.. "...")
+s = assert(socket.bind(host, port))
+i, p = s:getsockname()
+assert(i, p)
+print("Waiting connection from talker on " .. i .. ":" .. p .. "...")
+c = assert(s:accept())
+print("Connected. Here is the stuff:")
+l, e = c:receive()
+while not e do
+ print(l)
+ l, e = c:receive()
+end
+print(e)
diff --git a/ThirdParty/luasocket/samples/lpr.lua b/ThirdParty/luasocket/samples/lpr.lua
new file mode 100644
index 0000000..49a1dfa
--- /dev/null
+++ b/ThirdParty/luasocket/samples/lpr.lua
@@ -0,0 +1,51 @@
+local lp = require("socket.lp")
+
+local function usage()
+ print('\nUsage: lua lpr.lua [filename] [keyword=val...]\n')
+ print('Valid keywords are :')
+ print(
+ ' host=remote host or IP address (default "localhost")\n' ..
+ ' queue=remote queue or printer name (default "printer")\n' ..
+ ' port=remote port number (default 515)\n' ..
+ ' user=sending user name\n' ..
+ ' format=["binary" | "text" | "ps" | "pr" | "fortran"] (default "binary")\n' ..
+ ' banner=true|false\n' ..
+ ' indent=number of columns to indent\n' ..
+ ' mail=email of address to notify when print is complete\n' ..
+ ' title=title to use for "pr" format\n' ..
+ ' width=width for "text" or "pr" formats\n' ..
+ ' class=\n' ..
+ ' job=\n' ..
+ ' name=\n' ..
+ ' localbind=true|false\n'
+ )
+ return nil
+end
+
+if not arg or not arg[1] then
+ return usage()
+end
+
+do
+ local opt = {}
+ local pat = "[%s%c%p]*([%w]*)=([\"]?[%w%s_!@#$%%^&*()<>:;]+[\"]?.?)"
+ for i = 2, #arg, 1 do
+ string.gsub(arg[i], pat, function(name, value) opt[name] = value end)
+ end
+ if not arg[2] then
+ return usage()
+ end
+ if arg[1] ~= "query" then
+ opt.file = arg[1]
+ r,e=lp.send(opt)
+ io.stdout:write(tostring(r or e),'\n')
+ else
+ r,e=lp.query(opt)
+ io.stdout:write(tostring(r or e), '\n')
+ end
+end
+
+-- trivial tests
+--lua lp.lua lp.lua queue=default host=localhost
+--lua lp.lua lp.lua queue=default host=localhost format=binary localbind=1
+--lua lp.lua query queue=default host=localhost
diff --git a/ThirdParty/luasocket/samples/mclisten.lua b/ThirdParty/luasocket/samples/mclisten.lua
new file mode 100644
index 0000000..d40d789
--- /dev/null
+++ b/ThirdParty/luasocket/samples/mclisten.lua
@@ -0,0 +1,18 @@
+local socket = require"socket"
+local group = "225.0.0.37"
+local port = 12345
+local c = assert(socket.udp())
+print(assert(c:setoption("reuseport", true)))
+print(assert(c:setsockname("*", port)))
+--print("loop:", c:getoption("ip-multicast-loop"))
+--print(assert(c:setoption("ip-multicast-loop", false)))
+--print("loop:", c:getoption("ip-multicast-loop"))
+--print("if:", c:getoption("ip-multicast-if"))
+--print(assert(c:setoption("ip-multicast-if", "127.0.0.1")))
+--print("if:", c:getoption("ip-multicast-if"))
+--print(assert(c:setoption("ip-multicast-if", "10.0.1.4")))
+--print("if:", c:getoption("ip-multicast-if"))
+print(assert(c:setoption("ip-add-membership", {multiaddr = group, interface = "*"})))
+while 1 do
+ print(c:receivefrom())
+end
diff --git a/ThirdParty/luasocket/samples/mcsend.lua b/ThirdParty/luasocket/samples/mcsend.lua
new file mode 100644
index 0000000..7c24cdf
--- /dev/null
+++ b/ThirdParty/luasocket/samples/mcsend.lua
@@ -0,0 +1,20 @@
+local socket = require"socket"
+local group = "225.0.0.37"
+local port = 12345
+local c = assert(socket.udp())
+--print(assert(c:setoption("reuseport", true)))
+--print(assert(c:setsockname("*", port)))
+--print(assert(c:setoption("ip-multicast-loop", false)))
+--print(assert(c:setoption("ip-multicast-ttl", 4)))
+--print(assert(c:setoption("ip-multicast-if", "10.0.1.3")))
+--print(assert(c:setoption("ip-add-membership", {multiaddr = group, interface = "*"})))
+local i = 0
+while 1 do
+ local message = string.format("hello all %d!", i)
+ assert(c:sendto(message, group, port))
+ print("sent " .. message)
+ socket.sleep(1)
+ c:settimeout(0.5)
+ print(c:receivefrom())
+ i = i + 1
+end
diff --git a/ThirdParty/luasocket/samples/talker.lua b/ThirdParty/luasocket/samples/talker.lua
new file mode 100644
index 0000000..d5a36cb
--- /dev/null
+++ b/ThirdParty/luasocket/samples/talker.lua
@@ -0,0 +1,20 @@
+-----------------------------------------------------------------------------
+-- TCP sample: Little program to send text lines to a given host/port
+-- LuaSocket sample files
+-- Author: Diego Nehab
+-----------------------------------------------------------------------------
+local socket = require("socket")
+host = host or "localhost"
+port = port or 8080
+if arg then
+ host = arg[1] or host
+ port = arg[2] or port
+end
+print("Attempting connection to host '" ..host.. "' and port " ..port.. "...")
+c = assert(socket.connect(host, port))
+print("Connected! Please type stuff (empty line to stop):")
+l = io.read()
+while l and l ~= "" and not e do
+ assert(c:send(l .. "\n"))
+ l = io.read()
+end
diff --git a/ThirdParty/luasocket/samples/tinyirc.lua b/ThirdParty/luasocket/samples/tinyirc.lua
new file mode 100644
index 0000000..5babb7e
--- /dev/null
+++ b/ThirdParty/luasocket/samples/tinyirc.lua
@@ -0,0 +1,89 @@
+-----------------------------------------------------------------------------
+-- Select sample: simple text line server
+-- LuaSocket sample files.
+-- Author: Diego Nehab
+-----------------------------------------------------------------------------
+local socket = require("socket")
+host = host or "*"
+port1 = port1 or 8080
+port2 = port2 or 8181
+if arg then
+ host = arg[1] or host
+ port1 = arg[2] or port1
+ port2 = arg[3] or port2
+end
+
+server1 = assert(socket.bind(host, port1))
+server2 = assert(socket.bind(host, port2))
+server1:settimeout(1) -- make sure we don't block in accept
+server2:settimeout(1)
+
+io.write("Servers bound\n")
+
+-- simple set implementation
+-- the select function doesn't care about what is passed to it as long as
+-- it behaves like a table
+-- creates a new set data structure
+function newset()
+ local reverse = {}
+ local set = {}
+ return setmetatable(set, {__index = {
+ insert = function(set, value)
+ if not reverse[value] then
+ table.insert(set, value)
+ reverse[value] = #set
+ end
+ end,
+ remove = function(set, value)
+ local index = reverse[value]
+ if index then
+ reverse[value] = nil
+ local top = table.remove(set)
+ if top ~= value then
+ reverse[top] = index
+ set[index] = top
+ end
+ end
+ end
+ }})
+end
+
+set = newset()
+
+io.write("Inserting servers in set\n")
+set:insert(server1)
+set:insert(server2)
+
+while 1 do
+ local readable, _, error = socket.select(set, nil)
+ for _, input in ipairs(readable) do
+ -- is it a server socket?
+ if input == server1 or input == server2 then
+ io.write("Waiting for clients\n")
+ local new = input:accept()
+ if new then
+ new:settimeout(1)
+ io.write("Inserting client in set\n")
+ set:insert(new)
+ end
+ -- it is a client socket
+ else
+ local line, error = input:receive()
+ if error then
+ input:close()
+ io.write("Removing client from set\n")
+ set:remove(input)
+ else
+ io.write("Broadcasting line '", line, "'\n")
+ writable, error = socket.skip(1, socket.select(nil, set, 1))
+ if not error then
+ for __, output in ipairs(writable) do
+ if output ~= input then
+ output:send(line .. "\n")
+ end
+ end
+ else io.write("No client ready to receive!!!\n") end
+ end
+ end
+ end
+end