My problem is that I’m creating a FTP client, and so far its working flawlessly besides one minor detail, that keeps bugging me.
I need to know how many lines the FTP welcome message spans over… And this cannot be acceptable!
private Socket connection;
private PrintWriter outStream;
private Scanner inStream;
public void InitiateConnection() throws IOException
{
log.Info(this, "Initiating connection to host: " + host + ":" + port);
connection = new Socket(host, port);
log.Info(this, "Connection initiated.");
outStream = new PrintWriter(connection.getOutputStream(), true);
inStream = new Scanner(connection.getInputStream());
Listen();
Listen();
Listen();
}
public String Listen() throws IOException
{
if(connection == null)
throw new IOException("Connection not initiated yet");
String response = inStream.nextLine();
log.Info(this, "Response: " + response);
return response;
}
This is the simple setup, I have left out all other code, as it doesn’t have anything to do with my problem.
I have tried multiple things to try to achieve this.
Failed Solution 1:
String response = "";
while(response != null)
Listen();
Failed Solution 2:
while(connection.getInputStream().available > 0)
Listen();
And countless others… But either it doesn’t work, or the methods block and wait for new input. I have even tried with a timeout, but that doesn’t work flawlessly either, its not a proper solution to this problem…
I need to be able to get the entire welcome message from the FTP server, without knowing the amount of lines…
So I can both get this:
Response: 220-FileZilla Server version 0.9.39 beta
Response: 220-written by Tim Kosse (Tim.Kosse@gmx.de)
Response: 220 Please visit http://sourceforge.net/projects/filezilla/
And this:
Response: 220-FileZilla Server version 0.9.40 beta
Response: 220 Welcome to Andrés FTP Server
If you have a close look at the messages, you see that all but the last lines have a
-behind the status code. The last line has a, however, indicating, well, the last line.You can read that in RFC 959, section 4.2:
There is nothing said about the 2nd to second-last line, but it is logical that they have the same format as the 1st one.
Update: The FTP protocol seems to be badly documented, but I found another reference stating the same as me above:
The TCP/IP Guide mentions that