I’m trying to use my RN171 module to transmit somewhat large files over wifi. I’m using a fez Raptor main board and the files I’m attempting to transfer are stored on an sd card. Using the simple http example I was able to respond using using the code following; which successfully initiates and proceeds to attempt a download through any standard browser.
var SDCard = sdCard.GetStorageDevice();
using (var file = SDCard.OpenRead(@ "\test.csv"))
{
request.Response.HeaderData["Accept-Ranges"] = "bytes";
request.Response.HeaderData["Content-Type"] = "application/octet-stream";
request.Response.HeaderData["Content-Length"] = file.Length.ToString();
request.Response.HeaderData["Connection"] = "keep-alive";
request.Response.StatusCode = GTM.GHIElectronics.HttpResponse.ResponseStatus.OK;
//All you have to do is send the document data through the response //object.
//Header data is automatically applied for you when you chose to send.
//request.Response.Send(System.Text.Encoding.UTF8.GetBytes(document));
//using(LargeBuffer buffer = new LargeBuffer((int)file.Length))
//{
// file.Read(buffer.Bytes, 0, (int)file.Length);
// request.Response.Send(buffer.Bytes);
//}
var dataRemaining = (int)file.Length;
var buffer = new byte[1024]; // adjust size to your needs
int cnt = 0;
while (dataRemaining > 0)
{
if (dataRemaining < buffer.Length)
{
buffer = new byte[dataRemaining];
}
cnt = file.Read(buffer, 0, buffer.Length);
if (dataRemaining == (int)file.Length)
{
request.Response.Send(buffer);
}
else
{
wifi_RN171.Send(buffer);
}
dataRemaining -= cnt;
}
}
Unfortunately This method has consistently left me lacking in bytes on the destination pc, and as such said download will never complete.
In an attempt to remedy this I discovered that using the code above the 1024th byte in the buffer is being repeatedly dropped. With that in mind I was able to add a 1025th byte to the buffer to achieve complete downloads However I later discovered that this is in no way scale-able as larger files grew more and more complex patterns of data loss, and smaller files risked having no data loss at all, leaving me with too much data.
I’m looking for any advice or solutions that can help solve this data loss problem.