ArxOne / FTP

Simple FTP client
MIT License
37 stars 15 forks source link

IO Exception while connection to FTP server #16

Closed gcmartijn closed 8 years ago

gcmartijn commented 8 years ago

I use multiple threads those include sometimes uploading a image. But sometimes it works, and sometimes not, they call that random ;)

The error I get is this: IO Exception while connection to FTP server

Its a fact the the target FTP directory contains many pictures, and need some time to open that directory.

the code and structure I use is like this:

while (SELECT image,.* FROM something){
    thread start (
        doheavymanythings()
    )
}

void doheavymanythings(){
  // many things here
  // including image upload
  UploadImage()
}

bool UploadImage()
    {
        try
        {
            using (var ftpClient = new FtpClient(new Uri(uploadOptions.FTPHost),
                new NetworkCredential(uploadOptions.FTPUser, uploadOptions.FTPPass),
                new FtpClientParameters
                {
                    Passive = true,
                    ChannelProtection = FtpProtection.ControlChannel
                }))
            {
                using (var s = ftpClient.Stor(uploadOptions.FTPPath + imgFileName))
                {
                    msg("FTP start upload", 0, false);
                    byte[] fileData = null;

                    try
                    {
                        fileData = File.ReadAllBytes(Uploader.Properties.Resources.imageTempPath + imgFileName);
                    }
                    catch (Exception e)
                    {
                        // NO ERROR
                        error("FTP error2 :" + e.Message);
                    }

                    if (fileData != null)
                    {
                        for (int i = 0; i < fileData.Length; i++)
                        {
                            s.WriteByte(fileData[i]);
                            msg("FTP upload [" + (i + 1) * 100 / fileData.Length + " %]", (i + 1) * 100 / fileData.Length);
                        }
                    }
                    return true;
                }
            }
        }
        catch (Exception e)
        {
           // HERE I GET THE ERROR
           // IO Exception while connection to FTP server
            error("FTP error :" + e.Message);
        }
        finally
        {
            msg();
        }

        return false;
    }
picrap commented 8 years ago

Could you give more details about the exception, such as its stack trace? Also, what does your FTP server say? What is the average load when it fails? 20 threads? 100?

gcmartijn commented 8 years ago

2 threads at the moment, but I upgraded visual studio 2013 yesterday (service pack 5) and now I don't get the error...

picrap commented 8 years ago

Glad I could help :smile: