| View previous topic :: View next topic |
| Author |
Message |
Elxx
Joined: 07 Dec 2005 Posts: 16
|
Posted: Wed Dec 07, 2005 4:54 pm Post subject: Sending binary data over WLAN |
|
|
I am working on a full-featured web server in Lua, and I'm having trouble sending some binary files using the WLAN functions. Here's a portion of my code:
| Code: | file = io.open(getloc, "rb")
if file then
incomingSocket:send("HTTP/1.0 200 OK\r\nContent-Type: " .. type .. "\r\n\r\n") -- Mime type was set earlier, for a PNG it will be image/png
f_size = fileSize(file) -- Uses a fileSize function I declared
graphicsPrint("File size is: " .. f_size .. "\n")
graphicsPrint("Sending file")
c_count = 0
chunksize = 128
bytes_read = 0
while true do
bytes = file:read(chunksize)
if bytes ~= nil then
incomingSocket:send(bytes)
end
c_count = c_count + 1
graphicsPrint(".")
System.sleep(5)
bytes_read = bytes_read + chunksize
if bytes_read >= f_size then break end
end
graphicsPrint("\nSent a file with " .. c_count .. " chunks of " .. chunksize .. " bytes.\n")
file:close()
end |
I can successfully send a PNG file using this and it displays on my computer. However, JPG's are either not displaying at all or breaking halfway through.
Is this a bug in the WLAN functions, or is there some way for me to package my binary data so it stays intact when I send it? Thanks in advance. |
|
| Back to top |
|
 |
Shine
Joined: 03 Dec 2004 Posts: 728 Location: Germany
|
Posted: Wed Dec 07, 2005 5:53 pm Post subject: Re: Sending binary data over WLAN |
|
|
| The sockets are all in non-blocking mode and you have to check the result of send, how many bytes were actually sent and then use string.substr and a loop to send all. |
|
| Back to top |
|
 |
|