| Constructor: |
public FTPControlSocket(String remoteHost) throws IOException, FTPException {
this(remoteHost, CONTROL_PORT);
}
Constructor. Performs TCP connection and
sets up reader/writer Parameters:
remoteHost - Remote hostname
|
public FTPControlSocket(InetAddress remoteAddr) throws IOException, FTPException {
this(remoteAddr, CONTROL_PORT);
}
Constructor. Performs TCP connection and
sets up reader/writer Parameters:
remoteAddr - Remote inet address
|
public FTPControlSocket(String remoteHost,
int controlPort) throws IOException, FTPException {
controlSock = new Socket(remoteHost, controlPort);
initStreams();
validateConnection();
}
Constructor. Performs TCP connection and
sets up reader/writer. Allows different control
port to be used Parameters:
remoteHost - Remote hostname
controlPort - port for control stream
|
public FTPControlSocket(InetAddress remoteAddr,
int controlPort) throws IOException, FTPException {
controlSock = new Socket(remoteAddr, controlPort);
initStreams();
validateConnection();
}
Constructor. Performs TCP connection and
sets up reader/writer. Allows different control
port to be used Parameters:
remoteAddr - Remote inet address
controlPort - port for control stream
|
| Method from com.enterprisedt.net.ftp.FTPControlSocket Detail: |
FTPDataSocket createDataSocket(FTPConnectMode connectMode) throws IOException, FTPException {
if (connectMode == FTPConnectMode.ACTIVE) {
return new FTPDataSocket(createDataSocketActive());
}
else { // PASV
return new FTPDataSocket(createDataSocketPASV());
}
}
Request a data socket be created on the
server, connect to it and return our
connected socket. |
ServerSocket createDataSocketActive() throws IOException, FTPException {
// use any available port
ServerSocket socket = new ServerSocket(0);
// get the local address to which the control socket is bound.
InetAddress localhost = controlSock.getLocalAddress();
// send the PORT command to the server
setDataPort(localhost, (short)socket.getLocalPort());
return socket;
}
Request a data socket be created on the Client
client on any free port, do not connect it to yet. |
Socket createDataSocketPASV() throws IOException, FTPException {
// PASSIVE command - tells the server to listen for
// a connection attempt rather than initiating it
String reply = sendCommand("PASV");
validateReply(reply, "227");
// The reply to PASV is in the form:
// 227 Entering Passive Mode (h1,h2,h3,h4,p1,p2).
// where h1..h4 are the IP address to connect and
// p1,p2 the port number
// Example:
// 227 Entering Passive Mode (128,3,122,1,15,87).
// extract the IP data string from between the brackets
int bracket1 = reply.indexOf('(");
int bracket2 = reply.indexOf(')");
String ipData = reply.substring(bracket1+1,bracket2);
int parts[] = new int[6];
int len = ipData.length();
int partCount = 0;
StringBuffer buf = new StringBuffer();
// loop thru and examine each char
for (int i = 0; i < len && partCount < = 6; i++) {
char ch = ipData.charAt(i);
if (Character.isDigit(ch))
buf.append(ch);
else if (ch != ',") {
throw new FTPException("Malformed PASV reply: " + reply);
}
// get the part
if (ch == '," || i+1 == len) { // at end or at separator
try {
parts[partCount++] = Integer.parseInt(buf.toString());
buf.setLength(0);
}
catch (NumberFormatException ex) {
throw new FTPException("Malformed PASV reply: " + reply);
}
}
}
// assemble the IP address
// we try connecting, so we don't bother checking digits etc
String ipAddress = parts[0] + "."+ parts[1]+ "." +
parts[2] + "." + parts[3];
// assemble the port number
int port = (parts[4] < < 8) + parts[5];
// create the socket
return new Socket(ipAddress, port);
}
Request a data socket be created on the
server, connect to it and return our
connected socket. |
void debugResponses(boolean on) {
debugResponses = on;
}
Switch debug of responses on or off |
String getRemoteHostName() {
InetAddress addr = controlSock.getInetAddress();
return addr.getHostName();
}
Get the name of the remote host |
public void logout() throws IOException {
writer.close();
reader.close();
controlSock.close();
}
Quit this FTP session and clean up. |
String readReply() throws IOException {
StringBuffer reply = new StringBuffer(reader.readLine());
if (debugResponses)
System.out.println(reply.toString());
String replyCode = reply.toString().substring(0, 3);
// check for multiline response and build up
// the reply
if (reply.charAt(3) == '-") {
boolean complete = false;
while (!complete) {
String line = reader.readLine();
if (debugResponses)
System.out.println(line);
if (line.length() > 3 &&
line.substring(0, 3).equals(replyCode) &&
line.charAt(3) == ' ") {
reply.append(line.substring(3));
complete = true;
}
else { // not the last line
reply.append(" ");
reply.append(line);
}
} // end while
} // end if
return reply.toString();
}
Read the FTP server's reply to a previously
issued command. RFC 959 states that a reply
consists of the 3 digit code followed by text.
The 3 digit code is followed by a hyphen if it
is a muliline response, and the last line starts
with the same 3 digit code. |
String sendCommand(String command) throws IOException {
if (debugResponses)
System.out.println("--- > " + command);
// send it
writer.write(command + EOL);
writer.flush();
// and read the result
return readReply();
}
Send a command to the FTP server and
return the server's reply |
void setTimeout(int millis) throws IOException {
if (controlSock == null)
throw new IllegalStateException(
"Failed to set timeout - no control socket");
controlSock.setSoTimeout(millis);
}
Set the TCP timeout on the underlying control socket.
If a timeout is set, then any operation which
takes longer than the timeout value will be
killed with a java.io.InterruptedException. |
protected byte[] toByteArray(short value) {
byte[] bytes = new byte[2];
bytes[0] = (byte) (value > > 8); // bits 1- 8
bytes[1] = (byte) (value & 0x00FF); // bits 9-16
return bytes;
}
Convert a short into a byte array |
void validateReply(String reply,
String expectedReplyCode) throws IOException, FTPException {
// all reply codes are 3 chars long
String replyCode = reply.substring(0, 3);
// if unexpected reply, throw an exception
if (!replyCode.equals(expectedReplyCode)) {
throw new FTPException(reply.substring(4), replyCode);
}
}
Validate the response the host has supplied against the
expected reply. If we get an unexpected reply we throw an
exception, setting the message to that returned by the
FTP server |
void validateReply(String reply,
String[] expectedReplyCodes) throws IOException, FTPException {
// all reply codes are 3 chars long
String replyCode = reply.substring(0, 3);
for (int i = 0; i < expectedReplyCodes.length; i++)
if (replyCode.equals(expectedReplyCodes[i]))
return;
// got this far, not recognised
throw new FTPException(reply.substring(4), replyCode);
}
Validate the response the host has supplied against the
expected reply. If we get an unexpected reply we throw an
exception, setting the message to that returned by the
FTP server |