Re: Streaming files



Then what should I do?

"Göran Andersson" wrote:

Michael wrote:
Hello,
I need some help. I am trying to write my own web server for serving up
large files
on my own network. I am using FileStream, BinaryReader, TcpClient, and
Sockets.
I am trying to break up the large file and send it a chunck at a time. But
I keep getting
and error stating that "Error occured: An operation on a socket could not be
performed
because the system lacked sufficient buffer space or because a queue was
full".

Here is some of the code:

Socket s;
byte[] incomingBuffer;

s = this.listener.AcceptSocket();

incomingBuffer = new byte[1024]; // 1Kb
bytesRead = s.Receive(incomingBuffer);
string buffer = Encoding.ASCII.GetString(incomingBuffer);

What do you do here? You know that characters are 16 bit values, right?
And you know that the ASCII encoding only uses 7-bit character codes, right?

How are you going to use the string?


totalBytesRead = fileBytes.Length;

What does fileBytes contain? How large is it? The way that you use it,
it looks like it contains all the data from the file. Wasn't that what
you were trying to avoid?

int iCount = 1;
int lIndex = 0;
int iTemp = 1;
int iDivides = totalBytesRead / 100000;
int iRemander = totalBytesRead % 100000;



while (iCount <= iDivides)
{
FileStream fs = new FileStream(virtualDir + requestedFile, FileMode.Open,
FileAccess.Read, FileShare.Read);

Why are you opening and closing the file for each iteration of the loop?


BinaryReader reader = new BinaryReader(fs);
reader.Read(fileBytes, lIndex, (100000 * iCount));

As you are increasing iCount for every iteration in the loop, you will
be reading larger and larger pieces of the file.

As you are increasing lIndex for every iteration in the loop, you will
be reading several copies of some data into different locations of the
array.

How large is fileBytes? If you want to fit all that data into the array,
it has to be much larger than the file.


if ((numBytes = s.Send(fileBytes, fileBytes.Length, 0)) == -1)

You are sending the entire contents of the array fileBytes, not just
what you read into it. That means that you will be sending several
copies of part of the file mixed with the data that was in the array
from the beginning.

this.Log("Socket error: Cannot send packet");
else
this.Log(String.Format("Bytes sent: {0}", numBytes)); lIndex +=
(100000 + iTemp);

Why are you increasing lIndex by 100001 the first time?


iTemp = 0;
iCount++;
reader.Close();
fs.Close();
}


Can anyone give any direction? If I'm not going at it the right way,please
let me know.
One of the reasons I'm trying to break up the is because my app kept trying
to load the
whole thing into memory before it sent the file. So my memory for the app
would grow to
over a gig. I just need some way to read in the file and then stream it out.
Or stream it
as I read it.

Thanks,
Michael


--
Göran Andersson
_____
http://www.guffa.com

.



Relevant Pages

  • Re: Comparing pointers to NULL
    ... Suppose our data is an array of records with each record having many ... In C it is natural to use a struct to represent a record and an ... in the body of a loop over record number the current record number ...
    (comp.lang.c)
  • Re: C code is not generating required results.
    ... int main ... the array by the size of an element. ... prevent the user of your program from entering more characters than ... want to loop. ...
    (comp.lang.c)
  • Re: read keyboard input and storing in an array?
    ... When in a loop, how do I simply ... The data returned by 'br.readLine' would always be a String so I've stored ... simply convert it to an int as follows: ... you could copy 'myInt' to your array. ...
    (comp.lang.java.help)
  • Re: Beginners prime number generator
    ... int main{ ... int nextprime = 0; ... is typically tested by being *less than* the length of the array. ... a sqrtcalculation in the outer loop and just compare counter ...
    (comp.lang.c)
  • Re: Iterate through member variables of a class
    ... I did that because the OP was asking about iterating through member ... variables of a class by using a while loop. ... you can add or remove elements from the array and the ... So it would be better if I declared the int as a size_t instead? ...
    (comp.lang.cpp)

Loading