473,406 Members | 2,467 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Streaming files

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);
totalBytesRead = fileBytes.Length;
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);

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

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

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
Jul 17 '07 #1
4 1960
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
Jul 17 '07 #2
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
Jul 19 '07 #3
On Thu, 19 Jul 2007 07:38:10 -0700, Michael
<Mi*****@discussions.microsoft.comwrote:
Then what should I do?
For receiving, you should:

* not convert your byte data to ASCII characters

There may be other things you should or should not do, but since you
didn't post all of your receiving code, it's hard to comment on what else
is wrong.

For sending, you should:

* calculate bytes read based on actual bytes read, rather than some
other variable or property
* open the file once, rather than each time through your loop
* only read the same amount of bytes each time you read
* update your index to read from based on the bytes read, rather than
on some incorrect calculation
* not have arbitrary things like "iTemp" that add 1 for no good reason
to some calculation

There may be other things you need to change, but I'd start with the above
list and see what you have at that point.

Pete
Jul 19 '07 #4
On Tue, 17 Jul 2007 09:00:01 -0700, Michael
<Mi*****@discussions.microsoft.comwrote:
>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);
totalBytesRead = fileBytes.Length;
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);

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

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

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
Is there any particular reason you're not using plain old FTP?

--
http://bytes.thinkersroom.com
Jul 19 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: A.M-SG | last post by:
Hi, I have a ASP.NET aspx file that needs to pass large images from a network storage to client browser. The requirement is that users cannot have access to the network share. The aspx file...
1
by: Vanga Sasidhar | last post by:
Hi All, I am developing a program in Visual Basic .NET 2002. There are some videos in AVI format. For the protection of these AVI files i encrypted my files with the use of DES algorithm in the...
1
by: adiela | last post by:
hello everyone..i would like to ask something regarding my project.. i've been planning to develop a website using asp code for streaming multimedia features...unfortunatly...i have zero...
5
by: Manuel Alves | last post by:
Hi, Is it possible to store media files (like windows .wmv) on SQL Server 2005 and stream it back to the client via media server (not just download it)? Regards, Manuel Alves
2
by: Cerebral Believer | last post by:
Hi All, I plan to use streaming audio on my site. I had uploaded some .mp3's as part of a trial, and these streamed very badly so I tried .ram (RealAudio) files instead. The RealAudio files...
8
by: poorna | last post by:
hi all i want to upload the video files to the server.. then i encode all the video files into flv files ... and then i am go to streaming ... in the mean while i create the thumbnail image...
4
by: Daniel Marious | last post by:
Hi, I'm looking for a .Net/COM component which would allow a .Net programmer with no streaming experience to be able to save online streams to local resources (files or to DB). I know that if...
8
by: Amjad | last post by:
Hi i am writing a application where i want to browse video file and copy data into stream and send that stream over network...I have develop P2P windows application where i successfully transfer...
3
by: Brad | last post by:
I have an aspx page that is sending pdf files to client browsers: it uses a filestream to read the pdf file and response.binarywrite to send content to the browser. This has worked great for years...
1
by: Chris Kennedy | last post by:
I want to put some mp3 files on a server and stream them through a flash player. Is there any way of locking down the files so only the flash player can see and access them - i.e. to stop people...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.