net.select(socket_array, [timeout])function

Check for network events (readability, writability and errors) on the provided array of Sockets, by updating their corresponding properties Socket.canread, Socket.canwrite, Socket.failed.

Parameters

socket_array

A table containing an array of Sockets to check for network events.

[timeout]

An optional number to indicate the maximum time for select to wait for network events, in microseconds. If that argument is not provided, the function wait until a network event occurs.

Return value

  • In case of success, returns true.
  • In case of error, returns false (you can check error message with the net.error property).
  • If no networking event occured and the time limit expired, returns nil

Example

-- Multi-connection Server example import net -- create the server listening Socket local server = net.Socket("127.0.0.1", 5000) -- array of clients Sockets local sockets = {server} server.disconnected = false if server:bind() then print("Server running on 127.0.0.1:5000") else error("Network error : cannot create the server Socket") end while true do if net.select(sockets) == false then error("Network error : "..net.error) break end local alive_sockets = {} for client in each(sockets) do -- check for readability if client.canread then if client == server then if server.canread then -- check for new connection local newclient = server:accept() print(newclient.ip.." has connected") sockets[#sockets+1] = newclient end else local data = client:recv() if data == false then print(client.ip.." has disconnected") client.disconnected = true else print(client.ip..": "..tostring(data)) end end end -- check for error if client.failed then if client == server then error("fatal network error with server") else print(client.ip.." has encountered a fatal network error") client.disconnected = true end end if not client.disconnected then alive_sockets[#alive_sockets+1] = client end end -- keep only connected clients sockets = alive_sockets end