How use Passive-Mode in FtpWebRequest & fix PASV error in .Net 3.5 & Define port-range by codes

SilverLight picture SilverLight · Jan 9, 2014 · Viewed 9.6k times · Source

please see my windows form codes first :

        using System;
        using System.Collections.Generic;
        using System.ComponentModel;
        using System.Data;
        using System.Drawing;
        using System.Linq;
        using System.Text;
        using System.Windows.Forms;

        namespace my_prog
        {
            public partial class Form1 : Form
            {
                public Form1()
                {
                    InitializeComponent();
                }
                string ftp_username = "goodzilla_user";
                string ftp_password = "goodzilla_pass";
                string ftp_remote_host = @"ftp://11.11.111.11";

                private void Form1_Load(object sender, EventArgs e)
                {
                    UploadFile("d:\\test.txt", ftp_remote_host + @"/test.txt", ftp_username, ftp_password);
                }

                #region UploadFile Method

                /// <summary>
                /// Methods to upload file to FTP Server
                /// </summary>
                /// <param name="_FileName">local source file name</param>
                /// <param name="_UploadPath">Upload FTP path including Host name</param>
                /// <param name="_FTPUser">FTP login username</param>
                /// <param name="_FTPPass">FTP login password</param>
                /// 
                public void UploadFile(string _FileName, string _UploadPath, string _FTPUser, string _FTPPass)
                {
                    System.IO.FileInfo _FileInfo = new System.IO.FileInfo(_FileName);

                    // Create FtpWebRequest object from the Uri provided
                    System.Net.FtpWebRequest _FtpWebRequest = (System.Net.FtpWebRequest)System.Net.FtpWebRequest.Create(new Uri(_UploadPath));

                    // Provide the WebPermission Credintials
                    _FtpWebRequest.Credentials = new System.Net.NetworkCredential(_FTPUser, _FTPPass);

                    // By default KeepAlive is true, where the control connection is not closed
                    // after a command is executed.
                    _FtpWebRequest.KeepAlive = false;

                    // set timeout for 20 seconds
                    _FtpWebRequest.Timeout = 20000;

                    // Specify the command to be executed.
                    _FtpWebRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile;

                    // Specify the data transfer type.
                    _FtpWebRequest.UseBinary = true;

                    // Notify the server about the size of the uploaded file
                    _FtpWebRequest.ContentLength = _FileInfo.Length;

                    // The buffer size is set to 2kb
                    int buffLength = 2048;
                    byte[] buff = new byte[buffLength];

                    // Opens a file stream (System.IO.FileStream) to read the file to be uploaded
                    System.IO.FileStream _FileStream = _FileInfo.OpenRead();

                    try
                    {
                        // Stream to which the file to be upload is written
                        System.IO.Stream _Stream = _FtpWebRequest.GetRequestStream();

                        // Read from the file stream 2kb at a time
                        int contentLen = _FileStream.Read(buff, 0, buffLength);

                        // Till Stream content ends
                        while (contentLen != 0)
                        {
                            // Write Content from the file stream to the FTP Upload Stream
                            _Stream.Write(buff, 0, contentLen);
                            contentLen = _FileStream.Read(buff, 0, buffLength);
                        }

                        // Close the file stream and the Request Stream
                        _Stream.Close();
                        _Stream.Dispose();
                        _FileStream.Close();
                        _FileStream.Dispose();

                        MessageBox.Show("Done");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Upload Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }

                #endregion

            }
        }

i am using UploadFile method for uploading my data to my windows server 2008 r2 server.
these codes in .net 4 work perfect and my problem is about .net 3.5.
in .net 3.5 i got this error :

"The server returned an address in response to the PASV command that is different than the address to which the FTP connection was made."

for below reasons i do n't want to use active mode :

  1. as you know passive mode is better that active mode for connectivity...

  2. when i am using active mode in .net 3.5 and turn the Proxy Software on i got the error below :

"The underlying connection was closed: The server committed a protocol violation."

but .net 4 has no problem with that Proxy Software and passive mode and i can not switch to .net 4 because of my users...
so how can i fix passive mode error in .net 3.5?
in every thread in stack people say just use :

    _FtpWebRequest.UsePassive = false;    

and this is not my answer!

Note : firewall in both server and client is off


The OTHER QUESTION IS :

is it possible to define port-range of pssive mode by codes?
i asked this question in this thread because i thought by doing that we can fix that PASV error and help passive-mode to do it's job faster...



EDIT :
i found the thread below & i think i have the situation in reply #2,
ftp-problem
i have two network adapters in my server and the ip of each one inside server is like 192.168.5.?? & 192.168.5.??
but my two public ip addresses are different.
so how can i fix that error by changing something in my codes or in my windows server 2008-r2 VPS and why that error only appears in .net 3.5 and in .net 4 we don't have it?
i have full access to my server and can change every thing that is necessary.

thanks in advance

Answer

SilverLight picture SilverLight · Jan 10, 2014

Here is your answer :
it seems that problem has nothing to do with .net 3.5 and .net 4
you can fix that problem inside server like below
configuring-ftp-firewall-settings-in-iis-7
for proxy software error : change Port Ranges.
for passive error : change External IP Address of Firewall to your public ip address.

img

EDIT :
would be really appreciate to other people learn us is it possbile to define port-ranges in code behind or not?