eg:
public String[] connect(String host, int port)
throws IllegalStateException, IOException,
FTPIllegalReplyException, FTPException {
synchronized (lock) {
// Is this client already connected to any host?
if (connected) {
throw new IllegalStateException("Client already connected to "
+ host + " on port " + port);
}
// Ok, it's connection time. Let's try!
Socket connection = null;
try {
// Open the connection.
connection = connector.connectForCommunicationChannel(host, port);
if (security == SECURITY_FTPS) {
connection = ssl(connection, host, port);
}
// Open the communication channel.
communication = new FTPCommunicationChannel(connection, pickCharset());
for (Iterator i = communicationListeners.iterator(); i.hasNext();) {
communication.addCommunicationListener((FTPCommunicationListener) i.next());
}
// Welcome message.
FTPReply wm = communication.readFTPReply();
// Does this reply mean "ok"?
if (!wm.isSuccessCode()) {
// Mmmmm... it seems no!
throw new FTPException(wm);
}
// Flag this object as connected to the remote host.
this.connected = true;
this.authenticated = false;
this.parser = null;
this.host = host;
this.port = port;
this.username = null;
this.password = null;
this.utf8Supported = false;
this.restSupported = false;
this.mlsdSupported = false;
this.modezSupported = false;
this.dataChannelEncrypted = false;
// Returns the welcome message.
return wm.getMessages();
} catch (IOException e) {
// D'oh!
throw e;
} finally {
// If connection has failed...
if (!connected) {
if (connection != null) {
// Close the connection, 'cause it should be open.
try {
connection.close();
} catch (Throwable t) {
;
}
}
}
}
}
}