Search code examples
javaftpftp-clientapache-commons-net

FTP Server connection closes in about 5 minutes


i'm using Apache commons-net 3.6 library in java

I'm making a program that accesses the ftp server at a certain interval-cycle and receives files(only one file)

When I first run the program, it works without any problem, but About 5 minutes later, when I access the file again, I print out an error message..

(If the interval-cycle is less than 5 minutes, it will operate periodically)

Of course, the connection to the server is only made at the beginning of the first

error message : org.apache.commons.net.ftp.FTPConnectionClosedException: FTP response 421 received.  Server closed connection.

I think it's a ftp-connection timeout problem

So the way I tried

FTPClient.setControlKeepAliveTimeout(sec);
FTPClient.setConnectTimeout(ms);
FTPClient.setDataTimeout(ms);
FTPClient.connect();
FTPClient.setSoTimeout(ms);

but, not working.. Error messages are still output.

Please help me :(


Solution

  • It sounds like the server is deciding to close the FTP connection after a period (5 minutes) of inactivity. This is done as a way to manage resources on the server side. Idle session are using serve memory, kernel resources, port numbers, and so on. If idle sessions are not booted of, there is a risk that the server will run out of them ... and stop working.

    Check the FTP server logs. You may need to change the server-side logging level ... temporarily ... to get more clues.

    If this is the problem, changing client-side timeouts etc, using keep-alives, etc won't help. Indeed, if you check the documentation, most of the params that you have been tweaking are about the client timing out the server, not the other way around.

    Solutions:

    • Change the FTP server configuration to allow session to be idle for longer

    • On the client side, send periodic requests to make the serve think that the session is still being used.