aboutsummaryrefslogtreecommitdiff
path: root/samples
diff options
context:
space:
mode:
Diffstat (limited to 'samples')
-rw-r--r--samples/daytimeclnt.lua6
-rw-r--r--samples/echoclnt.lua6
-rw-r--r--samples/echosrvr.lua6
-rw-r--r--samples/listener.lua4
-rw-r--r--samples/talker.lua6
-rw-r--r--samples/tinyirc.lua90
6 files changed, 75 insertions, 43 deletions
diff --git a/samples/daytimeclnt.lua b/samples/daytimeclnt.lua
index 4debc81..85ddca1 100644
--- a/samples/daytimeclnt.lua
+++ b/samples/daytimeclnt.lua
@@ -1,3 +1,9 @@
1-----------------------------------------------------------------------------
2-- UDP sample: daytime protocol client
3-- LuaSocket 1.5 sample files.
4-- Author: Diego Nehab
5-- RCS ID: $Id$
6-----------------------------------------------------------------------------
1host = host or "127.0.0.1" 7host = host or "127.0.0.1"
2port = port or 13 8port = port or 13
3if arg then 9if arg then
diff --git a/samples/echoclnt.lua b/samples/echoclnt.lua
index cd8b450..bca0b4d 100644
--- a/samples/echoclnt.lua
+++ b/samples/echoclnt.lua
@@ -1,3 +1,9 @@
1-----------------------------------------------------------------------------
2-- UDP sample: echo protocol client
3-- LuaSocket 1.5 sample files
4-- Author: Diego Nehab
5-- RCS ID: $Id$
6-----------------------------------------------------------------------------
1host = host or "localhost" 7host = host or "localhost"
2port = port or 7 8port = port or 7
3if arg then 9if arg then
diff --git a/samples/echosrvr.lua b/samples/echosrvr.lua
index 6117557..18bd84e 100644
--- a/samples/echosrvr.lua
+++ b/samples/echosrvr.lua
@@ -1,3 +1,9 @@
1-----------------------------------------------------------------------------
2-- UDP sample: echo protocol server
3-- LuaSocket 1.5 sample files
4-- Author: Diego Nehab
5-- RCS ID: $Id$
6-----------------------------------------------------------------------------
1host = host or "127.0.0.1" 7host = host or "127.0.0.1"
2port = port or 7 8port = port or 7
3if arg then 9if arg then
diff --git a/samples/listener.lua b/samples/listener.lua
index c035ab2..4846419 100644
--- a/samples/listener.lua
+++ b/samples/listener.lua
@@ -1,6 +1,8 @@
1----------------------------------------------------------------------------- 1-----------------------------------------------------------------------------
2-- Little program to dump lines received at a given port 2-- TCP sample: Little program to dump lines received at a given port
3-- LuaSocket 1.5 sample files 3-- LuaSocket 1.5 sample files
4-- Author: Diego Nehab
5-- RCS ID: $Id$
4----------------------------------------------------------------------------- 6-----------------------------------------------------------------------------
5host = host or "*" 7host = host or "*"
6port = port or 8080 8port = port or 8080
diff --git a/samples/talker.lua b/samples/talker.lua
index 688824f..c7a239a 100644
--- a/samples/talker.lua
+++ b/samples/talker.lua
@@ -1,3 +1,9 @@
1-----------------------------------------------------------------------------
2-- TCP sample: Little program to send text lines to a given host/port
3-- LuaSocket 1.5 sample files
4-- Author: Diego Nehab
5-- RCS ID: $Id$
6-----------------------------------------------------------------------------
1host = host or "localhost" 7host = host or "localhost"
2port = port or 8080 8port = port or 8080
3if arg then 9if arg then
diff --git a/samples/tinyirc.lua b/samples/tinyirc.lua
index d3e56e7..0b20303 100644
--- a/samples/tinyirc.lua
+++ b/samples/tinyirc.lua
@@ -1,16 +1,9 @@
1function set_add(set, sock) 1-----------------------------------------------------------------------------
2 table.insert(set, sock) 2-- Select sample: simple text line server
3end 3-- LuaSocket 1.5 sample files.
4 4-- Author: Diego Nehab
5function set_remove(set, sock) 5-- RCS ID: $Id$
6 for i = 1, table.getn(set) do 6-----------------------------------------------------------------------------
7 if set[i] == sock then
8 table.remove(set, i)
9 break
10 end
11 end
12end
13
14host = host or "*" 7host = host or "*"
15port1 = port1 or 8080 8port1 = port1 or 8080
16port2 = port2 or 8081 9port2 = port2 or 8081
@@ -21,49 +14,62 @@ if arg then
21end 14end
22 15
23server1, error = socket.bind(host, port1) 16server1, error = socket.bind(host, port1)
24if not server1 then print(error) exit() end 17assert(server1, error)
25server1:timeout(1) 18server1:timeout(1)
26server2, error = socket.bind(host, port2) 19server2, error = socket.bind(host, port2)
27if not server2 then print(error) exit() end 20assert(server2, error)
28server2:timeout(1) 21server2:timeout(1)
29 22
30sock_set = {server1, server2} 23function newset()
24 local reverse = {}
25 local set = {}
26 setmetatable(set, { __index = {
27 insert = function(set, value)
28 table.insert(set, value)
29 reverse[value] = table.getn(set)
30 end,
31 remove = function(set, value)
32 table.remove(set, reverse[value])
33 reverse[value] = nil
34 end,
35 id = function(set, value)
36 return reverse[value]
37 end
38 }})
39 return set
40end
41
42sockets = newset()
31 43
32sock_id = {} 44sockets:insert(server1)
33sock_id[server1] = 1 45sockets:insert(server2)
34sock_id[server2] = 2
35next_id = 3
36 46
37while 1 do 47while 1 do
38 local readable, _, error = socket.select(sock_set, nil) 48 local readable, _, error = socket.select(sockets, nil)
39 for _, sock in readable do 49 for _, input in readable do
40 -- is it a server socket 50 -- is it a server socket?
41 if sock_id[sock] < 3 then 51 local id = sockets:id(input)
42 local incomming = sock:accept() 52 if input == server1 or input == server2 then
43 if incomming then 53 local new = input:accept()
44 incomming:timeout(1) 54 if new then
45 sock_id[incomming] = next_id 55 new:timeout(1)
46 set_add(sock_set, incomming) 56 sockets:insert(new)
47 io.write("Added client id ", next_id, ". ", 57 io.write("Server ", id, " got client ", sockets:id(new), "\n")
48 table.getn(sock_set)-2, " total.\n")
49 next_id = next_id + 1
50 end 58 end
51 -- it is a client socket 59 -- it is a client socket
52 else 60 else
53 local line, error = sock:receive() 61 local line, error = input:receive()
54 local id = sock_id[sock]
55 if error then 62 if error then
56 sock:close() 63 input:close()
57 set_remove(sock_set, sock) 64 io.write("Removing client ", id, "\n")
58 io.write("Removed client number ", id, ". ", 65 sockets:remove(input)
59 getn(sock_set)-2, " total.\n")
60 else 66 else
61 io.write("Broadcasting line '", id, "> ", line, "'.\n") 67 io.write("Broadcasting line '", id, "> ", line, "'.\n")
62 __, writable, error = socket.select(nil, sock_set, 1) 68 __, writable, error = socket.select(nil, sockets, 1)
63 if not error then 69 if not error then
64 for ___, outgoing in writable do 70 for ___, output in writable do
65 io.write("Sending to client ", sock_id[outgoing], "\n") 71 io.write("Sending to client ", sockets:id(output), "\n")
66 outgoing:send(id, "> ", line, "\r\n") 72 output:send(id, "> ", line, "\r\n")
67 end 73 end
68 else io.write("No one ready to listen!!!\n") end 74 else io.write("No one ready to listen!!!\n") end
69 end 75 end