我有以下代码,应该在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\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");
至
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