How does luasocket settimeout () work?

I have the following code:

function Server:run()
    print("Running.")
    self.running = true
    while self.running do
        if self.client == nil then
            self.client = self.socket:accept()
            print("Client connected.")
            self.client:settimeout(10)
        end
        local line, err = self.client:receive()
        if err then
            print("Error: " .. err)
        elseif line == "quit" then
            print("Quitting.")
            self.client:close()
            self.running = false        
        else
            print("Received: " .. line)
        end
    end
    self:terminate()
end

      

I expect that when calling self.client: receive (), the server will wait 10 seconds or until a message appears and then continue on its way.

However, this is not the behavior I am experiencing. Instead, the server generates an instant timeout error no matter what the timeout value is, and does not wait at all for a message from the client.

I suspect I have misunderstood something. Any understanding will be appreciated. Thank.


The complete code is here:

Server: http://pastie.org/9659701

main: http://pastie.org/9659703


+3


source to share


1 answer


The code works for me as expected (Windows, LuaJIT 2.0.2, luasocket 3.0-rc1); I tested on the following standalone script:

local socket = require "socket"
local server = assert(socket.bind("*", 3333))
local client = server:accept()
print("accepted connection; waiting for data...")
client:settimeout(10)
local start = os.time()
local line, err, partial = client:receive("*l")
if line then
  print(("received '%s'; echoing back..."):format(line))
  client:send(line.."\n")
else
  print(("received error '%s' after %.2f seconds."):format(err, os.time()-start))
end
client:close()

      



You can start telnet localhost 3333

and see "accepted connection, waiting for data ..."; if I don't send anything, I get a “received error message” after 10.00 seconds. ”This is exactly what I expect.

I would check to see if there is a boolean error and self.client

never nil

in your case and you don't call settimeout

. If that still doesn't help, please make a separate example that we can run with love2d (for example, I can't see where you are calling bind

).

0


source







All Articles