mirror of
				https://github.com/lunarmodules/luasocket.git
				synced 2025-10-31 02:15:38 +01:00 
			
		
		
		
	Local index in for loop was getting changed...
This commit is contained in:
		| @@ -1,11 +1,11 @@ | |||||||
| function set_add(set, sock) | function set_add(set, sock) | ||||||
|     tinsert(set, sock) |     table.insert(set, sock) | ||||||
| end | end | ||||||
|  |  | ||||||
| function set_remove(set, sock) | function set_remove(set, sock) | ||||||
|     for i = 1, getn(set) do |     for i = 1, table.getn(set) do | ||||||
|         if set[i] == sock then |         if set[i] == sock then | ||||||
|            tremove(set, i) |            table.remove(set, i) | ||||||
|            break |            break | ||||||
|         end |         end | ||||||
|     end  |     end  | ||||||
| @@ -20,10 +20,10 @@ if arg then | |||||||
|     port2 = arg[3] or port2 |     port2 = arg[3] or port2 | ||||||
| end | end | ||||||
|  |  | ||||||
| server1, error = bind(host, port1) | server1, error = socket.bind(host, port1) | ||||||
| if not server1 then print(error) exit() end | if not server1 then print(error) exit() end | ||||||
| server1:timeout(1) | server1:timeout(1) | ||||||
| server2, error = bind(host, port2) | server2, error = socket.bind(host, port2) | ||||||
| if not server2 then print(error) exit() end | if not server2 then print(error) exit() end | ||||||
| server2:timeout(1) | server2:timeout(1) | ||||||
|  |  | ||||||
| @@ -35,7 +35,7 @@ sock_id[server2] = 2 | |||||||
| next_id = 3 | next_id = 3 | ||||||
|  |  | ||||||
| while 1 do | while 1 do | ||||||
|     local readable, _, error = select(sock_set, nil) |     local readable, _, error = socket.select(sock_set, nil) | ||||||
|     for _, sock in readable do |     for _, sock in readable do | ||||||
|         -- is it a server socket |         -- is it a server socket | ||||||
|         if sock_id[sock] < 3 then |         if sock_id[sock] < 3 then | ||||||
| @@ -44,8 +44,8 @@ while 1 do | |||||||
|                 incomming:timeout(1) |                 incomming:timeout(1) | ||||||
| 				sock_id[incomming] = next_id | 				sock_id[incomming] = next_id | ||||||
|                 set_add(sock_set, incomming)  |                 set_add(sock_set, incomming)  | ||||||
|                 write("Added client id ", next_id, ". ",  |                 io.write("Added client id ", next_id, ". ",  | ||||||
| 					getn(sock_set)-2, " total.\n") | 					table.getn(sock_set)-2, " total.\n") | ||||||
| 				next_id = next_id + 1 | 				next_id = next_id + 1 | ||||||
|             end |             end | ||||||
|         -- it is a client socket |         -- it is a client socket | ||||||
| @@ -55,17 +55,17 @@ while 1 do | |||||||
|             if error then  |             if error then  | ||||||
|                 sock:close() |                 sock:close() | ||||||
|                 set_remove(sock_set, sock)  |                 set_remove(sock_set, sock)  | ||||||
|                 write("Removed client number ", id, ". ", |                 io.write("Removed client number ", id, ". ", | ||||||
| 					getn(sock_set)-2, " total.\n") | 					getn(sock_set)-2, " total.\n") | ||||||
|             else |             else | ||||||
|             	write("Broadcasting line '", id, "> ", line, "'.\n") |             	io.write("Broadcasting line '", id, "> ", line, "'.\n") | ||||||
|             	_, writable, error = select(nil, sock_set, 1) |             	__, writable, error = socket.select(nil, sock_set, 1) | ||||||
|             	if not error then |             	if not error then | ||||||
|                 	for _, outgoing in writable do |                 	for ___, outgoing in writable do | ||||||
|                         write("Sending to client ", sock_id[outgoing], "\n") |                         io.write("Sending to client ", sock_id[outgoing], "\n") | ||||||
|                     	outgoing:send(id, "> ", line, "\r\n") |                     	outgoing:send(id, "> ", line, "\r\n") | ||||||
|                 	end |                 	end | ||||||
|             	else write("No one ready to listen!!!\n") end |             	else io.write("No one ready to listen!!!\n") end | ||||||
| 			end | 			end | ||||||
|         end |         end | ||||||
|     end |     end | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user