尝试登录时FTP Server奇怪的响应

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了尝试登录时FTP Server奇怪的响应相关的知识,希望对你有一定的参考价值。

我有以下代码,应该在ftp服务器上登录,函数send_get()只是将消息从msg发送到ftp服务器并获取响应。 print_resp()打印来自服务器的响应。当我执行此代码时,我得到:

451 The parameter is incorrect. 

451 The parameter is incorrect. 

500 '

 ': command not understood.

451 The parameter is incorrect. 

500 '

 ': command not understood.

我使用test.rebex.net(195.144.107.198)服务器,它似乎在broswers和其他在线ftp测试工作正常。

bool ftp_mirror::login(const char* user, const char* pass){
    strcpy(msg, "CLNT ftp-mirror
");
    if(!send_get())
        return false;
    print_resp();

    strcpy(msg, "USER ");
    strcat(msg, user);
    strcat(msg, "
");

    if(!send_get())
        return false;
    print_resp();

    strcpy(msg, "PASS ");
    strcat(msg, pass);
    strcat(msg, "
");

    if(!send_get())
        return false;
    print_resp();
    return true;
}

这些是我在我的代码中使用的一些其他功能

bool ftp_mirror::send_msg(){
    if(0 > send(msg_sock, msg, strlen(msg), 0)){
        perror("send_msg")
        return false;
    }
    return true;
}

bool ftp_mirror::get_resp(){
    memset(resp, 0, RESP_LENGTH);
    if(0 > recv(msg_sock, resp, RESP_LENGTH, 0)){
        perror("get_resp")
        return false;
    }
    return true;
}

bool ftp_mirror::send_get(){
    if(!send_msg())
        return false;
    if(!get_resp())
        return false;
    return true;
}
答案

FTP协议使用CRLF作为其换行序列。所以改变

strcat(msg, "
");

strcat(msg, "
");

我还建议使用sprintf()而不是strcpy()strcat()的序列,例如

sprintf(msg, "USER %s
", user);

或许你应该避免重新发明轮子,并使用像libcurl这样的现有库。如果你需要更轻的东西,请参阅Good simple C/C++ FTP and SFTP client library recommendation for embedded Linux

以上是关于尝试登录时FTP Server奇怪的响应的主要内容,如果未能解决你的问题,请参考以下文章

关于片段生命周期

登录失败:未知用户名或密码错误。使用 C# 的 FTP

登录 - 填写表格并提交

未知的 Bittorrent 消息

SQL Server 登录前握手

从 JSON 获取的 NSData 的奇怪响应