Implementing own ftp-server: response on LIST command

2.2k Views Asked by At

I'm trying to implement my own ftp-server without any libraries on Java. For testing my server I use FTP-client FileZilla. The problem is that I can't send list of files in the current directory through data connection (or in other words I can't implement FTP command LIST). I've tried both active and passive modes.

Here is the output of FileZilla:

Command:    PASV
Response:   227 Entering Passive Mode (192,168,0,87,125,63)
Command:    LIST
Response:   150 Opening BINARY mode data connection.
// Here I'm trying to send list of files through data connection
Response:   226 Transfer complete.
Error:  Connection timed out
Error:  Failed to retrieve directory listing

Here is my implementation of LIST and PASV commands:

public class FTPCommandSocket implements Runnable {

    Socket command_socket;
    Socket data_socket = null;
    FTPDataSocket ftpds = null;
    ServerSocket ss = null;
    // ...
    @Override
    public void run() {
        try {
            // Send response to the client.
            sendResponse("220 WELCOME\n");

            while(from_client_command.read(input_b) != -1)
            {
                String input = new String(input_b);
                String cmd = parseInput(input);
                switch(cmd) {
                    // ... (other commands)
                    case Command.PASV:
                        sendResponse("227 Entering Passive Mode (192,168,0,87,125,63)\n");
                        ss = new ServerSocket(125 * 256 + 63);
                        data_socket = ss.accept();
                        ftpds = new FTPDataSocket(data_socket);
                    break;
                    case Command.LIST:
                        sendResponse("150 Opening BINARY mode data connection.\n");
                        ftpds.sendFilesList(cur_path);
                        Thread.sleep(500);
                        sendResponse("226 Transfer complete.\n");
                    break;
                    //... (other commands)
                    default:
                        sendResponse("502 Command is not implemented.\n");
                }
            }
        } catch (...) {}
    }

    private void sendResponse(String string) throws IOException {
        to_client_command.write(string.getBytes());
        to_client_command.flush();
    }
}

This is data connection class:

public class FTPDataSocket {
    Socket data_socket;
    OutputStream to_client_data;
    InputStream from_client_data;
    private final byte[] input_b = new byte[15000];

    public FTPDataSocket(Socket s) throws IOException {
        this.data_socket = s;
        from_client_data = data_socket.getInputStream();
        to_client_data = data_socket.getOutputStream();
    }

    public void sendFilesList(String path) throws IOException {
        // Debug version: there are no real files yet.
        sendResponse("-rwxr-xr-x 1 100 100 14757 a.out\r\n");
    }

    private void sendResponse(String string) throws IOException {
        to_client_data.write(string.getBytes());
        to_client_data.flush();
        System.out.print(string);
    }
}

Do you have any ideas, what I'm doing wrong?

Thank you in advance!

1

There are 1 best solutions below

1
On BEST ANSWER

The problem was that socket was not closed after sending data.