As the title says... Chat on...

User avatar
By grex
#46618 Hi,

I am trying to send a larger file over a socket but only the first chunk is being sent. My code is along the lines of the example at https://nodemcu.readthedocs.io/en/dev/en/modules/net/#netsocketsend. My code looks like this:

srv=net.createServer(net.TCP,30)
srv:listen(80,function(conn)
conn:on("receive", function(client,request)
[...]
SendFile(path,client)

[...]
function SendFile(path,client)

function sendChunk()
local buf = file.read()
print('sending')
if buf then
client:send(buf, function(conn) print('done sending') sendChuck() end)
else
print('closing client')
client:close()
file.close()
end
end

local filename = "www"..path.gsub(path,'/','_')
if file.open(filename, "r") ~= nil then
sendChunk();
else
print('file does not exist:'..filename)
Send404(client)
end
end


When I execute it on an existing file it sends the first chunk of 1024 bytes and the quits

It only prints

sending

nither 'done sending' nor 'closing client' is printed

If I try it with

client:on('sent',function(x) sendChunk() end)

right before calling the initial sendChunk its the same result.
Am I blind or is my usage of the api invalid (or maybe is there a problem in nodeMCU)
I am new to Lua and nodeMCU but not to software development.

Looking forward to any comments so I can go on with the project

Regards
grex
User avatar
By grex
#46841 Hi,

Thanks you took the time to have a look.
I tried some things last night and found a remainder of my last refactoring.
I left over another on sent in another place just closing the connection. Taking that out fixed the problem.

Thanks again for taking care