commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Rick Herrick" <>
Subject Re: [FTPClient] : No errors occured when I stop the ftp server during a transfer
Date Wed, 27 Jul 2005 16:22:30 GMT

This is happening because the FTP client by default doesn't time out, so
it just sits there waiting for something to happen.  You need to:

1) Make sure you're using Java 1.4 or later.  The Socket timeouts don't
actually do anything in 1.3 or earlier, or so I've been told.  I'm using
at least 1.4 in all my dev environments, so I haven't tested this in
earlier versions of the JVM.  It definitely works in 1.4 and 1.5.

2) Create a custom socket factory that sets the timeout when the socket is
created.  Basically, each one of the createSocket() methods that you
implement in there looks like this:

public Socket createSocket(String host, int port) throws
UnknownHostException, IOException
  Socket socket = new Socket();

  SocketAddress socketAddress = new InetSocketAddress(host, port);
  socket.connect(socketAddress, timeout);
  return socket;

Set the socket factory on your FTPClient using the setSocketFactory()
method, which is inherited from SocketClient.  This looks something like

FTPClient client = new FTPClient();
MySocketFactory factory = new MySocketFactory();
factory.setDefaultTimeout(30000); // time in ms

3) Once you've created your FTPClient object, set all of the different
timeouts on it (the one set in the SocketFactory affects the initial
connection timeout, I think).  So setDataTimeout(), setDefaultTimeout(),

// Set the ftp timeout values on the client

4) When you do an operation that may time out, e.g. retrieveFile() or
retrieveFileStream(), wrap that in a try/catch, catching the
SocketTimeoutException.  In that exception handling block, you can do
whatever you want to handle that situation, e.g. increment an attempt
counter so that you try 5 times, or just bail out of the operation
entirely, or whatever.  You'll usually want a finally block to close open
streams, close the FTP connection, or whatever.

Hope this helps!  Let me know if you have any specific questions on this
operation.  I actually just worked this out a few days ago and am working
on a nice sandbox sample app that demonstrates the capability.  Drop me an
e-mail if you'd like to see that when it's done...

Nicolas Maujean said:
> Hi,
>    During the transfer a file, when I stop the ftp server, no errors
> occured, why ? what can I do so an error occur ?
> thank you for your answer,
> Nicolas Maujean
> --------------------------------------------------------------------- To
unsubscribe, e-mail: For
additional commands, e-mail:

Rick Herrick

I haven't got time for inner peace.

Get out of control, but appear under control. It's not bad to alarm other
people, though--it's good for them.--Hunter S. Thompson

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message