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.
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.