Keeping an FTP Session Open
We are making a timed call to an FTP folder to check for new files. Once a file appears, we download it. Is it possible to keep the session open between these timed calls, or should it be closed and re-opened every time we check? The interval is very small 2 seconds, but may run for days at a time.
The session remains open by default. An FTP server (depending on its settings) will close a control channel after some period of inactivity. Of course, in this case that’s not something to be worried about. There are three issues that immediately come to mind:
1) Your FTP session could become disconnected by external events, such as network outages. Therefore, it’s best to add logic to check the connection and auto-reconnect if necessary. You may wish to check the IsConnected property before getting the updated directory listing. ( Accessing the IsConnected property may internally cause a NOOP command to be sent to the FTP server. ) If IsConnected is false, then call Connect to re-establish the connection and if necessary, call ChangeRemoteDir to navigate back to the FTP directory.
2) The directory listing for the current remote directory is cached by the FTP2 component. To get an updated listing, call ClearDirCache prior to checking for new files.
3) Fetching a directory listing from an FTP server is technically a data transfer. The download of the directory listing happens on a separate data connection that is established for each access. (This is how the FTP protocol works, and cannot change.) In your case, you’ll be using a new port from the ephemeral port range every 2 seconds. Watch out for issues relating to running out of ports as described here: Running Out of Ports for FTP Data Transfers.