Trying to write a lua server...

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
i_love2d_you
Prole
Posts: 6
Joined: Tue Jul 29, 2014 10:15 pm

Trying to write a lua server...

Post by i_love2d_you »

Hi! This question isn't specific to LOVE, but I'm not sure where else to look.

I've been trying to do some basic networking in lua with luasocket.

I have a basic server set up, but I can't figure out how settimeout() works.

Code: Select all

function Server:run()
    print("Running.")
    self.running = true
    while self.running do
        if self.client == nil then
            print("Waiting for client.")
            self.client = self.server: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 the program to get to self.client:receive() and then wait there until it either gets a message from the client or times out after 10 seconds. However, this is not the behaviour I experience.

Instead, I just get a constant spamming of the error message "Error: timeout" as though the program is not waiting at all.
User avatar
Positive07
Party member
Posts: 1014
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: Trying to write a lua server...

Post by Positive07 »

i_love2d_you wrote:...

I expect the program to get to self.client:receive() and then wait there until it either gets a message from the client or times out after 10 seconds. However, this is not the behaviour I experience.

Instead, I just get a constant spamming of the error message "Error: timeout" as though the program is not waiting at all.
Well luasocket tries to receive, and after 10 seconds it timesout... so that's an error in the reception right? luasocket returns "false" followed by the error message... and the error was "timeout", so in your code you do:

Code: Select all

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
err has a value ("timeout") so the first part executes and prints "Error: timeout". Is that your problem?
You can just ignore timeout errors by doing

Code: Select all

local line, err = self.client:receive()
        if err and err ~= "timeout" then
            print("Error: " .. err)
        elseif line == "quit" then
            print("Quitting.")
            self.client:close()
            self.running = false        
        else
            print("Received: " .. line)
        end
    end
Also I dont recommend setting the timeout of the client but the server instead so I would do

Code: Select all

self.server:settimeout(10)
I hope that helps you.
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
i_love2d_you
Prole
Posts: 6
Joined: Tue Jul 29, 2014 10:15 pm

Re: Trying to write a lua server...

Post by i_love2d_you »

Thanks for the reply, but I'm not sure I follow.

When I call this line:

Code: Select all

local line, err = self.client:receive()
I expect it to wait 10 seconds before it times out. But it doesn't. It doesn't wait any time at all.

The guide on their site says to use client:settimeout(). Setting server:settimeout() did nothing for me (it never times out).
User avatar
Positive07
Party member
Posts: 1014
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: Trying to write a lua server...

Post by Positive07 »

I have a question, is this timeout needed? your code doesnt quit when it times out, it tries to receive again.
After it timesout it prints the error and then "while self.running do" will be true so it will do everything again.
So if timeout does nothing more than a print you can just ignore it...

Code: Select all

if err and err ~= "timeout" then
If you are doing this with love you can implement your own timeout using dt/love.timer.getDelta()

Code: Select all

love.update = function (dt)
   time = time or 0 + dt
        if time < 10 do
             self.client:receive()
        else
             print ("Error: timeout")
        end
end
or something like that
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Trying to write a lua server...

Post by bartbes »

I would like to mention you're checking for errors wrong, you should check the truthiness of line, not of err. That said, unless luasocket is acting really weirdly, that shouldn't matter.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 147 guests