javaftppassive-mode

Simple FTP client in Java can send the "list" command just once in passive mode


I still have some problems regarding the FTP session between my simple FTP client written in Java and a proFTP server.

I have connected to the server using passive mode. When I send the list command I get a response in ASCII of which files that are on the server. If I send the list command again, I don´t get any response. I cannot even get a response from other FTP commands that I send. It seems like the session "hangs".

Why does my simple java application stop getting replies when sending the list command a second time?


Solution

  • In FTP, both active and passive modes supply a separate channel for transferring data. Whenever you want to send a command in passive mode that involves the sending of data (e.g. list) you must re-send the PASV to tell the server that you're about to perform an operation that involves the data connection. The server can then open up a new socket or continue to use the same one.

    Here's an example session:

    230 Login successful.
    PASV
    227 Entered Passive Mode (123,245,209,137,199,61)
    LIST
    150 Here comes the directory listing.
    226 Directory send OK.
    LIST
    425 Use PORT or PASV first.
    PASV
    227 Entered Passive Mode (123,245,209,137,202,198)
    LIST
    150 Here comes the directory listing.
    226 Directory send OK.
    

    Notice that my second attempt to use LIST failed because I hadn't opened a data channel. Once I sent another PASV, I was able to use LIST again.