首页 > 解决方案 > 为什么我的 Poco FTPClientSession 无法打开连接或登录服务器?

问题描述

我正在尝试构建一个原始的 FTP 客户端。但首先我需要连接到服务器并使用 FTP 登录。当使用 Poco::Net::FTPClientSession::open() 和它需要的详细信息时,它不断告诉我我必须

"specify a password" or "Cannot set file type" both with error code 331.

我尝试在 FTPClient 构造函数中首先使用 Poco::Net::StreamSocket.connect() 并且返回的错误是;

terminate called after throwing an instance of 'Poco::Net::NetException'

与 FTP 服务器建立简单连接并登录的步骤是什么?我不太明白我做错了什么,我似乎也找不到一个简单的 FTP Poco lib 示例。

另一个问题是我似乎无法获得有关 Poco::Net::NetException 原因可能是什么的描述。

所以这里是来源。

来源:ftp_client.h

#include <iostream>
#include <fstream>
#include "Poco/Net/Net.h"
#include "Poco/Net/StreamSocket.h"
#include "Poco/Net/FTPClientSession.h"
#include "Poco/Net/SocketAddress.h"
#include "Poco/Net/NetException.h"

namespace FTP{

    struct Details{
        std::string username;
        std::string password;
        std::string connection_url;
        std::string connection_url_port;
        uint32_t port;
        
        Details(std::string username_, std::string password_, std::string connection_url_, uint32_t port_ = 21){
            username = username_;
            password = password_;
            connection_url = connection_url_;
            connection_url_port = connection_url_;
            connection_url_port.append(":");
            connection_url_port.append(std::to_string(port_));
            port = port_;
        }
    };

    class FTPClient{
        private:
        Details details;
        Poco::Net::SocketAddress socket_address;
        Poco::Net::StreamSocket socket;
        Poco::Net::FTPClientSession FTPClientHandle;

        public:
        FTPClient(Details details_);
        std::string login();`enter code here`
    };
}

来源:ftp_client.cpp

#include "ftp_client.h"
namespace FTP{

    FTPClient::FTPClient(Details details_)
    : details(details_),
    socket_address(details.connection_url_port),
    socket(socket_address),
    FTPClientHandle(socket,false)
    {
        int x = 0;
        //std::cout << "Address: " << socket_address.addr() << std::endl;
        //socket.connect(socket_address);
        try{
            FTPClientHandle.open(details.connection_url.c_str(),details.port,details.username , details.password);
        } catch(Poco::Net::FTPException e){
            std::cout << "Error on Login: " << e.message() << e.code() << std::endl;
        }
        //login();
    }


    std::string FTPClient::login() {
        try {
                std::cout << "Trying :" << details.username << " : " << details.password << std::endl;
                FTPClientHandle.login(details.username, details.password);
            } catch (Poco::Net::FTPException r){
                std::cout << ("Login failed : " + r.message() + " Code: " + std::to_string(r.code())) << std::endl;
            }
        return "Login failed";    
    }

在 main() 中,我初始化 FTP::FTPClient 并将详细信息传递给它。当我使用 FileZilla 登录 FTP 服务器时,这些详细信息没有问题。我也尝试过使用“ftp://qqq.www.eee.rrr”,因为看到 curl 有时使用这种表示法。因为我提供了端口 21,所以我删除了它。

主文件

int main(int argc, char* argv[])
{
    FTP::Details details("xxxxxx","eeeeee","qqq.www.eee.rrr");
    FTP::FTPClient client(details);

}

标签: c++socketsftppocopoco-libraries

解决方案


推荐阅读