[FTPClient] : No errors occured when I stop the ftp server during a transfer

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

[FTPClient] : No errors occured when I stop the ftp server during a transfer

Nicolas Maujean
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: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [FTPClient] : No errors occured when I stop the ftp server during a transfer

Rick Herrick
Nicolas,

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
this:

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

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(),
and
setSoTimeout().

// Set the ftp timeout values on the client
client.setDataTimeout(ftpTimeout);
client.setDefaultTimeout(ftpTimeout);
client.setSoTimeout(ftpTimeout);

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: [hidden email] For
additional commands, e-mail: [hidden email]
>
>


--
Rick Herrick
[hidden email]

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: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [FTPClient] : No errors occured when I stop the ftp server during a transfer

Rick Herrick
Daniel F. Savarese said:

> A few clarfications.
>
> setSoTimeout should be called only after a connection has been established
> and affects only the control connection.  It affects the actively
> connected
> socket.  If you call it before connecting, you'll get a
> NullPointerException.
> setDefaultTimeout should be called before establishing a connection.  The
> actual timeout specified is then automatically applied to the control
> connection after it is established.  It basically saves you the trouble of
> calling setSoTimeout after every connection establishment.  setDataTimeout
> should be called before a data connection is established
> (e.g., a file transfer) because it doesn't affect an active data
> connection.
> It does the same thing as setDefaultTimeout, except for data connections
> (i.e., the timeout for the data connection is automatically set
> immediately
> after the data connection is established).

Ah, cool, thanks for that information.  I tried to find that while working
on my remote asset transfer app, but couldn't find it documented anywhere.
 That would be huge to have in the Javadocs at the very least.

Luckily I didn't manage to shoot myself in the foot :^)

--
Rick Herrick
[hidden email]

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: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

[HttpClient] : How I send a compress stream using HttpClient

Nicolas Maujean
Hi,

  How I send a compress stream using HttpClient ?

thank you for your answer,

Nicolas Maujean



---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]