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 self.client:receive() is called, the server will wait for 10 seconds or until it has a message, and then continue on its way.
However, this is not the behaviour I experience. The server instead instantly generates a timeout error no matter what value the timeout is set to, and doesn't wait at all for a message from the client.
I suspect I've misunderstood something. Any insight would be appreicated. Thanks.
Full code here:
Server: http://pastie.org/9659701
main: http://pastie.org/9659703
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 run telnet localhost 3333 and should see "accepted connection; waiting for data..."; if I don't send anything, I get "received error 'timeout' after 10.00 seconds.", which is exactly what I'd expect.
I'd check if there is a logic error and self.client is never nil in your case and you don't call settimeout. If this still doesn't help, make a standalone example that we can run using love2d (for example, I don't see where you call bind).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With