I have a Lua script that recursively sends commands and receives data back over a socket connection, using NSE (nmap scripting engine). It seems to usually work, until it gets a large string back, then it tends to truncate the received data. On sending the next command, the data after the truncation, that should have been received in the previous command, comes through (followed eventually by the correct data). simplified sample output below. Note "data38" is truncated and continues in next instance of the command:
"send command1"
"recieved data ="
data1
data2
data3
....
....
....
data37
da
**returning**
"send command2"
"received data ="
ta38 (should be from command1)
data39 (should be from command1)
etc etc etc
sample code is as follows:
local function blah(id)
local response
local data
local commmand
command = "dir..id"
socket:send(command)
response,data = socket:receive()
print(data)
--do recursion her depending on data results.
print "**returning**"
return
action = function(host,port)
socket = nmap.new_socket()
socket:connect(host,port)
socket:set_timeout(15000)
test = blah(id)
return test
The problem seems to be that the socket can only receive a certain number of bytes, and then returns. Socket is a global variable, as I do not want to open a new socket for every instance of "blah". Is there any way I can make the socket wait to receive all the data (until the string is null terminated for example) and then print the data??
Update I have been trying different approaches to pass a size parameter to the receive method, as stated in: http://w3.impa.br/~diego/software/luasocket/tcp.html However these seem to have no effect whatsoever eg.
response,data = socket:receive(65536)
response,data = socket:receive('a*')