The FTP protocol is designed to support a control channel, and use that control channel to tell the server to open TCP connections and transfer files.
The server sending or receiving files does NOT have to be the same as the server that the FTP control channel is connected to. It can be a "triangle" type connection.
It also allows the client to log in once on the control channel, and repeatedly tell the server to transfer files, without re-logging into the control channel.
Apparently, this concept has completely escaped MS when they created the C# FtpWebRequest
class.
I need to do exactly what the FTP protocol was designed to do:
Connect to a server
Pass in credentials
Create directories (and happily ignore an 'already exists' error)
Repeatedly transfer files up to the server
Log out of the control channel
I sure am not seeing that ability in the FtpWebRequest
class. Or anything that would appear to allow that kind of a flow in C# code.
I have looked:
and others.
But none of this seems to allow control that way it was meant to be.
I can specify the KeepAlive
property, but the loop has to repeatedly call the WebRequest.Create(targetName);
function, which would create a NEW connection, and get a NEW response. Then they fall out of scope or are orphaned, so by definition, they are destroyed. Therefore the connection MUST be closed, and then will have to be reopened. For a data connection, that's okay, but where is the ability to manipulate the CONTROL port?
The class doesn't allow the user to differentiate from a CONTROL port and a DATA port, as the FTP specification defines.
Is there a ways to use a C# class to do FTP the way it was meant to be? Because in Microsoft's narrow mindset, the whole world looks like an HTTP Get/Response protocol.
Any advice is appreciated.
-Scotty
FtpWebRequest
works on top of a connection pool. So it actually implicitly reuses an underlying FTP connection, as long as theFtpWebRequest.KeepAlive
is set to its default value oftrue
.When the
KeepAlive
is set totrue
, the underlying FTP (control) connection is not closed, when a request finishes. When you create another instance of theFtpWebRequest
with the same host, port and username, the connection from previous request(s) is reused.Take this simple example of two file upload requests to the same FTP server:
If you enable .NET network logging, you will see that only one FTP control connection is opened to the server:
edtFTPnet looks promising, as did FluentFTP
However, in the end, using a DLL assembly or library for this type of project, whether with the LGPL or MIT or anything else, opens up the possibility of a malicious attack by someone replacing the DLL with one which was injected with some form of Malware. Because the source is freely available.
In the end, it is better to write your own, with the code embedded directly in the executable. The extra effort is worth the security.
So I am marking this as answered. The comments were appreciated.