ffyhlkain
ffyhlkain

Reputation: 57

FTP upload fails on one server on passive mode

I'm having a problem with my C# FTP upload script and my new file server. The scrip I use for uploading works fine on my old file server, but throws:

System.Net.WebException: Cannot open passive data connection

when I try to upload data.

    public static bool uploadFile(string aSourceUrl, string aUserName, string aPassword, string aSourceFileName, string aTargetFtpUrl, string aFilename, bool aPassiveMode = true)
    {
        string aFileurl = aSourceUrl + "/" + aSourceFileName;
        string aTargetUrl = aTargetFtpUrl + "/" + aFilename;
        Debug.Log("creating ftp upload. Source: " + aFileurl + " Target: " + aTargetUrl);
        System.IO.FileStream aFileStream = null;
        System.IO.Stream aRequestStream = null;

        try
        {
            var aFtpClient = (FtpWebRequest) FtpWebRequest.Create(aTargetUrl);
            aFtpClient.Credentials = new NetworkCredential(aUserName, aPassword);
            aFtpClient.Method = WebRequestMethods.Ftp.UploadFile;
            aFtpClient.UseBinary = true;
            aFtpClient.KeepAlive = true;
            aFtpClient.UsePassive = aPassiveMode;

            var aFileInfo = new System.IO.FileInfo(aFileurl);
            aFtpClient.ContentLength = aFileInfo.Length;
            byte[] aBuffer = new byte[4097];
            int aBytes = 0;
            int aTotal_bytes = (int) aFileInfo.Length;
            aFileStream = aFileInfo.OpenRead();
            aRequestStream = aFtpClient.GetRequestStream();
            while (aTotal_bytes > 0)
            {
                aBytes = aFileStream.Read(aBuffer, 0, aBuffer.Length);
                aRequestStream.Write(aBuffer, 0, aBytes);
                aTotal_bytes = aTotal_bytes - aBytes;
            }
            aFileStream.Close();
            aRequestStream.Close();
            var uploadResponse = (FtpWebResponse) aFtpClient.GetResponse();
            Debug.Log(uploadResponse.StatusDescription);
            uploadResponse.Close();
            return true;
        }
        catch (Exception e)
        {
            if (aFileStream != null) aFileStream.Close();
            if (aRequestStream != null) aRequestStream.Close();

            Debug.LogError(e.ToString());
            return false;
        }
   }

When switching to active mode, I get also an exception:

System.IO.IOException: Not connected

Strange thing is: if I upload data via a ftp client it works on both servers, so my guess is that something in my script may be missing.

Does anybody have a hint for me what could be the problem? As I mentioned, the script works fine on my old server and I and my server admin think that both servers are setup similarly.

Thanks!

Upvotes: 0

Views: 2391

Answers (2)

ffyhlkain
ffyhlkain

Reputation: 57

Well, after several different attempts and debug sessions, we figured out that the script just works fine in passive mode. The server is configured to use allow only active mode though.

It seems that the FtpWebRequest doesn't allow to set ports and switches to a port the client can't use in active mode and thus failing as it's trying to open a port the server doesn't support (wants port 21).

Solution seems to be to find an alternative to the FtpWebRequest which allows to specify the used ports for active mode.

Upvotes: 0

BugFinder
BugFinder

Reputation: 17868

Passive ftp doesnt just use port 20 and 21.... passive allows for more connections but uses +1024 ports. It would need to be permitted in firewalls which is usually why it fails.

Upvotes: 0

Related Questions