os.write("LIST\r\n".getBytes());
// readInputStream . . .
Didn't quite work. Kept getting connection error. I expected the file list would be returned in the input stream like the previous commands I tried. Maybe it had something to do with the active and passive modes in FTP. Googling "FTP passive mode" got me a very good link, Active FTP vs. Passive FTP, a Definitive Explanation.
It became clear after reading that there are 2 ports involved, command and data. The previous commands I tried happen not to have data. Now I understand what RFC 959 means when it says to "listen" on a port. You need to a ServerSocket in order to "listen". Of course, "listening" must be done on a separate thread.
Out of curiosity I also tried the old faithful ftp command line. Passing -d as an argument showed me the raw commands in the background. I quickly discovered that the "ls" client command actually issues PORT before LIST.
final int port = 12345; // just a random port number
Thread t = new Thread(new Runnable(
public void run() {
ServerSocket datalistener = new ServerSocket(port);
Socket data = datalistener.accept();
// readInputStream . . .
data.close();
datalistener.close();
}
)};
t.start();
String ftpclientIP = ftpclient.getLocalAddress()
.getHostAddress().replace('.', ',');
String portargs = ftpclientIP + (port/256) + (port%256);
os.write(("PORT " + portargs + "\r\n").getBytes());
os.write("LIST\r\n".getBytes());