473,219 Members | 2,148 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,219 software developers and data experts.

MemoryStream??

Hello,

I am working on an updater that Retrieves a file from a web page.
GetRequest.Method = "POST"

SW = New StreamWriter(TheRequest.GetRequestStream)
SW.WriteLine(TransferName)
SW.Close()
TheResponse = TheRequest.GetResponse()
ms = New MemoryStream()
CopyData(TheResponse.GetResponseStream(), ms)
ms.Seek(0, SeekOrigin.Begin)
System.IO.Directory.CreateDirectory(TransferFolder )
Dim st As FileStream = New FileStream(CompleteName, FileMode.Create)
ms.WriteTo(st)
st.Close()
ms.Close()

The application works good about 90% of the time I have noticed that when
the file is really big like 25 Megs, it stops sending at about 16 Megs. Is
this because the computer does not have enough memory to store the complete
file? or could it be something with the web page that is responding to the
request? or could it be something else.

Any suggestions will be greatly appreciated and even modifications to my
code above. I am not sure how to make the memory stream write to the file
every Meg to test the theory.

Thanks,
Chuck
Jul 21 '05 #1
6 2287
I receive the following Error Message.

unable to read data from transport connection

Thanks,

Chuck
"Charles A. Lackman" <Ch*****@CreateItSoftware.net> wrote in message
news:Oq**************@tk2msftngp13.phx.gbl...
Hello,

I am working on an updater that Retrieves a file from a web page.
GetRequest.Method = "POST"

SW = New StreamWriter(TheRequest.GetRequestStream)
SW.WriteLine(TransferName)
SW.Close()
TheResponse = TheRequest.GetResponse()
ms = New MemoryStream()
CopyData(TheResponse.GetResponseStream(), ms)
ms.Seek(0, SeekOrigin.Begin)
System.IO.Directory.CreateDirectory(TransferFolder )
Dim st As FileStream = New FileStream(CompleteName, FileMode.Create)
ms.WriteTo(st)
st.Close()
ms.Close()

The application works good about 90% of the time I have noticed that when
the file is really big like 25 Megs, it stops sending at about 16 Megs.
Is this because the computer does not have enough memory to store the
complete file? or could it be something with the web page that is
responding to the request? or could it be something else.

Any suggestions will be greatly appreciated and even modifications to my
code above. I am not sure how to make the memory stream write to the file
every Meg to test the theory.

Thanks,
Chuck

Jul 21 '05 #2
Replies only posted in microsoft.public.dotnet.general

Charles A. Lackman wrote:

Please don't cross-post. How did you figure
microsoft.public.dotnet.framework.windowsforms was involved in the problem?

Some good advice for getting good response can be found at
http://www.catb.org/~esr/faqs/smart-questions.html,
http://www.chiark.greenend.org.uk/~sgtatham/bugs.html
I receive the following Error Message.

unable to read data from transport connection


Please include the entire exception, you can get it by (for example):

catch(Exception e) {
Console.WriteLine("EXCEPTION:\n{0}", e);

This will include a stack-trace which makes it possible to pinpoint
where in your program the error ocurred.

BTW: It seems rather a waste to read the entire received file into
memory. Is there something wrong with just writing it directly to the disk?

What's wrong with simply:

void Copy(Stream instream, Stream outstream, byte[] buf) {
int r;
while ( true ) {
r = instream.Read(buf, 0, buf.Length);
if ( r == 0 )
break;
outstream.Write(buf, 0, r);
}
}

using ( Stream s = new FileStream(CompleteName, FileMode.Create) )
Copy(TheResponse.GetResponseStream(), s);
}
--
Helge
Jul 21 '05 #3
For one, if your intent is to store it in a file and never use it in memory
then you simply need to put it into the file stream directly. I would
recommend using a buffered loop to stream the file data from the response
stream to the file stream until there is no more data.

c# but easily translated and maybe this is what copy data is?:
FileStream fos = new FileStream(Name, FileMode.Create);
byte[] buf = new byte[4096];
int bytes = 0;
while((bytes = TheResponse.Read(buf, 0, 4096)) > 0) {
fos.Write(buf, 0, bytes);
}
fos.close();
TheResponse.close();

Do you get any exceptions for the failure? Could it be a timeout that you
are experiencing?

Fred

"Charles A. Lackman" <Ch*****@CreateItSoftware.net> wrote in message
news:Oq**************@tk2msftngp13.phx.gbl...
Hello,

