Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know the end of FTP Welcome message

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 ([email protected])
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
like image 509
André Snede Avatar asked Oct 26 '12 07:10

André Snede


People also ask

How do you terminate an FTP session?

Close an ftp connection to a remote system by using the bye command. ftp> bye 221-You have transferred 0 bytes in 0 files.

What is an FTP message?

Messages are information statements that are provided by the FTP client. Replies are the responses to commands that are returned from the FTP server to the client. Replies are described in FTPD reply codes in z/OS Communications Server: IP and SNA Codes. Messages are composed of a message ID followed by message text.

What is FTP in node JS?

node-ftp is an FTP client module for node. js that provides an asynchronous interface for communicating with an FTP server.


1 Answers

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:

Thus the format for multi-line replies is that the first line will begin with the exact required reply code, followed immediately by a Hyphen, "-" (also known as Minus), followed by text. The last line will begin with the same code, followed immediately by Space , optionally some text, and the Telnet end-of-line code.

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

It is possible for a reply to contain more than one line of text. In this case, each line starts with the reply code, and all lines but the last have a hyphen between the reply code and the reply text, to indicate that the reply continues. The last line has a space between the reply code and reply text, just like a single-line reply. This facility is often used to provide additional response information after a user logs in, via the 230 reply code.

like image 162
glglgl Avatar answered Oct 25 '22 00:10

glglgl