Main Site Documentation

Network - HTTP - Large Files


#1

Hi Freaks :wink:
I got a problem wenn transfer large files over sockets or httplistener class. (http like)
Small files are no problem.
An error will raise when send byte per byte, 1024byte Buffer or the Complete file… Error occurs on roundabout 5000byte

Hope anybody got an idea.

Thx. Nico


#2

debug it. What errors are being thrown? What have you done to understand the problem?

I would guess you have run out of memory. You need to handle the file in small chunks not try to handle it in one complete transaction.


#3

Hi nico,

Can we see the code you’re using? (please use the code tags when posting)


#4

Oh sorry, here is the Code:

HttpListener hplistener = new HttpListener("http", Settings.NetworkPort);
 
            hplistener.Start();
 
            int akt_position = 0;
            bool is_sending = false;
 
            while (hplistener.IsListening)
            {
                HttpListenerContext hcontext = hplistener.GetContext();
                HttpListenerRequest hrequest = hcontext.Request;
                Debug.Print("** Client Request URL: " + hrequest.RawUrl);
                HttpListenerResponse hresponse = hcontext.Response;
 
                string key = hrequest.Headers["Accept"];
                string path = Settings.VolumeRoot + @ "\" + Settings.NetworkPath + @ "\" + hrequest.RawUrl.Substring(1);
 
                byte[] msg;
                if (!File.Exists(path))
                {
                    hcontext.Response.StatusCode = (int)HttpStatusCode.NotFound;
                    msg = Encoding.UTF8.GetBytes("Sorry, that page does not exist");
                }
                else
                {
                    hcontext.Response.StatusCode = (int)HttpStatusCode.OK;
                    msg = File.ReadAllBytes(path);
                }
                hcontext.Response.ContentLength64 = msg.Length;
                using (Stream s = hcontext.Response.OutputStream)
                    s.Write(msg, 0, msg.Length);
 
            }
[/Code]
Already tested with small blocks.. Same Error..

There is no out of memory exception...
the error is a socketexception error Number 10035..
The file is 13666 bytes Long.

I can Read the file complete without any Error.

Thx for any help:-)

btw my work is on my desk@ work so i will send a complete devstage with originalfiles and Detail Error messages tomorrow...

#5

Can you try this:


HttpListener hplistener = new HttpListener("http", Settings.NetworkPort);
 
            hplistener.Start();
 
            int akt_position = 0;
            bool is_sending = false;
 
            while (hplistener.IsListening)
            {
                HttpListenerContext hcontext = hplistener.GetContext();
                HttpListenerRequest hrequest = hcontext.Request;
                Debug.Print("** Client Request URL: " + hrequest.RawUrl);
                HttpListenerResponse hresponse = hcontext.Response;
 
                string key = hrequest.Headers["Accept"];
                string path = Settings.VolumeRoot + @ "\" + Settings.NetworkPath + @ "\" + hrequest.RawUrl.Substring(1);
 
                byte[] msg;
                if (!File.Exists(path))
                {
                    hresponse.StatusCode = (int)HttpStatusCode.NotFound;
                    msg = Encoding.UTF8.GetBytes("Sorry, that page does not exist");
                }
                else
                {
                    hresponse.StatusCode = (int)HttpStatusCode.OK;
                    msg = File.ReadAllBytes(path);
                }
                hresponse.ContentLength64 = msg.Length;
                using (Stream s = hresponse.OutputStream)
                    s.Write(msg, 0, msg.Length);
 
                hresponse.Close();
            }

I think I had this same error, when the browser cancelled the connection while the socket was still sending. This should normally not happen unless you hit F5 a couple of times to refresh your browser.

Again, I would like to refer to my WebServer class on fezzer. I’ve put some try/catch blocks around code that could fail when the socket connection gets aborted. See: http://www.fezzer.com/project/243/webserver-extension-for-fez-cobra/

I’m currently using this class to serve a complete directory tree of files from a SD-card without any problems. (A complete directory of files, including subdirectories, can be added to the webserver with just one call to the AddVirtualDirectory method)

It even hosts the prototype.js library which is 160kBytes


#6

thank you Wouter,
youre source is great and works perfect…

but not if i want to transmit a png file (from browser img tag)…
jpg works great…

greetz from germany

Nico


#7

Can I get the png from somewhere? I want to simulate the problem.


#8

Sorry for the late response…
i attached the png file