I am working on an updater that Retrieves a file from a web page.
GetRequest.Method = "POST"

SW = New StreamWriter(TheRequest.GetRequestStream)
SW.WriteLine(TransferName)
SW.Close()
TheResponse = TheRequest.GetResponse()
ms = New MemoryStream()
CopyData(TheResponse.GetResponseStream(), ms)
ms.Seek(0, SeekOrigin.Begin)
System.IO.Directory.CreateDirectory(TransferFolder )
Dim st As FileStream = New FileStream(CompleteName, FileMode.Create)
ms.WriteTo(st)
st.Close()
ms.Close()

The application works good about 90% of the time I have noticed that when
the file is really big like 25 Megs, it stops sending at about 16 Megs. Is this because the computer does not have enough memory to store the complete file? or could it be something with the web page that is responding to the
request? or could it be something else.

Any suggestions will be greatly appreciated and even modifications to my
code above. I am not sure how to make the memory stream write to the file
every Meg to test the theory.

Thanks,
Chuck

Jul 21 '05 #4
Hello,

I have rewritten the stream directly to a file and still get the same error
(Freeze). I also changed the Session timeout on my server to last longer
(still same problem). Hmmm??

The Error I get is:

unable to read data from transport connection

Code For CopyData:

Private Sub CopyData(ByVal FromStream As Stream, ByVal ToStream As Stream)
Dim intBytesRead As Integer
Dim Counter As Integer = 0
Const intSize As Integer = 1024
Dim bytes(intSize) As Byte
ProgressBar1.Step = 1024
intBytesRead = FromStream.Read(bytes, 0, intSize)
While intBytesRead > 0
ToStream.Write(bytes, 0, intBytesRead)
intBytesRead = FromStream.Read(bytes, 0, intSize)
end Sub

Chuck
"Fred Hirschfeld" <a@b.c> wrote in message
news:u5**************@TK2MSFTNGP14.phx.gbl...
For one, if your intent is to store it in a file and never use it in
memory
then you simply need to put it into the file stream directly. I would
recommend using a buffered loop to stream the file data from the response
stream to the file stream until there is no more data.

c# but easily translated and maybe this is what copy data is?:
FileStream fos = new FileStream(Name, FileMode.Create);
byte[] buf = new byte[4096];
int bytes = 0;
while((bytes = TheResponse.Read(buf, 0, 4096)) > 0) {
fos.Write(buf, 0, bytes);
}
fos.close();
TheResponse.close();

Do you get any exceptions for the failure? Could it be a timeout that you
are experiencing?

Fred

"Charles A. Lackman" <Ch*****@CreateItSoftware.net> wrote in message
news:Oq**************@tk2msftngp13.phx.gbl...
Hello,

I am working on an updater that Retrieves a file from a web page.
GetRequest.Method = "POST"

SW = New StreamWriter(TheRequest.GetRequestStream)
SW.WriteLine(TransferName)
SW.Close()
TheResponse = TheRequest.GetResponse()
ms = New MemoryStream()
CopyData(TheResponse.GetResponseStream(), ms)
ms.Seek(0, SeekOrigin.Begin)
System.IO.Directory.CreateDirectory(TransferFolder )
Dim st As FileStream = New FileStream(CompleteName, FileMode.Create)
ms.WriteTo(st)
st.Close()
ms.Close()

The application works good about 90% of the time I have noticed that when
the file is really big like 25 Megs, it stops sending at about 16 Megs.

Is
this because the computer does not have enough memory to store the

complete
file? or could it be something with the web page that is responding to
the
request? or could it be something else.

Any suggestions will be greatly appreciated and even modifications to my
code above. I am not sure how to make the memory stream write to the
file
every Meg to test the theory.

Thanks,
Chuck


Jul 21 '05 #5
NOTE:

I am grabbing 4 files, three of which are under 10 megs. The larger file of
26 megs the third file to download. All the other files download just fine.
It's just the file that is over 15 Megs is size.

Chuck
"Fred Hirschfeld" <a@b.c> wrote in message
news:u5**************@TK2MSFTNGP14.phx.gbl...
For one, if your intent is to store it in a file and never use it in
memory
then you simply need to put it into the file stream directly. I would
recommend using a buffered loop to stream the file data from the response
stream to the file stream until there is no more data.

c# but easily translated and maybe this is what copy data is?:
FileStream fos = new FileStream(Name, FileMode.Create);
byte[] buf = new byte[4096];
int bytes = 0;
while((bytes = TheResponse.Read(buf, 0, 4096)) > 0) {
fos.Write(buf, 0, bytes);
}
fos.close();
TheResponse.close();

