通过套接字传输文件时FTP服务器挂起



我在java中制作一个简单的FTP服务器。当我在本地测试时(在我自己的机器上运行服务器和客户端),一切都工作了。但是,当我在两台不同的远程机器上运行服务器和客户机时,客户机在收到来自服务器的"150 File status okay"消息后不久就挂起了。我不明白为什么它在一个地方可以正常工作,但在另一个地方不行。以下是相关代码:

服务器(发送文件):

FileInputStream input = null;
                            try {
                                input = new FileInputStream(filePath);
                            } catch (FileNotFoundException e) {
                                out.writeBytes("550 File not found or access denied.rn");
                            }
                            out.writeBytes("150 File status okay.rn");
                            // TCP CONNECT
                            DataOutputStream outToClient_d = null;
                            Socket clientSocket1 = null;
                            try {
                                ipAddress = ipAddress.substring(0,
                                        ipAddress.length() - 1);
                                clientSocket1 = new Socket(ipAddress,
                                        portNumber);
                                outToClient_d = new DataOutputStream(
                                        clientSocket1.getOutputStream());
                            }
                            catch (UnknownHostException e) {
                                out.writeBytes("425 Can not open data connection.rn");
                            }
                            byte[] buf = new byte[2048];
                            int len;
                            while ((len = input.read(buf)) > 0) {
                                outToClient_d.write(buf, 0, len);
                            }
                            input.close();
                            out.writeBytes("250 Requested file action completed.rn");
                            clientSocket1.close();
                            outToClient_d.close();

客户端(将文件保存到/retr_files):

InputStream inFromServer_d = null;
    if (welcomeSocket != null) {
        if (!welcomeSocket.isClosed()) {
            welcomeSocket.close();
        }
    }
    try {
        welcomeSocket = new ServerSocket(port);
        System.out.print("PORT " + myIP + "," + num1 + "," + num2 + "rn");
        out.writeBytes("PORT " + myIP + "," + num1 + "," + num2 + "rn");
        System.out.print(parseReply(getResponse()));
        System.out.print("RETR " + pathname + "rn");
        out.writeBytes("RETR " + pathname + "rn");
        String reply = parseReply(getResponse());
        if (reply.charAt(10) == '1') {
            System.out.print(reply);
            System.out.print(parseReply(getResponse()));
            try {
                clientSocket_d = welcomeSocket.accept();
            } catch (IOException e) {
                System.out
                        .print("GET failed, FTP-data port not allocated.rn");
                System.exit(-1);
            }
            inFromServer_d = clientSocket_d.getInputStream();
            // READ
            InputStream input = inFromServer_d;
            OutputStream output = new FileOutputStream("retr_files/file"
                    + retrCnt);
            byte[] buf = new byte[2048];
            int len;
            while ((len = input.read(buf)) > 0) {
                output.write(buf, 0, len);
            }
            input.close();
            output.close();
            clientSocket_d.close();
        } else {
            System.out.print(reply);
        }
    } catch (IOException e) {
        System.out.print("GET failed, FTP-data port not allocated.rn");
        System.exit(-1);
    }

任何帮助都是感激的!

我猜在客户端和服务器之间有防火墙阻止了从服务器到客户端的反向连接。这个问题就是为什么现在人们通常使用"被动"传输而不是"主动"传输的原因。

相关内容

  • 没有找到相关文章

最新更新