Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 通过套接字传输文件时FTP服务器挂起_Java_Sockets_Ftp_Stream - Fatal编程技术网

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

Java 通过套接字传输文件时FTP服务器挂起,java,sockets,ftp,stream,Java,Sockets,Ftp,Stream,我正在用java制作一个简单的FTP服务器。当我在本地测试它(在我自己的机器上运行服务器和客户机)时,一切都正常。然而,当我在两台不同的远程机器上运行服务器和客户机时,客户机在从服务器收到“150文件状态正常”消息后不久就会挂起。我不明白为什么它在一个地方工作得很好,而在另一个地方却不行。以下是相关代码: 服务器(发送文件): 客户端(将文件保存到/retr\u文件中): 感谢您的帮助 我猜客户端和服务器之间有防火墙,阻止了从服务器到客户端的反向连接。这个问题就是人们现在通常使用“被动”传输而不

我正在用java制作一个简单的FTP服务器。当我在本地测试它(在我自己的机器上运行服务器和客户机)时,一切都正常。然而,当我在两台不同的远程机器上运行服务器和客户机时,客户机在从服务器收到“150文件状态正常”消息后不久就会挂起。我不明白为什么它在一个地方工作得很好,而在另一个地方却不行。以下是相关代码:

服务器(发送文件):

客户端(将文件保存到/retr\u文件中):


感谢您的帮助

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

了解您的
输出对象是什么会有帮助。包括它的声明和任务。对不起!它与另一个将FTP命令/响应写入服务器/客户端的套接字相关联。如何确定客户端的
myIp
?挂起时代码的堆栈跟踪是什么?所以这很奇怪。。。我测试这个的文件是一个.gif文件。对于一个.jpg来说,它工作得很好。有没有关于.gif的东西不允许它们以字节流的形式传输?我不这么认为。这两台远程机器都在我学校的网络上(是的,这是一个硬件作业…=/)@user1718698-为什么这就不成问题了?默认情况下,windows通常运行防火墙,您也可以在linux上设置防火墙。
FileInputStream input = null;
                            try {
                                input = new FileInputStream(filePath);
                            } catch (FileNotFoundException e) {
                                out.writeBytes("550 File not found or access denied.\r\n");
                            }
                            out.writeBytes("150 File status okay.\r\n");

                            // 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.\r\n");
                            }

                            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.\r\n");
                            clientSocket1.close();
                            outToClient_d.close();
InputStream inFromServer_d = null;

    if (welcomeSocket != null) {
        if (!welcomeSocket.isClosed()) {
            welcomeSocket.close();
        }
    }

    try {
        welcomeSocket = new ServerSocket(port);
        System.out.print("PORT " + myIP + "," + num1 + "," + num2 + "\r\n");
        out.writeBytes("PORT " + myIP + "," + num1 + "," + num2 + "\r\n");
        System.out.print(parseReply(getResponse()));
        System.out.print("RETR " + pathname + "\r\n");
        out.writeBytes("RETR " + pathname + "\r\n");
        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.\r\n");
                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.\r\n");
        System.exit(-1);
    }