Do you get any exceptions for the failure? Could it be a timeout that you
are experiencing?

Fred

"Charles A. Lackman" <Ch*****@CreateItSoftware.net> wrote in message
news:Oq**************@tk2msftngp13.phx.gbl...
Hello,

I am working on an updater that Retrieves a file from a web page.
GetRequest.Method = "POST"

SW = New StreamWriter(TheRequest.GetRequestStream)
SW.WriteLine(TransferName)
SW.Close()
TheResponse = TheRequest.GetResponse()
ms = New MemoryStream()
CopyData(TheResponse.GetResponseStream(), ms)
ms.Seek(0, SeekOrigin.Begin)
System.IO.Directory.CreateDirectory(TransferFolder )
Dim st As FileStream = New FileStream(CompleteName, FileMode.Create)
ms.WriteTo(st)
st.Close()
ms.Close()

The application works good about 90% of the time I have noticed that when
the file is really big like 25 Megs, it stops sending at about 16 Megs.

Is
this because the computer does not have enough memory to store the

complete
file? or could it be something with the web page that is responding to
the
request? or could it be something else.

Any suggestions will be greatly appreciated and even modifications to my
code above. I am not sure how to make the memory stream write to the
file
every Meg to test the theory.

Thanks,
Chuck


Jul 21 '05 #6
Fred Hirschfeld wrote:
c# but easily translated and maybe this is what copy data is?:
FileStream fos = new FileStream(Name, FileMode.Create);
byte[] buf = new byte[4096];
int bytes = 0;
while((bytes = TheResponse.Read(buf, 0, 4096)) > 0) {
fos.Write(buf, 0, bytes);
}
fos.close();
TheResponse.close();


The above code leaks the openend file in case of exceptions, and the use
of 4096 as a constant for Read would probably be better substituted with
buf.Length.

And you would probably want some cleanup code to remove the file if
something goes wrong, something along the lines of:

Stream s = null;
try {
using ( s = new FileStream(Name, FileMode.Create) ) {
byte[] buf = new byte[size];
int r;
while ( (r = TheResponse.Read(buf,0,buf.Length) != 0 )
s.Write(buf, 0, r);
} catch ( Exception e ) {
if ( s != null )
File.Delete(Name);
throw;
}

--
Helge

Jul 21 '05 #7

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

Similar topics

1
by: xmlguy | last post by:
PREVIOUS BACKGROUND POST: I am trying to reuse a Memory Stream for loading and transforming the xml it contains Basically I have defined following interfaces: Class Render {
1
by: Arcnet | last post by:
Using MemoryStream I Have a problem to create a new Image from byte array that originaly was created from an older image (Everything is being preformed in the same Thread) //Get Image...
3
by: Nicolas | last post by:
Hi Everybody, I'm working with the MemoryStream and I'm having problems with the ReadBytes method. When I use it, this method never returns bytes!!!! MemoryStream sw = new MemoryStream();...
2
by: SQLScott | last post by:
For some reason I am drawing a blank on this, so I would be extremely greatful if someone could help me out. I am trying to get a MemoryStream out of a Byte array. How do I get it back out? ...
13
by: Don | last post by:
When I run the following code, the MemoryStream's Position is always set to 762 instead of 0, which is what I would expect: Dim bmp As Image Dim ms As MemoryStream bmp = New...
10
by: Asaf | last post by:
Hi, I am trying to Compress & Decompress a DataSet. When running this code I am receiving the error: System.Xml.XmlException was unhandled Message="Root element is missing."...
4
by: Heron | last post by:
Hi, Could someone explain me why the following code doesn't work? The memorystream always remains with length 0. MemoryStream input = new MemoryStream();
4
by: | last post by:
Hi all, I want to create a method that does the following: 1) Programmatically instantiate a new XmlDataSource control 2) For each file in a named directory, make a "FileSystemItem" element 3)...
3
by: =?Utf-8?B?UGhpbCBKb2huc29u?= | last post by:
Hi, I am using dotnet remoting with a binarry formatter. I have a property that returns a memorystream that has had a file loaded into it. When I try to access this property though I get an...
3
by: =?Utf-8?B?VmljdG9y?= | last post by:
Hi, Could you tell me can I keep the MemoryStream open and "close" the BinaryReader? As the MemoryStream is used for buffering the TCP data and BinaryReader is only used to read the...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.