I keep getting a exception when I try to FTP to my Win 2008 Server from C# code using VS2008 as debugger.
当我尝试使用VS2008作为调试器从C#代码FTP到我的Win 2008服务器时,我一直遇到异常。
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
当我执行代码时,我在GetRequestStream()调用中遇到FTP错误227的连接失败。在异常中我可以看到连接失败: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.
我不知道如何使用端口52184.我在ftpServerIP中指定它应该是端口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..
我曾尝试连接到不同的FTP帐户,并且一切正常。因此,我测试了我的192.168.10.35:21 FTP,但它在CuteFTP Pro和其他类似的工作正常。这让它变得更加奇怪..
3 个解决方案
#1
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.
我的猜测是Windows防火墙问题,FTP使用的不仅仅是端口21的其他端口 - 有时将FTP模式从主动更改为被动有助于使事情正常工作。
reqFTP.UsePassive = false;
Look at this good article on FTP: Active FTP vs. Passive FTP, a Definitive Explanation
看看这篇关于FTP的好文章:主动FTP与被动FTP,一个明确的解释
#2
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!
它工作得很快,没有错误!
#3
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.
区分COMMAND端口和DATA端口很重要。如果您处于ACTIVE或PASSIVE模式,连接协议也会发生变化。
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.
1)客户端发起从随机未指定的COMMAND端口(N> 1023)到默认服务器COMMAND端口(21)的连接。客户端将指定其DATA端口(N + 1)并开始侦听此端口。
2) The server initiate a connection from his default DATA port (20) to specified client DATA port (N+1).
2)服务器启动从其默认DATA端口(20)到指定客户端DATA端口(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.
1)客户端使用PASSIVE命令从随机未指定的COMMAND端口(N> 1023)启动到默认服务器COMMAND端口(21)的连接。服务器打开一个随机的DATA端口(P> 1023)并将其发送到客户端。
2) The client initiate a connection from his DATA port (N+1) to the specified server DATA port (P > 1023).
2)客户端启动从其DATA端口(N + 1)到指定服务器DATA端口的连接(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).
如果使用ACTIVE模式,则很可能需要让客户端的防火墙接受从服务器到端口的连接(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.
在您的示例中,您处于ACTIVE模式。您的客户端从其COMMAND端口(52183)启动了到服务器的默认COMMAND端口(21)的连接,并指定了其DATA端口(52184 = 52183 + 1)。然后,服务器启动从其默认DATA端口(20)到客户端的DATA端口(52184)的连接,该端口很可能被客户端的防火墙拒绝。
I hope this helps you solve your problem!
我希望这可以帮助您解决问题!
#1
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.
我的猜测是Windows防火墙问题,FTP使用的不仅仅是端口21的其他端口 - 有时将FTP模式从主动更改为被动有助于使事情正常工作。
reqFTP.UsePassive = false;
Look at this good article on FTP: Active FTP vs. Passive FTP, a Definitive Explanation
看看这篇关于FTP的好文章:主动FTP与被动FTP,一个明确的解释
#2
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!
它工作得很快,没有错误!
#3
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.
区分COMMAND端口和DATA端口很重要。如果您处于ACTIVE或PASSIVE模式,连接协议也会发生变化。
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.
1)客户端发起从随机未指定的COMMAND端口(N> 1023)到默认服务器COMMAND端口(21)的连接。客户端将指定其DATA端口(N + 1)并开始侦听此端口。
2) The server initiate a connection from his default DATA port (20) to specified client DATA port (N+1).
2)服务器启动从其默认DATA端口(20)到指定客户端DATA端口(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.
1)客户端使用PASSIVE命令从随机未指定的COMMAND端口(N> 1023)启动到默认服务器COMMAND端口(21)的连接。服务器打开一个随机的DATA端口(P> 1023)并将其发送到客户端。
2) The client initiate a connection from his DATA port (N+1) to the specified server DATA port (P > 1023).
2)客户端启动从其DATA端口(N + 1)到指定服务器DATA端口的连接(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).
如果使用ACTIVE模式,则很可能需要让客户端的防火墙接受从服务器到端口的连接(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.
在您的示例中,您处于ACTIVE模式。您的客户端从其COMMAND端口(52183)启动了到服务器的默认COMMAND端口(21)的连接,并指定了其DATA端口(52184 = 52183 + 1)。然后,服务器启动从其默认DATA端口(20)到客户端的DATA端口(52184)的连接,该端口很可能被客户端的防火墙拒绝。
I hope this helps you solve your problem!
我希望这可以帮助您解决问题!