aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDiego Nehab <diego@tecgraf.puc-rio.br>2003-06-30 06:10:02 +0000
committerDiego Nehab <diego@tecgraf.puc-rio.br>2003-06-30 06:10:02 +0000
commit167727be3cefb3f45a26b0b1d96dc94a900a84b4 (patch)
treebb07db0d01dbdebe6b25162b25fbeefc2ee19d05
parentbd54cd42d4c8a7d6bb9550d8aa0eac243cda63c0 (diff)
downloadluasocket-167727be3cefb3f45a26b0b1d96dc94a900a84b4.tar.gz
luasocket-167727be3cefb3f45a26b0b1d96dc94a900a84b4.tar.bz2
luasocket-167727be3cefb3f45a26b0b1d96dc94a900a84b4.zip
Bug in usocket_recv...
-rw-r--r--makefile.dist66
-rw-r--r--samples/tinyirc.lua42
-rw-r--r--src/auxiliar.c5
-rw-r--r--src/inet.c1
4 files changed, 62 insertions, 52 deletions
diff --git a/makefile.dist b/makefile.dist
index e493305..9cf2757 100644
--- a/makefile.dist
+++ b/makefile.dist
@@ -2,45 +2,49 @@
2# Distribution makefile 2# Distribution makefile
3#-------------------------------------------------------------------------- 3#--------------------------------------------------------------------------
4 4
5DIST = luasocket-1.5-alpha 5DIST = luasocket-2.0-alpha
6 6
7LUA = \ 7LUA = \
8 concat.lua \ 8auxiliar.lua
9 code.lua \ 9code.lua
10 url.lua \ 10concat.lua
11 http.lua \ 11ftp.lua
12 smtp.lua \
13 ftp.lua \
14 select.lua \
15 luasocket.lua
16 12
17TESTS = \ 13TESTS = \
18 testclnt.lua \ 14codetest.lua
19 testsrvr.lua \ 15concattest.lua
20 testcmd.lua \ 16ftptest.lua
21 codetest.lua \
22 urltest.lua \
23 concattest.lua \
24 ftptest.lua \
25 httptest.lua \
26 smtptest.lua \
27 mbox.lua \
28 udptest.lua
29 17
30EXAMPLES = \ 18EXAMPLES = \
31 check-links.lua \ 19check-links.lua
32 daytimeclnt.lua \ 20daytimeclnt.lua
33 echoclnt.lua \ 21echoclnt.lua
34 echosrvr.lua \ 22echosrvr.lua
35 get.lua \ 23dict.lua
36 listener.lua \
37 talker.lua \
38 tinyirc.lua
39 24
40ETC = \ 25ETC = \
41 cl-compat.lua \ 26cl-compat.lua
42 tftp.lua \ 27
43 dict.lua 28get.lua
29http.lua
30httptest.lua
31listener.lua
32lua.lua
33luasocket.lua
34mbox.lua
35noglobals.lua
36select.lua
37smtp.lua
38smtptest.lua
39talker.lua
40testclnt.lua
41test.lua
42testsrvr.lua
43tftp.lua
44tinyirc.lua
45udptest.lua
46url.lua
47urltest.lua
44 48
45MAIN = \ 49MAIN = \
46 auxiliar.c \ 50 auxiliar.c \
diff --git a/samples/tinyirc.lua b/samples/tinyirc.lua
index 0ad00ab..d9cb896 100644
--- a/samples/tinyirc.lua
+++ b/samples/tinyirc.lua
@@ -6,7 +6,7 @@
6----------------------------------------------------------------------------- 6-----------------------------------------------------------------------------
7host = host or "*" 7host = host or "*"
8port1 = port1 or 8080 8port1 = port1 or 8080
9port2 = port2 or 8081 9port2 = port2 or 8181
10if arg then 10if arg then
11 host = arg[1] or host 11 host = arg[1] or host
12 port1 = arg[2] or port1 12 port1 = arg[2] or port1
@@ -15,11 +15,16 @@ end
15 15
16server1, error = socket.bind(host, port1) 16server1, error = socket.bind(host, port1)
17assert(server1, error) 17assert(server1, error)
18server1:timeout(1) 18server1:timeout(1) -- make sure we don't block in accept
19server2, error = socket.bind(host, port2) 19server2, error = socket.bind(host, port2)
20assert(server2, error) 20assert(server2, error)
21server2:timeout(1) 21server2:timeout(1) -- make sure we don't block in accept
22 22
23io.write("Servers bound\n")
24
25-- simple set implementation
26-- the select function doesn't care about what is passed to it as long as
27-- it behaves like a table
23function newset() 28function newset()
24 local reverse = {} 29 local reverse = {}
25 local set = {} 30 local set = {}
@@ -32,46 +37,43 @@ function newset()
32 table.remove(set, reverse[value]) 37 table.remove(set, reverse[value])
33 reverse[value] = nil 38 reverse[value] = nil
34 end, 39 end,
35 id = function(set, value)
36 return reverse[value]
37 end
38 }}) 40 }})
39 return set 41 return set
40end 42end
41 43
42sockets = newset() 44set = newset()
43 45
44sockets:insert(server1) 46io.write("Inserting servers in set\n")
45sockets:insert(server2) 47set:insert(server1)
48set:insert(server2)
46 49
47while 1 do 50while 1 do
48 local readable, _, error = socket.select(sockets, nil) 51 local readable, _, error = socket.select(set, nil)
49 for _, input in readable do 52 for _, input in readable do
50 -- is it a server socket? 53 -- is it a server socket?
51 local id = sockets:id(input)
52 if input == server1 or input == server2 then 54 if input == server1 or input == server2 then
55 io.write("Waiting for clients\n")
53 local new = input:accept() 56 local new = input:accept()
54 if new then 57 if new then
55 new:timeout(1) 58 new:timeout(1)
56 sockets:insert(new) 59 io.write("Inserting client in set\n")
57 io.write("Server ", id, " got client ", sockets:id(new), "\n") 60 set:insert(new)
58 end 61 end
59 -- it is a client socket 62 -- it is a client socket
60 else 63 else
61 local line, error = input:receive() 64 local line, error = input:receive()
62 if error then 65 if error then
63 input:close() 66 input:close()
64 io.write("Removing client ", id, "\n") 67 io.write("Removing client from set\n")
65 sockets:remove(input) 68 set:remove(input)
66 else 69 else
67 io.write("Broadcasting line '", id, "> ", line, "'.\n") 70 io.write("Broadcasting line '", line, "'\n")
68 __, writable, error = socket.select(nil, sockets, 1) 71 __, writable, error = socket.select(nil, set, 1)
69 if not error then 72 if not error then
70 for ___, output in writable do 73 for ___, output in writable do
71 io.write("Sending to client ", sockets:id(output), "\n") 74 output:send(line .. "\n")
72 output:send(id, "> ", line, "\r\n")
73 end 75 end
74 else io.write("No one ready to listen!!!\n") end 76 else io.write("No client ready to receive!!!\n") end
75 end 77 end
76 end 78 end
77 end 79 end
diff --git a/src/auxiliar.c b/src/auxiliar.c
index 8b2fa37..65425c5 100644
--- a/src/auxiliar.c
+++ b/src/auxiliar.c
@@ -25,6 +25,11 @@ void aux_open(lua_State *L)
25 lua_pushnumber(L, 1); 25 lua_pushnumber(L, 1);
26 lua_rawset(L, -3); 26 lua_rawset(L, -3);
27#endif 27#endif
28 /* make version string available so scripts */
29 lua_pushstring(L, "version");
30 lua_pushstring(L, LUASOCKET_VERSION);
31 lua_rawset(L, -3);
32 /* store namespace as global */
28 lua_settable(L, LUA_GLOBALSINDEX); 33 lua_settable(L, LUA_GLOBALSINDEX);
29 /* make sure modules know what is our namespace */ 34 /* make sure modules know what is our namespace */
30 lua_pushstring(L, "LUASOCKET_LIBNAME"); 35 lua_pushstring(L, "LUASOCKET_LIBNAME");
diff --git a/src/inet.c b/src/inet.c
index 312a45b..574399c 100644
--- a/src/inet.c
+++ b/src/inet.c
@@ -177,7 +177,6 @@ const char *inet_tryconnect(p_sock ps, const char *address,
177 if (!strlen(address) || !inet_aton(address, &remote.sin_addr)) { 177 if (!strlen(address) || !inet_aton(address, &remote.sin_addr)) {
178 struct hostent *hp = gethostbyname(address); 178 struct hostent *hp = gethostbyname(address);
179 struct in_addr **addr; 179 struct in_addr **addr;
180 remote.sin_family = AF_INET;
181 if (!hp) return sock_hoststrerror(); 180 if (!hp) return sock_hoststrerror();
182 addr = (struct in_addr **) hp->h_addr_list; 181 addr = (struct in_addr **) hp->h_addr_list;
183 memcpy(&remote.sin_addr, *addr, sizeof(struct in_addr)); 182 memcpy(&remote.sin_addr, *addr, sizeof(struct in_addr));