1

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?

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
nakiya
  • 14,063
  • 21
  • 79
  • 118
  • 1
    Maybe you're receiving a non-printable character (or `0` at the beginning)? Have you tested size of part? Perhaps the string is empty? – W.B. May 09 '13 at 11:03

2 Answers2

2

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.

hugomg
  • 68,213
  • 24
  • 160
  • 246
0

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.

Textmode
  • 509
  • 3
  • 18