nakiya
nakiya

Reputation: 14403

lua socket receive reports same data multiple times

I have the following code which sits inside a loop (Simplified). cscc is a client socket connecting to a server on localhost. My server sends a single character to the client. However, I don't seem to be receiving it properly. Protocol is TCP.

    rect, _, st = socket.select({cscc}, nil, .5)

    if(rect[cscc] ~= nil) then
        data, err, part = csc:receive(512)
        if(part ~= nil) then
            print(err.." : "..part)
        end
        socket.sleep(1)
    end

When the character is sent from the server, I get the following line repeating as output:

timeout :

obviously, part is not null here. What is going on here? Why am I receiving the same thing over and over?

Upvotes: 1

Views: 451

Answers (2)

Textmode
Textmode

Reputation: 508

The LuaSocket reference documentation says:

"In case of error, the method returns nil followed by an error message which can be the string 'closed' [...] or the string 'timeout' in case there was a timeout during the operation. Also, after the error message, the function returns the partial result of the transmission."

The empty string would count as a 'partial result'.


Try testing against (part and #part > 0), which will check that part is non-nil, then check if it actually contains any bytes.

It also might be worthwhile checking with netcat or similar to make sure that your server is actually sending the data you expect.

Upvotes: 0

hugomg
hugomg

Reputation: 69924

Perhaps the server never got to really send any data at all. Check if part is non empty and see what happens if you don't pass the third parameter to socket.select.

Upvotes: 2

Related Questions