Set Port number when using FtpWebRequest in C#
Asked Answered
A

3

9

I keep getting a exception when I try to FTP to my Win 2008 Server from C# code using VS2008 as debugger.

My test class looks like this:

public class FTP
{
    private string ftpServerIP = "192.168.10.35:21";
    private string ftpUserID = "Administrator";
    private string ftpPassword = "XXXXXXXX";
    private string uploadToFolder = "uploadtest";

    public void Upload(string filename)
    {
        FileInfo fileInf = new FileInfo(filename);
        string uri = "ftp://" + ftpServerIP + "/" + uploadToFolder + "/" + fileInf.Name;
        FtpWebRequest reqFTP;

        reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
        reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
        reqFTP.KeepAlive = false;
        reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
        reqFTP.UseBinary = true;
        reqFTP.ContentLength = fileInf.Length;

        int buffLength = 2048;
        byte[] buff = new byte[buffLength];
        int contentLen;

        FileStream fs = fileInf.OpenRead();
        try
        {
            Stream strm = reqFTP.GetRequestStream();
            contentLen = fs.Read(buff, 0, buffLength);

            while (contentLen != 0)
            {
                strm.Write(buff, 0, contentLen);
                contentLen = fs.Read(buff, 0, buffLength);
            }

            strm.Close();
            fs.Close();
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }
}

When I execute the code I get a Connection Failed with FTP error 227 in the GetRequestStream() call. In the exception I can see the connection fails to: 192.168.10.35:52184

I have no idea how it comes up with port 52184. I specify in the ftpServerIP that it should be port 21.

I have found a few persons with the same issues on google but I haven't found a good example on how this is solved and I still don't understand why it happens.

Anyone know how to handle this issue??

UPDATE:

I have tried to connect to a different FTP account and there it all works fine. Therefore I tested my 192.168.10.35:21 FTP but it works fine in CuteFTP Pro and the likes. This just makes it even more strange..

Arch answered 18/5, 2009 at 8:16 Comment(0)
R
7

My guess would be Windows firewall issues, FTP uses other ports than just port 21 - sometimes changing the FTP mode from active to passive helps to get things working.

reqFTP.UsePassive = false;

Look at this good article on FTP: Active FTP vs. Passive FTP, a Definitive Explanation

Ratel answered 18/5, 2009 at 8:25 Comment(1)
I think I see the difference but I fail to change from Passive to Active, does anyone have a good guide for that?Arch
A
4

Thies got it right, it had to do with passive mode

The fix in the code is so insanely simple :)

reqFTP.UsePassive = false;

And it worked fast and without errors!

Arch answered 18/5, 2009 at 9:8 Comment(0)
C
1

It is important to differentiate the COMMAND port and the DATA port. The connection protocol will also change depending if you are in ACTIVE or PASSIVE mode.

ACTIVE MODE :

1) The client initiate a connection from a random unspecified COMMAND port (N > 1023) to the default server COMMAND port (21). The client will specify his DATA port (N+1) and start listening on this port.

2) The server initiate a connection from his default DATA port (20) to specified client DATA port (N+1).

PASSIVE MODE :

1) The client initiate a connection from a random unspecified COMMAND port (N > 1023) to the default server COMMAND port (21) with the PASSIVE command. The server open a random DATA port (P > 1023) and send it to the client.

2) The client initiate a connection from his DATA port (N+1) to the specified server DATA port (P > 1023).

If you use ACTIVE mode, you will most likely need to let your client's firewall accept the connection from the server to your port (N+1 > 1024).

In your example, you were in ACTIVE mode. Your client initiated a connection from his COMMAND port (52183) to the server's default COMMAND port (21) and specified its DATA port (52184 = 52183 + 1). Then the server initiated a connection from its default DATA port (20) to the client's DATA port (52184) which was most likely rejected by the client's firewall.

I hope this helps you solve your problem!

Collincolline answered 24/1, 2017 at 22:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.