diff options
author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-05-30 21:36:22 +0000 |
---|---|---|
committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-05-30 21:36:22 +0000 |
commit | 5ca1049ab47f3f9ff9157f71af9072f04a637500 (patch) | |
tree | 24fcb14f2890900a4a709312ab25bfc2c14a3939 /samples | |
parent | c23240726e3044e3eaa32a82a999b754c08bc183 (diff) | |
download | luasocket-5ca1049ab47f3f9ff9157f71af9072f04a637500.tar.gz luasocket-5ca1049ab47f3f9ff9157f71af9072f04a637500.tar.bz2 luasocket-5ca1049ab47f3f9ff9157f71af9072f04a637500.zip |
Fine tuning the "require" business.
Diffstat (limited to 'samples')
-rw-r--r-- | samples/listener.lua | 15 | ||||
-rw-r--r-- | samples/talker.lua | 13 | ||||
-rw-r--r-- | samples/tinyirc.lua | 19 |
3 files changed, 17 insertions, 30 deletions
diff --git a/samples/listener.lua b/samples/listener.lua index dff4d25..bedbde5 100644 --- a/samples/listener.lua +++ b/samples/listener.lua | |||
@@ -4,6 +4,7 @@ | |||
4 | -- Author: Diego Nehab | 4 | -- Author: Diego Nehab |
5 | -- RCS ID: $Id$ | 5 | -- RCS ID: $Id$ |
6 | ----------------------------------------------------------------------------- | 6 | ----------------------------------------------------------------------------- |
7 | require("socket") | ||
7 | host = host or "*" | 8 | host = host or "*" |
8 | port = port or 8080 | 9 | port = port or 8080 |
9 | if arg then | 10 | if arg then |
@@ -11,18 +12,10 @@ if arg then | |||
11 | port = arg[2] or port | 12 | port = arg[2] or port |
12 | end | 13 | end |
13 | print("Binding to host '" ..host.. "' and port " ..port.. "...") | 14 | print("Binding to host '" ..host.. "' and port " ..port.. "...") |
14 | s, e = socket.bind(host, port) | 15 | s = socket.try(socket.bind(host, port)) |
15 | if not s then | 16 | i, p = socket.try(s:getsockname()) |
16 | print(e) | ||
17 | exit() | ||
18 | end | ||
19 | i, p = s:getsockname() | ||
20 | print("Waiting connection from talker on " .. i .. ":" .. p .. "...") | 17 | print("Waiting connection from talker on " .. i .. ":" .. p .. "...") |
21 | c, e = s:accept() | 18 | c = socket.try(s:accept()) |
22 | if not c then | ||
23 | print(e) | ||
24 | exit() | ||
25 | end | ||
26 | print("Connected. Here is the stuff:") | 19 | print("Connected. Here is the stuff:") |
27 | l, e = c:receive() | 20 | l, e = c:receive() |
28 | while not e do | 21 | while not e do |
diff --git a/samples/talker.lua b/samples/talker.lua index 1b0652f..94d2133 100644 --- a/samples/talker.lua +++ b/samples/talker.lua | |||
@@ -4,6 +4,7 @@ | |||
4 | -- Author: Diego Nehab | 4 | -- Author: Diego Nehab |
5 | -- RCS ID: $Id$ | 5 | -- RCS ID: $Id$ |
6 | ----------------------------------------------------------------------------- | 6 | ----------------------------------------------------------------------------- |
7 | require("socket") | ||
7 | host = host or "localhost" | 8 | host = host or "localhost" |
8 | port = port or 8080 | 9 | port = port or 8080 |
9 | if arg then | 10 | if arg then |
@@ -11,18 +12,10 @@ if arg then | |||
11 | port = arg[2] or port | 12 | port = arg[2] or port |
12 | end | 13 | end |
13 | print("Attempting connection to host '" ..host.. "' and port " ..port.. "...") | 14 | print("Attempting connection to host '" ..host.. "' and port " ..port.. "...") |
14 | c, e = socket.connect(host, port) | 15 | c = socket.try(socket.connect(host, port)) |
15 | if not c then | ||
16 | print(e) | ||
17 | os.exit() | ||
18 | end | ||
19 | print("Connected! Please type stuff (empty line to stop):") | 16 | print("Connected! Please type stuff (empty line to stop):") |
20 | l = io.read() | 17 | l = io.read() |
21 | while l and l ~= "" and not e do | 18 | while l and l ~= "" and not e do |
22 | t, e = c:send(l, "\n") | 19 | socket.try(c:send(l, "\n")) |
23 | if e then | ||
24 | print(e) | ||
25 | os.exit() | ||
26 | end | ||
27 | l = io.read() | 20 | l = io.read() |
28 | end | 21 | end |
diff --git a/samples/tinyirc.lua b/samples/tinyirc.lua index b48b90a..eac979d 100644 --- a/samples/tinyirc.lua +++ b/samples/tinyirc.lua | |||
@@ -4,6 +4,7 @@ | |||
4 | -- Author: Diego Nehab | 4 | -- Author: Diego Nehab |
5 | -- RCS ID: $Id$ | 5 | -- RCS ID: $Id$ |
6 | ----------------------------------------------------------------------------- | 6 | ----------------------------------------------------------------------------- |
7 | require("socket") | ||
7 | host = host or "*" | 8 | host = host or "*" |
8 | port1 = port1 or 8080 | 9 | port1 = port1 or 8080 |
9 | port2 = port2 or 8181 | 10 | port2 = port2 or 8181 |
@@ -13,12 +14,10 @@ if arg then | |||
13 | port2 = arg[3] or port2 | 14 | port2 = arg[3] or port2 |
14 | end | 15 | end |
15 | 16 | ||
16 | server1, error = socket.bind(host, port1) | 17 | server1 = socket.try(socket.bind(host, port1)) |
17 | assert(server1, error) | 18 | server2 = socket.try(socket.bind(host, port2)) |
18 | server1:settimeout(1) -- make sure we don't block in accept | 19 | server1:settimeout(1) -- make sure we don't block in accept |
19 | server2, error = socket.bind(host, port2) | 20 | server2:settimeout(1) |
20 | assert(server2, error) | ||
21 | server2:settimeout(1) -- make sure we don't block in accept | ||
22 | 21 | ||
23 | io.write("Servers bound\n") | 22 | io.write("Servers bound\n") |
24 | 23 | ||
@@ -49,7 +48,7 @@ set:insert(server2) | |||
49 | 48 | ||
50 | while 1 do | 49 | while 1 do |
51 | local readable, _, error = socket.select(set, nil) | 50 | local readable, _, error = socket.select(set, nil) |
52 | for _, input in readable do | 51 | for _, input in ipairs(readable) do |
53 | -- is it a server socket? | 52 | -- is it a server socket? |
54 | if input == server1 or input == server2 then | 53 | if input == server1 or input == server2 then |
55 | io.write("Waiting for clients\n") | 54 | io.write("Waiting for clients\n") |
@@ -68,10 +67,12 @@ while 1 do | |||
68 | set:remove(input) | 67 | set:remove(input) |
69 | else | 68 | else |
70 | io.write("Broadcasting line '", line, "'\n") | 69 | io.write("Broadcasting line '", line, "'\n") |
71 | __, writable, error = socket.select(nil, set, 1) | 70 | writable, error = socket.skip(1, socket.select(nil, set, 1)) |
72 | if not error then | 71 | if not error then |
73 | for ___, output in writable do | 72 | for __, output in ipairs(writable) do |
74 | output:send(line .. "\n") | 73 | if output ~= input then |
74 | output:send(line .. "\n") | ||
75 | end | ||
75 | end | 76 | end |
76 | else io.write("No client ready to receive!!!\n") end | 77 | else io.write("No client ready to receive!!!\n") end |
77 | end | 78 | end |