By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,156 Members | 1,026 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,156 IT Pros & Developers. It's quick & easy.

WebRequest: underlying connection was closed - what the hell?

P: n/a
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.
Dec 18 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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

SharpCoderMP wrote:
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.
Dec 18 '06 #2

P: n/a
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' :/

shriop wrote:
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

SharpCoderMP wrote:
>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.
Dec 18 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.