gpt4 book ai didi

c++ - 尝试登录时 FTP 服务器出现奇怪的响应

转载 作者:太空宇宙 更新时间:2023-11-04 05:16:49 25 4
gpt4 key购买 nike

我有以下代码,应该将我登录到 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) 服务器,它似乎在浏览器和其他在线 ftp 测试器上运行良好。

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

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

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

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

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 协议(protocol)使用 CRLF 作为其换行符序列。所以改变一下

strcat(msg, "\n");

strcat(msg, "\r\n");

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

sprintf(msg, "USER %s\r\n", user);

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

关于c++ - 尝试登录时 FTP 服务器出现奇怪的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48020733/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com