3

I would like to connect to a FTP-Server which works in the 'active-mode'. This means, the client can send the port number, on which the data-connection should be done. Usually this is a random port. (N > 1023)

In our case, it would be really nice, if we could always use a specific port for the connection to the FTP-Server. Something like '8232'.

Is this possible?

This is my code:

FtpWebRequest.DefaultWebProxy = null;
            FtpWebRequest ftpWebRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + "OurDomainOfTheFTP" + "/"));
            ftpWebRequest.Credentials = new NetworkCredential(this.benutzer, this.passwort);
            ftpWebRequest.Timeout = 5000;
            ftpWebRequest.UsePassive = false; 

            ftpWebRequest.Method = WebRequestMethods.Ftp.ListDirectory;

            WebResponse webResponse = ftpWebRequest.GetResponse();

            webResponse.Close();

Can i just write something like that?

 FtpWebRequest ftpWebRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + "OurDomainOfTheFTP" + ":8232/"));
Maximus1809
  • 575
  • 1
  • 10
  • 30

1 Answers1

3

Unfortunately the port you supply to FtpWebRequest.Create is the command port, not the data port. In your initial example the port is omitted, causing it to default to 21.

It seems FtpWebRequest does not support specifying a port number for the data connection, see this link.

This thread contains some alternative FTP (and other protocols) clients that perhaps are able to specify this.

Community
  • 1
  • 1
C.Evenhuis
  • 25,996
  • 2
  • 58
  • 72
  • Is there any solution to set the data port? Is this library maybe the right one? http://netftp.codeplex.com/ – Maximus1809 Feb 24 '14 at 15:58
  • @MaxLebold afraid I haven't tried this with any of them myself. – C.Evenhuis Feb 24 '14 at 16:02
  • Ok. I've implemented a solution where i can set a range of data-ports. Now my problem is, that a connection, which was closed, is not really closed an the port is still in use. I get the error: The specific adress is already in use. – Maximus1809 Feb 24 '14 at 20:41