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

拟墨画扇 提交于 2019-12-24 13:19:50

问题


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?


回答1:


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).




回答2:


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



来源:https://stackoverflow.com/questions/17837036/luasocket-tcp-stops-listening-after-r-n-r-n

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!