File Retrieval: Using the FTP Agent (Part I)
Example 1 uses
RWFtpAgent for FTP file and directory access. The program follows these steps:
1. Creates an FTP agent object that establishes a connection with an FTP server.
2. Performs login negotiation.
4. Reads all data in the remote file from the socket portal.
5. Closes the data connection.
NOTE: Servers and files shown in the code might not exist and are included as examples only.
Example 1 – Retrieving a file
try {
RWFtpAgent agent("ftp.roguewave.com",
"anonymous", "me@roguewave.com"); // 1
RWSocketPortal sPortal = agent.get("remote_file"); // 2
RWCString packet;
while (!(packet=sPortal.recv()).isNull()) { // 3
cout << packet << endl;
}
bool dataClosed = agent.dataClose(); // 4
} // 5
catch (const RWxmsg& msg) { // 6
cout << "ERROR: " << msg.why() << endl;
}
NOTE: The dataClose() method needs to be invoked for each data transfer session. This is because the FTP protocol establishes a new data connection for each data transfer session. The connection must be removed afterwards.