diff options
Diffstat (limited to 'samples')
-rw-r--r-- | samples/README | 32 | ||||
-rw-r--r-- | samples/listener.lua | 25 | ||||
-rw-r--r-- | samples/talker.lua | 22 |
3 files changed, 79 insertions, 0 deletions
diff --git a/samples/README b/samples/README new file mode 100644 index 0000000..ef5017b --- /dev/null +++ b/samples/README | |||
@@ -0,0 +1,32 @@ | |||
1 | This directory contains some sample programs using LuaSocket as well as | ||
2 | the automatic tests used to make sure the library is working properly. | ||
3 | |||
4 | The files provided are: | ||
5 | |||
6 | server.lua -- test server | ||
7 | client.lua -- test client | ||
8 | command.lua -- test command definitions | ||
9 | |||
10 | The automatic tests are composed by three files: client.lua, command.lua | ||
11 | and server.lua. To run the automatic tests on your system, make sure to | ||
12 | compile the library with _DEBUG defined (check makefile) and then open | ||
13 | two terminals. Run 'luasocket server.lua' on one of them and 'luasocket | ||
14 | client.lua' on the other. The programs should start talking to each | ||
15 | other. | ||
16 | |||
17 | listen.lua -- echo server | ||
18 | talk.lua -- echo tester | ||
19 | |||
20 | listen.lua and talk.lua are about the simplest applications you can | ||
21 | write using LuaSocket. Run 'luasocket listen.lua' and 'luasocket | ||
22 | talk.lua' on different terminals. Whatever you type on talk.lua will be | ||
23 | printed by listen.lua. | ||
24 | |||
25 | dict.lua -- dict client | ||
26 | |||
27 | The dict.lua module is a cool simple client for the DICT protocol, | ||
28 | written by Luiz Henrique Figueiredo. Just run it and enter a few words | ||
29 | to see it working. | ||
30 | |||
31 | Good luck, | ||
32 | Diego. | ||
diff --git a/samples/listener.lua b/samples/listener.lua new file mode 100644 index 0000000..a47d9a3 --- /dev/null +++ b/samples/listener.lua | |||
@@ -0,0 +1,25 @@ | |||
1 | host = "localhost" | ||
2 | port = 8080 | ||
3 | if arg then | ||
4 | host = arg[1] or host | ||
5 | port = arg[2] or port | ||
6 | end | ||
7 | print("Binding to host '" ..host.. "' and port " ..port.. "...") | ||
8 | s, i, p, e = bind(host, port) | ||
9 | if not s then | ||
10 | print(e) | ||
11 | exit() | ||
12 | end | ||
13 | print("Waiting connection from talker on " .. i .. ":" .. p .. "...") | ||
14 | c, e = s:accept() | ||
15 | if not c then | ||
16 | print(e) | ||
17 | exit() | ||
18 | end | ||
19 | print("Connected. Here is the stuff:") | ||
20 | l, e = c:receive() | ||
21 | while not e do | ||
22 | print(l) | ||
23 | l, e = c:receive() | ||
24 | end | ||
25 | print(e) | ||
diff --git a/samples/talker.lua b/samples/talker.lua new file mode 100644 index 0000000..b3313e6 --- /dev/null +++ b/samples/talker.lua | |||
@@ -0,0 +1,22 @@ | |||
1 | host = "localhost" | ||
2 | port = 8080 | ||
3 | if arg then | ||
4 | host = arg[1] or host | ||
5 | port = arg[2] or port | ||
6 | end | ||
7 | print("Attempting connection to host '" ..host.. "' and port " ..port.. "...") | ||
8 | c, e = connect(host, port) | ||
9 | if not c then | ||
10 | print(e) | ||
11 | exit() | ||
12 | end | ||
13 | print("Connected! Please type stuff (empty line to stop):") | ||
14 | l = read() | ||
15 | while l and l ~= "" and not e do | ||
16 | e = c:send(l, "\n") | ||
17 | if e then | ||
18 | print(e) | ||
19 | exit() | ||
20 | end | ||
21 | l = read() | ||
22 | end | ||