aboutsummaryrefslogtreecommitdiff
path: root/samples
diff options
context:
space:
mode:
Diffstat (limited to 'samples')
-rw-r--r--samples/README2
-rw-r--r--samples/echoclnt.lua10
-rw-r--r--samples/echosrvr.lua9
-rw-r--r--samples/listener.lua7
-rw-r--r--samples/talker.lua4
-rw-r--r--samples/tinyirc.lua4
6 files changed, 19 insertions, 17 deletions
diff --git a/samples/README b/samples/README
index 3ce4067..0100a4a 100644
--- a/samples/README
+++ b/samples/README
@@ -7,7 +7,7 @@ is not supported.
7listener.lua and talker.lua are about the simplest applications you can 7listener.lua and talker.lua are about the simplest applications you can
8write using LuaSocket. Run 8write using LuaSocket. Run
9 9
10 'lua listen.lua' and 'lua talk.lua' 10 'lua listener.lua' and 'lua talker.lua'
11 11
12on different terminals. Whatever you type on talk.lua will be 12on different terminals. Whatever you type on talk.lua will be
13printed by listen.lua. 13printed by listen.lua.
diff --git a/samples/echoclnt.lua b/samples/echoclnt.lua
index 038061d..764e433 100644
--- a/samples/echoclnt.lua
+++ b/samples/echoclnt.lua
@@ -12,13 +12,13 @@ if arg then
12 port = arg[2] or port 12 port = arg[2] or port
13end 13end
14host = socket.dns.toip(host) 14host = socket.dns.toip(host)
15udp = socket.try(socket.udp()) 15udp = assert(socket.udp())
16socket.try(udp:setpeername(host, port)) 16assert(udp:setpeername(host, port))
17print("Using remote host '" ..host.. "' and port " .. port .. "...") 17print("Using remote host '" ..host.. "' and port " .. port .. "...")
18while 1 do 18while 1 do
19 line = io.read() 19 line = io.read()
20 if not line then os.exit() end 20 if not line or line == "" then os.exit() end
21 socket.try(udp:send(line)) 21 assert(udp:send(line))
22 dgram = socket.try(udp:receive()) 22 dgram = assert(udp:receive())
23 print(dgram) 23 print(dgram)
24end 24end
diff --git a/samples/echosrvr.lua b/samples/echosrvr.lua
index 3ebbe85..d4449e5 100644
--- a/samples/echosrvr.lua
+++ b/samples/echosrvr.lua
@@ -12,10 +12,11 @@ if arg then
12 port = arg[2] or port 12 port = arg[2] or port
13end 13end
14print("Binding to host '" ..host.. "' and port " ..port.. "...") 14print("Binding to host '" ..host.. "' and port " ..port.. "...")
15udp = socket.try(socket.udp()) 15udp = assert(socket.udp())
16socket.try(udp:setsockname(host, port)) 16assert(udp:setsockname(host, port))
17socket.try(udp:settimeout(5)) 17assert(udp:settimeout(5))
18ip, port = socket.try(udp:getsockname()) 18ip, port = udp:getsockname()
19assert(ip, port)
19print("Waiting packets on " .. ip .. ":" .. port .. "...") 20print("Waiting packets on " .. ip .. ":" .. port .. "...")
20while 1 do 21while 1 do
21 dgram, ip, port = udp:receivefrom() 22 dgram, ip, port = udp:receivefrom()
diff --git a/samples/listener.lua b/samples/listener.lua
index 65c6501..4d4c3b6 100644
--- a/samples/listener.lua
+++ b/samples/listener.lua
@@ -12,10 +12,11 @@ if arg then
12 port = arg[2] or port 12 port = arg[2] or port
13end 13end
14print("Binding to host '" ..host.. "' and port " ..port.. "...") 14print("Binding to host '" ..host.. "' and port " ..port.. "...")
15s = socket.try(socket.bind(host, port)) 15s = assert(socket.bind(host, port))
16i, p = socket.try(s:getsockname()) 16i, p = s:getsockname()
17assert(i, p)
17print("Waiting connection from talker on " .. i .. ":" .. p .. "...") 18print("Waiting connection from talker on " .. i .. ":" .. p .. "...")
18c = socket.try(s:accept()) 19c = assert(s:accept())
19print("Connected. Here is the stuff:") 20print("Connected. Here is the stuff:")
20l, e = c:receive() 21l, e = c:receive()
21while not e do 22while not e do
diff --git a/samples/talker.lua b/samples/talker.lua
index 3f6e69c..901ed2c 100644
--- a/samples/talker.lua
+++ b/samples/talker.lua
@@ -12,10 +12,10 @@ if arg then
12 port = arg[2] or port 12 port = arg[2] or port
13end 13end
14print("Attempting connection to host '" ..host.. "' and port " ..port.. "...") 14print("Attempting connection to host '" ..host.. "' and port " ..port.. "...")
15c = socket.try(socket.connect(host, port)) 15c = assert(socket.connect(host, port))
16print("Connected! Please type stuff (empty line to stop):") 16print("Connected! Please type stuff (empty line to stop):")
17l = io.read() 17l = io.read()
18while l and l ~= "" and not e do 18while l and l ~= "" and not e do
19 socket.try(c:send(l, "\n")) 19 assert(c:send(l .. "\n"))
20 l = io.read() 20 l = io.read()
21end 21end
diff --git a/samples/tinyirc.lua b/samples/tinyirc.lua
index 13f42ec..684e7c0 100644
--- a/samples/tinyirc.lua
+++ b/samples/tinyirc.lua
@@ -14,8 +14,8 @@ if arg then
14 port2 = arg[3] or port2 14 port2 = arg[3] or port2
15end 15end
16 16
17server1 = socket.try(socket.bind(host, port1)) 17server1 = assert(socket.bind(host, port1))
18server2 = socket.try(socket.bind(host, port2)) 18server2 = assert(socket.bind(host, port2))
19server1:settimeout(1) -- make sure we don't block in accept 19server1:settimeout(1) -- make sure we don't block in accept
20server2:settimeout(1) 20server2:settimeout(1)
21 21