1

I have a windows form application in which i am using a background worker to ftp upload files. After uploading 209 files successfully it gave error on file which only had size of 7.8kb that While Processing Img1.jpg Unable to write data to the transport connection. An existing connection was forcibly closed by the remote host.

string uri1;

ftpInfoUpload = LoadHostedSiteData(hs);
ftpInfoUpload[5] = imgRow["Filename"].ToString();

uri1 = String.Format("ftp://{0}/{1}/images/{2}", ftpInfoUpload[1], ftpInfoUpload[2], ftpInfoUpload[5]);

requestUpload = (FtpWebRequest)WebRequest.Create(uri1);
requestUpload.UsePassive = false;
requestUpload.UseBinary = true;
requestUpload.Method = WebRequestMethods.Ftp.UploadFile;
requestUpload.Credentials = new NetworkCredential(ftpInfoUpload[3], ftpInfoUpload[4]);


requestUpload.ContentLength = memStream.Length;
byte[] buff = new byte[bufferSize];
int contentLen;

// Stream to which the file to be upload is written
Stream strm = requestUpload.GetRequestStream();
memStream.Seek(0, SeekOrigin.Begin);
contentLen = memStream.Read(buff, 0, bufferSize);
                            // Till Stream content ends
while (contentLen > 0)
{   
    // Write Content from the file stream to the FTP   Upload Stream
    strm.Write(buff, 0, contentLen);
    contentLen = memStream.Read(buff, 0, bufferSize);
}

//Close the file stream and the Request Stream
strm.Close();
strm.Dispose();
ftpStream.Close();
memStream.Close();
//responseUpload.Close();
responseDownload.Close();

And ideas whats happening?

method
  • 1,369
  • 3
  • 16
  • 29
PUG
  • 4,301
  • 13
  • 73
  • 115
  • You probably exceeded a quota on the FTP server so it aborted the connection. Contact the server admin for support. – Hans Passant Jan 06 '12 at 17:06
  • @HansPassant quota for number of files? servers have a quota for the number of files user can transfer? – PUG Jan 06 '12 at 17:15
  • probably not. Rather a quota for the total amount of trafic you caused (depending on the size of the files you uploaded) or just a simple connection reset – yas4891 Jan 06 '12 at 18:18
  • @yas4891 assuming its that case, how would you suggest i deal with it please? – PUG Jan 09 '12 at 13:34
  • put the files into a queue, transfer the queued items and handle exceptions by reconnecting to the server and re-transmitting the remaining files – yas4891 Jan 09 '12 at 15:11

1 Answers1

1

I have set ftprequest.KeepAlive=true & set ftprequest.ConnectionGroupName = "Some Value", so that underlying code does not have to new create connection which have the same ftp server. I found this solution here. I also found this helpful. Also make sure not to create a new NetworkCredential object everytime you transfer a file that can cause exception. I have tested my code twice transferring 300 files and seems to work perfectly and quick. Setting KeepAlive=false can make transfers slow

Community
  • 1
  • 1
PUG
  • 4,301
  • 13
  • 73
  • 115