首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >实现自己的ftp-server:对LIST命令的响应

实现自己的ftp-server:对LIST命令的响应
EN

Stack Overflow用户
提问于 2014-11-17 19:21:32
回答 1查看 2.1K关注 0票数 0

我试图在Java上实现自己的ftp服务器,而不需要任何库。为了测试我的服务器,我使用FTP-客户端FileZilla。问题是我不能通过数据连接发送当前目录中的文件列表(换句话说,我不能实现FTP命令列表)。我试过主动模式和被动模式。

下面是FileZilla的输出:

代码语言:javascript
复制
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

下面是LIST和PASV命令的实现:

代码语言:javascript
复制
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();
    }
}

这是数据连接类:

代码语言:javascript
复制
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);
    }
}

你有什么想法吗,我做错什么了?

提前谢谢你!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-11-24 13:03:15

问题是在发送数据后没有关闭套接字。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26980284

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档