Luasocket TCP在\ r \ n \ r \ n之后停止侦听 [英] Luasocket TCP stops listening after \r\n\r\n
问题描述
每当我使用以下方式接收TCP消息时:
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
它可以很好地与\r\n
间隔的http标头一起工作,但是当我尝试对其进行http POST时,我得到的标头很好,但是数据却没有,巫婆在后面并且是空行(\ r \ n \ r \ n )
当我将其用作客户端时,它会做同样的事情:
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)
我看到了test1和test2
I see test1 and test2
我尝试弄乱setOption函数,但是没有用
I've tried messing around with the setOption function but it didnt work
有什么方法可以接收数据吗?
Is there any way to receive the data?
推荐答案
我不确定您期望什么,但是默认的模式"是读取一行,因为您的字符串"test1\r\ntest2\r\n\r\ntest3"
没有换行符最后,receive
调用等待该换行符.
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.
通常,在阅读标题之后,您已经知道要阅读的内容的长度,因此可以执行s:receive(number_of_bytes_to_read)
(至少这是我的工作,并且没有任何问题).
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).
这篇关于Luasocket TCP在\ r \ n \ r \ n之后停止侦听的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!