FXP Transfers
FXP transfers require the following basic steps:
- Create a connection to two FTP servers.
- Server 1 will use a passive data connection.
- Specify the address and port of the data connection on Server 2 using information from the response from Server 1's EPSV command.
- Send the STOR command on Server 1, which will be receiving the file, and RETR on Server 2, which will be sending the file.
First, implement the PITrail event to receive and parse the response from the server. This example will assume that the EPSV command was sent, so a 229 response is expected. Note that this can also be accomplished with a PASV command, but only when using IPv4.
ftpA = new Ftp();
string[] portdata;
ftpA.OnPITrail += new Ftp.OnPITrailHandler(delegate(object sender, FtpPITrailEventArgs e)
{
Console.WriteLine("FTPA:\t" + e.Message);
//The 229 response will be in this format:
//
Once the PITrail event is ready to parse the response to the EPSV command, log in, and send it.
//ftpA.Config("UseIPv6=true"); //Set UseIPv6 to true if necessary
ftpA.RemoteHost = "192.168.0.101";
ftpA.RemotePort = 21;
ftpA.User = "user";
ftpA.Password = "password";
ftpA.Passive = true;
ftpA.Logon();
ftpA.Command = "EPSV";
Now that the port that Server 1 is listening on is known, connect to Server 2, create an active data connection and issue the STOR and RETR commands.
//Since we determined what port ftpA is listening on, we can start the RETR on ftpB
System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(FTPRetriever), portdata);
ftpA.Command = "STOR test.txt\r\n"; //Store the file on Server 1
//done..disconnect Server 1
ftpA.Logoff();
static void FTPRetriever(object parameter)
{
string[] portdata = (string[])parameter;
Ftp ftpB = new Ftp();
ftpB.OnPITrail += new Ftp.OnPITrailHandler(delegate(object sender, FtpPITrailEventArgs e)
{
Console.WriteLine("FTPB:\t" + e.Message);
});
//ftpB.Config("UseIPv6=true"); //Set UseIPv6 to true if necessary
ftpB.Timeout = 5;
ftpB.RemoteHost = "192.168.0.102";
ftpB.User = "user";
ftpB.Password = "password";
ftpB.Passive = false;
ftpB.Logon();
//The server expects the EPRT command to be in the following format:
//EPRT
We appreciate your feedback. If you have any questions, comments, or suggestions about this article please contact our support team at support@nsoftware.com.