Undefined TIdFTP status after error 10038 “Socket

2019-09-07 06:37发布

问题:

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)?

回答1:

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

TIdFTP has a NATKeepAlive 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.

However, FTP.Connected shows True.

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.

Trying FTP.Disconnect gives me a 'connection closed gracefully' error

By default, Disconnect() sends a QUIT command to the server before closing the socket. Since the connection is already gone, that send fails.

Disconnect() has an optional ANotifyPeer parameter that you can set to False to skip the QUIT command.

You should also Clear() the IOHandler.InputBuffer after an unexpected disconnect to clear out any unread data.

The only solution is to end the program and start it again.

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.



回答2:

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.



标签: delphi ftp indy