WebRequest: underlying connection was closed - what the hell?

S

SharpCoderMP

I'm having troubles with uploading files to the ftp server using
FtpWebRequest. I occasionally get an Exception saying: "The underlying
connection was closed: An unexpected error occurred on a receive". but
what's wired i get this exception just after upload is finished when i
try to close the stream that was used for upload. this happens only when
upload takes more that certain amount of time. note that upload itself
goes smooth no matter how long it takes. here is a code example:

FileInfo fi = ObtainFile(...);
FtpWebRequest ftp = ObtainRequest(...);

const int BufferSize = 2048;
byte[] content = new byte[BufferSize - 1 + 1];
int dataRead = 0;
//open file for reading
using (FileStream fs = fi.OpenRead())
{
try
{
//open request to send
using (Stream rs = ftp.GetRequestStream())
{
do
{
dataRead = fs.Read(content, 0, BufferSize);
rs.Write(content, 0, dataRead);
bytesSent += dataRead;
} while (!(dataRead < BufferSize) || bytesSent < fiSize);
} // EXCEPTION IS THROWN HERE when stream is being closed.
}
catch (Exception....)
{
if (bytesSent == fiSize)
{ /* we need to ignore exception cause transfer was completed */ }
else
{ /* handle the exception */ }
}
}

any ideas how to prevent this? it seems that exception is thrown due to
the timeout because it takes quite long after upload finished and the
exception is thrown - program 'hangs' for some time after it leaves do
while loop until exception is thrown.
 
S

shriop

Try changing your while condition to

} while (dataRead > 1);

Not positive that will fix your situation, but it should be more
correct than what you're currently using and it might shed some light
on the issue.

Bruce Dunwiddie
http://www.csvreader.com
 
S

SharpCoderMP

but the problem is not the loop itself. loop condition has nothing to do
here. problem is with the connection that seems to close immediately
after last sent byte what causes an exception when code leaves 'using'
statement. at least that is what i 'think' :/
Try changing your while condition to

} while (dataRead > 1);

Not positive that will fix your situation, but it should be more
correct than what you're currently using and it might shed some light
on the issue.

Bruce Dunwiddie
http://www.csvreader.com
I'm having troubles with uploading files to the ftp server using
FtpWebRequest. I occasionally get an Exception saying: "The underlying
connection was closed: An unexpected error occurred on a receive". but
what's wired i get this exception just after upload is finished when i
try to close the stream that was used for upload. this happens only when
upload takes more that certain amount of time. note that upload itself
goes smooth no matter how long it takes. here is a code example:

FileInfo fi = ObtainFile(...);
FtpWebRequest ftp = ObtainRequest(...);

const int BufferSize = 2048;
byte[] content = new byte[BufferSize - 1 + 1];
int dataRead = 0;
//open file for reading
using (FileStream fs = fi.OpenRead())
{
try
{
//open request to send
using (Stream rs = ftp.GetRequestStream())
{
do
{
dataRead = fs.Read(content, 0, BufferSize);
rs.Write(content, 0, dataRead);
bytesSent += dataRead;
} while (!(dataRead < BufferSize) || bytesSent < fiSize);
} // EXCEPTION IS THROWN HERE when stream is being closed.
}
catch (Exception....)
{
if (bytesSent == fiSize)
{ /* we need to ignore exception cause transfer was completed */ }
else
{ /* handle the exception */ }
}
}

any ideas how to prevent this? it seems that exception is thrown due to
the timeout because it takes quite long after upload finished and the
exception is thrown - program 'hangs' for some time after it leaves do
while loop until exception is thrown.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top