C++ 尝试登录时FTP服务器出现异常响应
我有下面的代码,应该将我记录在ftp服务器上,函数send_get()只需将消息从msg发送到ftp服务器并获取响应。print_resp()打印来自服务器的响应。当我执行此代码时,我得到:C++ 尝试登录时FTP服务器出现异常响应,c++,linux,ftp,C++,Linux,Ftp,我有下面的代码,应该将我记录在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 ' ': comma
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\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协议使用
CRLF
作为其换行序列。所以改变
strcat(msg, "\n");
到
我还建议使用sprintf()
sprintf(msg, "USER %s\r\n", user);
或者,您应该避免重新发明轮子,使用像libcurl
这样的现有库。如果你需要更轻的东西,请看我想是时候给你买了。例如,打印发送和接收的确切原始数据(或在调试程序中单步执行代码时查看)。顺便说一下,上面的代码看起来像一些C++,而不是C代码。FTP是一个复杂的协议,所以你为什么不使用一些现有的库呢?我做了你建议的更改,现在服务器处理第一个命令并请求USER和PASS,但之后它再次中断,我想使用一个库,但这是一个学校项目,我必须从Scratch开始,我想你登录后发送的命令还有一些其他问题。
sprintf(msg, "USER %s\r\n", user);