套接字接收循环永远不会返回
我有一个从 Lua 中的套接字读取的循环:
socket = nmap.new_socket()
socket:connect(host, port)
socket:set_timeout(15000)
socket:send(command)
repeat
response,data = socket:receive_buf("\n", true)
output = output..data
until data == nil
基本上,数据的最后一行不包含“\n”字符,因此永远不会从套接字读取。但这个循环只是挂起并且永远不会完成。我基本上需要它在“\n”分隔符未被识别时返回。有谁知道如何做到这一点?
干杯
已更新 包含套接字代码
Update2
好的,我已经通过使用“receive_bytes”方法解决了等待“\n”字符的最初问题。
新代码:
--socket set as above
repeat
data = nil
response,data = socket:receive_bytes(5000)
output = output..data
until data == nil
return output
这有效,我得到了完整的大数据块。但我需要将缓冲区大小从 5000 字节减少,因为这是在递归函数中使用的,内存使用率可能会非常高。然而,我的“直到”条件仍然存在问题,如果我将缓冲区大小减少到需要该方法循环的大小,它只会在一次迭代后挂起。
更新3 我已经使用 string.match 和 receive_bytes 解决了这个问题。我一次至少接收 80 个字节。然后 string.match 检查数据变量是否包含某种模式。如果是这样,则退出。它不是最干净的解决方案,但它可以满足我需要它做的事情。这是代码:
repeat
response,data = socket:receive_bytes(80)
output = output..data
until string.match(data, "pattern")
return output
I have a loop that reads from a socket in Lua:
socket = nmap.new_socket()
socket:connect(host, port)
socket:set_timeout(15000)
socket:send(command)
repeat
response,data = socket:receive_buf("\n", true)
output = output..data
until data == nil
Basically, the last line of the data does not contain a "\n" character, so is never read from the socket. But this loop just hangs and never completes. I basically need it to return whenever the "\n" delimeter is not recognised. Does anyone know a way to do this?
Cheers
Updated
to include socket code
Update2
OK I have got around the initial problem of waiting for a "\n" character by using the "receive_bytes" method.
New code:
--socket set as above
repeat
data = nil
response,data = socket:receive_bytes(5000)
output = output..data
until data == nil
return output
This works and I get the large complete block of data back. But I need to reduce the buffer size from 5000 bytes, as this is used in a recursive function and memory usage could get very high. I'm still having problems with my "until" condition however, and if I reduce the buffer size to a size that will require the method to loop, it just hangs after one iteration.
Update3
I have gotten around this problem using string.match and receive_bytes. I take in at least 80 bytes at a time. Then string.match checks to see if the data variable conatins a certain pattern. If so it exits. Its not the cleanest solution, but it works for what I need it to do. Here is the code:
repeat
response,data = socket:receive_bytes(80)
output = output..data
until string.match(data, "pattern")
return output
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信在套接字中处理这种情况的唯一方法是设置超时。
以下链接有一些信息,但它是在http套接字上的:lua http套接字超时
还有这个(9.4 - 非抢占式多线程): http://www.lua.org/pil/9.4.html
这个问题:http://lua-list.2524044.n2.nabble.com/luasocket-howto-read-write-Non-blocking-TPC-socket-td5792021.html
可以找到关于Socket的很好的讨论在这个链接上:
http://nitoprograms.blogspot.com/2009/04/tcpip-net-sockets-faq.html
它是.NET,但概念是通用的。
I believe the only way to deal with this situation in a socket is to set a timeout.
The following link has a little bit of info, but it's on http socket: lua http socket timeout
There is also this one (9.4 - Non-Preemptive Multithreading): http://www.lua.org/pil/9.4.html
And this question: http://lua-list.2524044.n2.nabble.com/luasocket-howto-read-write-Non-blocking-TPC-socket-td5792021.html
A good discussion on Socket can be found on this link:
http://nitoprograms.blogspot.com/2009/04/tcpip-net-sockets-faq.html
It's .NET but the concepts are general.
请参阅更新 3。因为数据的最后一部分始终是相同的模式,所以我可以读取一个字节块,并每次检查该块是否具有该模式。如果它具有模式,则意味着这是数据的结尾,附加到输出变量并退出。
See update 3. Because the last part of the data is always the same pattern, I can read in a block of bytes and each time check if that block has the pattern. If it has the pattern it will mean that it is the end of the data, append to the output variable and exit.