c# - Asynchronous File IO stops after a few megabytes sent -


i trying create asynchronous tpl file server using sockets , networkstream. when testing it, browser small html file (1.9 kb) sends fine, , javascript or css files links send to, won't download more html page, including flash, images, etc. receive no errors, including no connection errors. can download 96k image that's limit. set connection: keep-alive in response headers.

does know why output streaming seems stalling?

async task<> writetostream(networkstream _networkstream, string filepath, int startingpoint = 0)     {     using (filestream sourcestream = new filestream(filepath,         filemode.open, fileaccess.read, fileshare.read,         buffersize: 4096, useasync: true))         {         byte[] buffer = new byte[4096];         int numread;         while ((numread = await sourcestream.readasync(buffer, 0, buffer.length)) != 0)         {                           _networkstream.write(buffer, 0, numread);         }     } } 

i tried replacing this:

_networkstream.write(buffer, 0, numread); 

with this:

await _networkstream.writeasync(buffer, 0, numread); 

and still have same problem.

i'm using sockets because can't use httplistener or tcplistener classes since need access incoming udp , tcp requests.

i can call writetostream() simplified method:

private async void sendfileexample() {     //this method demonstration, parameters hardcoded.      // info , assemble header     string file = @"c:\www\webpage.html";     byte[] data = null;     string responsecode = "200 ok";     string contenttype = "text/html";     long datalength = 1901;     string servername = "my stack overflow server overflowing with...";      string header = string.format("http/1.1 {0}\r\n"         + "server: {1}\r\n"         + "content-length: {2}\r\n"         + "content-type: {3}\r\n"         + "connection: keep-alive\r\n"         + "\r\n",         responsecode, servername, datalength, contenttype);      var headerbytes = encoding.ascii.getbytes(header);      //send header     await _networkstream.writeasync(headerbytes, 0, headerbytes.length);      //send payload     await writetostream(_networkstream, file, 0);      //flush networkstream     await _networkstream.flushasync();              } 

edit:

here calls listen loop:

_listentask = task.factory.startnew(() => listenloop()); 

here loop spools requests, spawning client each time:

private async void listenloop() {     (; ; )     {         // wait connection         var socket = await _tcplistener.acceptsocketasync();         if (socket == null)             break;          // got new connection, create client handler         var client = new client(socket,dbinfo,frmclient);         // create task handle new connection         task.factory.startnew(client.do);     } } 

connections handled method:

public async void do() {     byte[] buffer = new byte[4096];     (; ; )     {         // read chunk of data         int bytesread = await _networkstream.readasync(buffer, 0, buffer.length);          // if read returns no data connection closed.         if (bytesread == 0)             return;          // write buffer , process request         _memorystream.seek(0, seekorigin.end);         _memorystream.write(buffer, 0, bytesread);         bool done = processheader();         if (done)             break;     }  } 

processheader() gets meta data mime types passes stream writetostream() method @ top of post.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -