简体   繁体   中英

Luasocket TCP stops listening after \r\n\r\n

Whenever I receive TCP messages using something like this:

local socket=require "socket"
local sv=socket.bind("*",1337)
function love.update(dt)
    local s=sv:accept()
    while true do
        local s,e=s:receive()
        print(e or s)
    end
end

It works perfectly fine with \\r\\n spaced http headers, but when I try to http POST to it, I get the headers fine but not the data, witch is after and empty line (\\r\\n\\r\\n) it does the same thing when i use this as a client:

s.socket_write(h,"test1\r\ntest2\r\n\r\ntest3") -- (not luasocket)

I see test1 and test2

I've tried messing around with the setOption function but it didnt work

Is there any way to receive the data?

I'm not sure what you expect, but the default "mode" is to read one line and because your string "test1\\r\\ntest2\\r\\n\\r\\ntest3" doesn't have a newline in the end, the receive call waits for that newline.

Usually, after you read the headers, you already know the length of the content you want to read, so you can then do s:receive(number_of_bytes_to_read) (at least this is what I do and it works without any issues).

Have You tried filling socket.receive() pattern argument? [1]

Not sure which one is default, but You might consider using socket.receive('*a') and parsing those new lines later.

[1] http://w3.impa.br/~diego/software/luasocket/tcp.html#receive

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM