0

My client receives every message from the server just fine after connecting, but the server's output is always this:

Client #1 connected.
first packet error: timeout
Looping client #1
message error: timeout
Looping client #1
message error: timeout
Looping client #1
message error: timeout
...etc...

I've messed around with some values on each side. It usually does nothing, but sometimes when messing with the server's timeout values it makes it so the server just hangs forever on client:send() or client:receive(). I can never get it so that both of them can exchange messages with eachother. Why?

My goal is to have a client and server that spend minimal time (<2-3ms) every loop so that I can spend a lot of time doing other things (this was part of a bigger program but I isolated it for easier testing of the issue) on both the client or server. I can't afford to have long, blocking calls so I set the timeout really low.

Server source:

socket = require("socket");
server = assert(socket.bind("*", 534));
ip, port = server:getsockname();
server:settimeout(0.03);

nClients = 0;
clients = {};

print("Server started.");

while true do
    local client, err = server:accept();

    if (not err) then
        nClients = nClients + 1;
        print("Client #"..nClients.." connected.");

        clients[nClients] = client;
        clients[nClients]:settimeout(0.03);
        clients[nClients]:send("test");

        local msg, err = clients[nClients]:receive();
        if (not err) then
            print("we got it");
        else
            print("first packet error: "..err);
        end
    end

    for i=1,nClients do
        if (clients[i] ~= nil) then
            print("Looping client #"..i);

            local msg, err = clients[i]:receive();
            if (not err) then
                print("message received: "..msg);
            else
                print("message error: "..err);
            end

            clients[nClients]:send("another message");
        end
    end

    socket.sleep(0.01);
end

Client source:

socket = require("socket");
tcp = assert(socket.tcp());
ip, port = "127.0.0.1", 534;

print("Client started.");

tcp:settimeout(0.03);

tcp:connect(ip, port);

while true do
    local a, b, msg = tcp:receive();

    if (msg and #msg > 1) then
        print("message received: "..msg);
    end

    tcp:send("message");

    socket.sleep(0.01);
end
Accumulator
  • 873
  • 1
  • 13
  • 34
  • I don't t see how you can expect to receive anything when both sides are receiving before sending. – user207421 Oct 12 '17 at 17:01
  • The :recieve() lines should be skipped (and are being skipped) if they don't detect a message after the timeout time. It's just for checking if there is a message and if there isn't it moves on. That shouldn't be a problem? Will a message be lost if i'm not `:receive()`-ing at the *exact* point in time it arrives? It's TCP so no messages should be lost. – Accumulator Oct 12 '17 at 17:20
  • Just checked, pausing the client program WILL show a ton of messages when resuming it so it receives messages even if not `:receive()`-ing at the exact moment arrives. But the server just gives a "timeout" error when checking for new messages with `:receive()`. So there must be something wrong with the server code, that's what I came to ask, what is the problem with my server code that i can't receive messages? – Accumulator Oct 12 '17 at 17:55

0 Answers0