I use a TIdFTP control to connect to an FTP server. It seems that my FTP connection is terminated by something (ftp server/firewall?) after 60 seconds of inactivity. Once the connection was terminated I cannot use the FTP control (TIdFTP) anymore because its status is undefined. For example, every time I try to REconnect I get the same (10038) error. However, FTP.Connected shows True. Trying FTP.Disconnect gives me a 'connection closed gracefully' error and the control remains connected. The only solution is to end the program and start it again.
The NAT Keep Alive is already set to 15000.
How to reset the status of the FTP control (how do I disconnect)?
TIdFTP
has aNATKeepAlive
property to avoid that exact issue from happening when connected through an FTP-unaware router/firewall. During a transfer, TCP keepalives are temporarily enabled on the control connection so it does not get closed prematurely.That means the
IOHandler.InputBuffer
likely has unread data in it.Connected()
is designed to return True if read operations can still be satisfied even if the socket has been closed.By default,
Disconnect()
sends aQUIT
command to the server before closing the socket. Since the connection is already gone, that send fails.Disconnect()
has an optionalANotifyPeer
parameter that you can set to False to skip theQUIT
command.You should also
Clear()
theIOHandler.InputBuffer
after an unexpected disconnect to clear out any unread data.That is never the solution. Worse case, you could simply destroy the
TIdFTP
object and create a new one. But if you follow the steps above, you should not need to resort to even that.You cannot keep TIdFTP open. The connection will be closed from server side for every timed out time.
As you use this component to fetch or put file to file server for a time interval, You can connect just before using the TIDFTP, and disconnect it after the usage. Then you do not need to bother about freeing the component and connection failures.