Jump to content
chkaufmann

TIdFtp - Disconnect by server

Recommended Posts

Hi,

 

I use a TIdFtp component to upload files to a server in a background thread. The main application creates changed file irregularly, sometimes there is up to 30 minutes between two uploads, sometimes there are up to a 100 files at the same time.

 

My problem is that a some point the server disconnects and calling IdFtp.Put() fails. Is there no message from the server when it disconnects? Can a get this information from the server somehow? Or what is the best way to handle that?

My first approach was doing Connect/Put/Disconnect for each file but I have one user who has a problem with his web provider because of this - when uploading too many files at the same time.

 

Christian

Share this post


Link to post
21 hours ago, chkaufmann said:

My problem is that a some point the server disconnects

Are you trying to leave TIdFTP connected to the server during idle periods when no uploads are being performed?  That is not likely to work, unless you send commands periodically, even if just a simple NOOP, so the server knows you still want the connection to stay open.

21 hours ago, chkaufmann said:

calling IdFtp.Put() fails. Is there no message from the server when it disconnects?

Like many Internet protocols, FTP is a command+response protocol.  The server can't send unsolicited messages whenever it wants, it can only send responses to commands.  So no, there is usually no message sent when the server disconnects, unless it is sent as a response to a command (the FTP protocol has a 421 error code defined for this exact purpose).  However, Indy will raise an exception when it receives an error response to a command, or tries to read from/write to the socket after it has been disconnected.

21 hours ago, chkaufmann said:

Can a get this information from the server somehow?

Usually no.

21 hours ago, chkaufmann said:

Or what is the best way to handle that?

Just have your thread perform the TIdFTP.Put() normally and handle any exception it raises, and also have it either 1) connect TIdFTP only when it has transfers to perform and then disconnect, or else 2) call TIdFTP.Noop() at fixed intervals in between periods of active transfers.

21 hours ago, chkaufmann said:

My first approach was doing Connect/Put/Disconnect for each file

You don't need to connect/disconnect for each individual file.  You can connect once, do all of the transfers you need, and then disconnect.  Just don't leave the connection idle for a long time, as the server (or a network firewall/router) is likely to close the connection.

21 hours ago, chkaufmann said:

I have one user who has a problem with his web provider because of this - when uploading too many files at the same time.

Well, then you need to add throttling to your code logic.  Don't upload more than a few files at a time.  Many servers have such a restriction.  Either have 1 thread that sleeps between transfers, or make a few threads with their own connections to the server and then queue the files and have idle threads pulling from the queue as needed.

Share this post


Link to post

Server could close inactive connection, like

Quote

11:28:05    Response:    421 Timeout.
11:28:05    Status:    Connection closed by server

 

So you can:

1) Send keep-alives (just senseless commands like NOOP, PWD, TYPE I etc. But this will burden the server with permanent connection.

2) Connect to server when it is needed and keep the connection for some reasonable time after; then disconnect

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×