1

Ok, finally I need some help... I'm trying to set up a lua server which is listening to several clients at the same time.

We are programming an Android game using lua and this game has to communicate with my server. I need to send and receive data from players and store it in a MySQL database. But my biggest problem is, that luasocket can't handle multiple clients natively, so I tried to use Copas.

I'm testing around with Copas using this tutorial/manual: Copas Manual but my code doesn't even listen for incomming connections...so I've inserted some print commands to see where my code hangs. Could someone help me?

local socket  = require("socket")
local copas   = require("copas")
local server1 = assert(socket.bind("*", 2906))
local server2 = assert(socket.bind("*", 2907))
local server3 = assert(socket.bind("*", 2908))

--Register servers
copas.addserver(server1, echo1)
copas.addserver(server2, echo2)
copas.addserver(server3, echo3)

--Simple echo handler
function echo1(skt)
    print("1")
    reqdata = copas.receive(skt, pattern)
    copas.send(skt, respdata)
end

function echo2(skt)
    print("2")
    reqdata = copas.receive(skt, pattern)
    copas.send(skt, respdata)
end

function echo3(skt)
    print("3")
    reqdata = copas.receive(skt, pattern)
    copas.send(skt, respdata)
end

    print("4")
reading = {server}
    print("4.1")

while true do
    print("4.2")
input = socket.select(reading)
    print("4.3")
skt = input:accept()
    print("4.4")
newthread(echo1(skt))
    print("4.5")
end

print("5")
copas.loop()
print("6")
  • 2
    "so I've inserted some print commands to see where my code hangs" ... and? Where does it hang? What happens exactly? – Etan Reisner Dec 29 '15 at 14:27
  • when you call `addServer` handlers are not defined yet. – moteus Dec 29 '15 at 16:33
  • @EtanReisner Oh, sorry...it hangs at "4.2" I get printed "4", "4.1" and "4.2"...and then it hangs forever – Martin König Dec 29 '15 at 17:11
  • @moteus Ok, but I thought that my little echo functions are those handlers. How should I define those handlers correctly? Thanks! – Martin König Dec 29 '15 at 17:11
  • @moteus's point was that when the `copas.addserver(server1, echo1)` line runs there's no value in the `echo1` variable. So you are effectively calling `copas.addserver(server1, nil)` or `copas.addserver(server1)` which clearly doesn't do what you want it to. You need to define your functions first. – Etan Reisner Dec 29 '15 at 17:15
  • @EtanReisner Ahhh! Sure! Oh boy...it´s that simple :D I will try it in a moment. Thank you very much! Hope it works... – Martin König Dec 29 '15 at 17:18
  • @EtanReisner Nope, still not working. It still hangs at 4.2 http://i.imgur.com/FNsFaGj.png – Martin König Dec 29 '15 at 17:34
  • What is `server` that you are putting in the `reading` table? Did you mean to put `server1`, `server2` and `server3` in there instead? – Etan Reisner Dec 29 '15 at 17:36
  • `server` is a variable (or something else) from the Copas manual. I'm not an expert. I thought that `server` is something like an array which is handled or generated by Copas, based on my added servers (server1, server2, server3). I'm not quiet sure whether this is right or not to define three servers with three ports...but this is what I've understood. This is a quote from the manual: _First each server socket and its corresponding handler function have to be registered with Copas:_ – Martin König Dec 29 '15 at 17:45

1 Answers1

0

Your script blocks on 4.2 step, because you use select in an incorrect way. select needs to take a table of sockets it checks, so you need to use servers = {server1, server2, server3} and pass this table to select.

select then returns a table with the list of sockets in "readable" and "writable" states you can iterate. Something along these lines:

local servers = {server1, server2, server3}
local canread, canwrite, error = socket.select(servers, nil, 1)
for _, input in ipairs(canread) do
  local client = input:accept()
  ...
end

See the example from luasocket distribution and my answer to a similar question for details.

Community
  • 1
  • 1
Paul Kulchenko
  • 25,884
  • 3
  • 38
  • 56