Sending binary data over WLAN

Discuss using and improving Lua and the Lua Player specific to the PSP.

Moderators: Shine, Insert_witty_name

Post Reply
Elxx
Posts: 16
Joined: Wed Dec 07, 2005 4:48 pm
Contact:

Sending binary data over WLAN

Post by Elxx »

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: Select all

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.
Shine
Posts: 728
Joined: Fri Dec 03, 2004 12:10 pm
Location: Germany

Re: Sending binary data over WLAN

Post by Shine »

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.
Post Reply