0

Currently I'm writing a specific program, and one of it fuctions is download/upload files via ftp protocol. I made a method for upload, but when I calls it second time, my program freezes (and after 100 seconds it shows me an timeout error). Method:

public static void uploadFile(string FTPAddress, string filePath, string username, string password)
    {
        try
        {
            FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(FTPAddress + "/" + filePath);

            request.Method = WebRequestMethods.Ftp.UploadFile;
            request.Credentials = new NetworkCredential(username, password);
            request.UsePassive = true;
            request.UseBinary = true;
            request.KeepAlive = false;

            FileStream stream = File.OpenRead(filePath);
            byte[] buffer = new byte[stream.Length];

            stream.Read(buffer, 0, buffer.Length);
            stream.Flush();
            stream.Dispose();

            using (Stream reqStream = request.GetRequestStream())
            {
                reqStream.Write(buffer, 0, buffer.Length);
                reqStream.Flush();
                reqStream.Dispose();
            }

            request.Abort();

            MessageBox.Show("Uploaded Successfully");
        }
        catch (Exception e)
        {
            if (MessageBox.Show("Error", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error) == DialogResult.Cancel)
            {
                Application.Exit();
            }
            else
                FTPTools.uploadFile({calls this function again (it doesn't matter)});
        }
    }

I call it when pushing the button:

Application.DoEvents();
FTPTools.uploadFile({my ftp address}, {filename}, {login}, {password});

By using Visual Studio Debugger I've found that freeze happens when

Stream reqStream = request.GetRequestStream()

calls. Now I don't have any ideas how to fix it. Maybe someone there would solve my problem.


UPDATED 11/11/2016: Network trace log


UPDATED 12/11/2016: Today I little updated my code, but it doesn't helped me:

public static void uploadFile(string FTPAddress, string filePath, string username, string password)
    {
        FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(FTPAddress + "/" + filePath);

        request.Method = WebRequestMethods.Ftp.UploadFile;
        request.Credentials = new NetworkCredential(username, password);
        request.UsePassive = true;
        request.UseBinary = true;
        request.KeepAlive = false;
        request.Proxy = null;
        request.Timeout = 5000;
        request.ServicePoint.ConnectionLeaseTimeout = 5000;
        request.ServicePoint.MaxIdleTime = 5000;

        try
        {
            using (FileStream stream = File.OpenRead(filePath))
            {
                byte[] buffer = new byte[stream.Length];

                stream.Read(buffer, 0, buffer.Length);
                stream.Close();

                using (Stream reqStream = request.GetRequestStream())
                {
                    reqStream.Write(buffer, 0, buffer.Length);
                    reqStream.Close();
                }
                using (FtpWebResponse resp = (FtpWebResponse)request.GetResponse())
                {
                    resp.Close();
                }
                request.Abort();
            }

            MessageBox.Show("Uploaded Successfully");
        }
        catch (Exception e)
        {
            if (MessageBox.Show("Error", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error) == DialogResult.Cancel)
            {
                Application.Exit();
            }
            else
                FTPTools.uploadFile({calls this function again (it doesn't matter)});
        }
        finally
        {
            request.Abort();
        }
    }

UPDATED 12/12/2016

Today I've found an anomaly with my code: when I call this method (that uploads file) from a method with if/else statements, it gots time-outed at second call. But when I call this method (that uploads file) from a method without if/else statements, it works correctly at every call. I have no idea why it happens, but I cannot call my method without if/else statements.

For example, this code works correctly if I call 'upl()' method:

public void upl()
{
    FTPTools.uploadFile(address, fileName, username, password);
}

And this code throws a time-out exception at second call if I call 'checker()' method:

public void upl()
{
    FTPTools.uploadFile(address, fileName, username, password);
}
public void checker()
{
    int a = 0, b = 0;
    if (a == b)
        upl();
}
StafordDev
  • 31
  • 6

0 Answers0