我为mbed项目编写FTP脚本。我使用B-L475E-IOT01A开发板,并尝试将文件发送到FTP服务器。因此,我无法使用此库与服务器建立连接https://os.mbed.com/users/dkato/code/ftp-client/#e069c405c934.不幸的是,客户的工作没有达到应有的效果。我试着建立这样的连接:
bool FTPClient::open(const char* ip_addr, int port, const char* user, const char* pass)
{
SocketAddress ftpAddress(ip_addr, port);
//Connect to SocketAddress while using FTP Clients Network interface
FTPClientControlSock.open(p_network);
if (FTPClientControlSock.connect(ftpAddress) < 0) {
printf("ERROR: %s(%d)rn", __FILE__, __LINE__);
return false;
}
//recieve ftp server message
if (FTPClientControlSock.recv(p_ftp_buf, FTP_BUF_SIZE) <= 0) {
printf("ERROR: %s(%d)rn", __FILE__, __LINE__);
return false;
}
//prove ftp server message equals ftp server information messages (starting with not logged in code 220)
if (strncmp(p_ftp_buf, "220", 3) != 0) {
printf("ERROR: %s(%d)rn", __FILE__, __LINE__);
return false;
}
//store user info in ftp communication and send it
sprintf(p_ftp_buf, "USER %srn", user);
printf("%s", p_ftp_buf);
FTPClientControlSock.send(p_ftp_buf, strlen(p_ftp_buf));
//recieve ftp server info and print it
if (FTPClientControlSock.recv(p_ftp_buf, FTP_BUF_SIZE) <= 0) {
printf("ERROR: %s(%d)rn", __FILE__, __LINE__);
return false;
}
printf("%s", p_ftp_buf);
//prove ftp server message equals ftp server information messages (begin with code 331)
if (strncmp(p_ftp_buf, "331", 3) != 0) {
printf("ERROR: %s(%d)rn", __FILE__, __LINE__);
return false;
}
//store password in string and send it to server
sprintf(p_ftp_buf, "PASS %srn", pass);
printf("%s", p_ftp_buf);
FTPClientControlSock.send(p_ftp_buf, strlen(p_ftp_buf));
//recieve ftp server info and print it
if (FTPClientControlSock.recv(p_ftp_buf, FTP_BUF_SIZE) <= 0) {
printf("ERROR: %s(%d)rn", __FILE__, __LINE__);
return false;
}
//check login was successful
if (strncmp(p_ftp_buf, "230", 3) != 0) {
printf("ERROR: %s(%d)rn", __FILE__, __LINE__);
return false;
}
printf("%s", p_ftp_buf);
return true;
}
在这之后的终端上,我在控制台中得到了这样的输出:
220---------- Welcome to Pure-FTPd [privsep] [TLS] ----------
220-You are user number 4 of 500 allowed.
220-Local time is now 02:42. Server port: 21.
220-This is a private system - No anonymous login
220 You will be disconnected after 15 minutes of inactivity.
USER user
PASS pass
331 User user OK. Password required
is now 02:42. Server port: 21.
220-This is a private system - No anonymous login
220 You will be disconnected after 15 minutes of inactivity.
ERROR: ./ftp-client/FTPClient.cpp(96) //this is the line where the code 230 gets checked
我真的不知道我的错误在哪里。我希望能成功登录并与ftp服务器进行清晰的通信。你能帮我吗?
Julien,来自服务器的所有响应都遵循一个模式。。您需要等待来自服务器的CCD_ 1序列。您需要过滤TELNET协议转义序列(以字节0xff或0xfe开头,我记不清了(,代码必须始终位于一行的开头。此外,带有-
而不是空格的数字表示消息较大,您应该期待另一行(每条消息的最后一行有一个数字代码,后面跟着一个空格(
这对于保持自己与服务器同步至关重要。。。。或者你会开始收到不同于你想象的命令的响应。
从输出中不清楚您试图做什么,因为您显示的唯一内容是服务器的登录部分。
您的命令行也有(这是协议强制要求的(以rn
(按顺序(结束,否则您无法执行其他操作,或者您可以访问不理解您的服务器。
有关ftp协议如何工作的详细信息,请查看RFC-959-文件传输协议。传输通常在一个新的、不同的TCP连接中处理,因此您通常必须管理控制连接以及与之并行运行的一系列数据传输
好的,我终于建立了连接。同步服务器和客户端是正确的。此外,我需要不时地释放缓冲区,现在输出不那么令人困惑了。这是我的代码:
bool FTPClient::open(const char *ip_addr, int port, const char *user,
const char *pass) {
// Convert into SocketAddress
SocketAddress ftpAddress(ip_addr, port);
// Close FTP connection if open
if (_ctr_open) {
FTPClientControlSock.close();
}
// Connect to SocketAddress while using FTP Clients Network interface
FTPClientControlSock.open(p_network);
if (FTPClientControlSock.connect(ftpAddress) < 0) {
printf("ERROR: %s(%d)rn", __FILE__, __LINE__);
return false;
}
// set connection to true
_ctr_open = true;
// recieve ftp server messages and print if correct
if (FTPClientControlSock.recv(p_ftp_buf, FTP_BUF_SIZE) <= 0) {
printf("ERROR: %s(%d)rn", __FILE__, __LINE__);
return false;
}
if (strncmp(p_ftp_buf, "220", 3) != 0) {
printf("ERROR: %s(%d)rn", __FILE__, __LINE__);
return false;
} else {
printf("%s", p_ftp_buf);
_login = false;
}
wait_us(2000000);
memset(p_ftp_buf, 0, strlen(p_ftp_buf));
if (FTPClientControlSock.recv(p_ftp_buf, FTP_BUF_SIZE) <= 0) {
printf("ERROR: %s(%d)rn", __FILE__, __LINE__);
return false;
}
if (strncmp(p_ftp_buf, "220", 3) != 0) {
printf("ERROR: %s(%d)rn", __FILE__, __LINE__);
return false;
} else {
printf("%s", p_ftp_buf);
_login = false;
}
wait_us(2000000);
memset(p_ftp_buf, 0, strlen(p_ftp_buf));
// store user info in ftp communication print and send it
sprintf(p_ftp_buf, "USER %srn", user);
FTPClientControlSock.send(p_ftp_buf, strlen(p_ftp_buf));
// recieve ftp server message and print if correct
if (FTPClientControlSock.recv(p_ftp_buf, FTP_BUF_SIZE) <= 0) {
printf("ERROR: %s(%d)rn", __FILE__, __LINE__);
return false;
}
if (strncmp(p_ftp_buf, "331", 3) != 0) {
printf("ERROR: %s(%d)rn", __FILE__, __LINE__);
return false;
} else {
printf("%s", p_ftp_buf);
_login = false;
}
wait_us(2000000);
memset(p_ftp_buf, 0, strlen(p_ftp_buf));
// store password in string and send it to server
sprintf(p_ftp_buf, "PASS %srn", pass);
FTPClientControlSock.send(p_ftp_buf, strlen(p_ftp_buf));
wait_us(2000000);
// recieve ftp server info and print it
if (FTPClientControlSock.recv(p_ftp_buf, FTP_BUF_SIZE) <= 0) {
printf("LINE: %drn", __LINE__);
printf("%s", p_ftp_buf);
printf("ERROR: %s(%d)rn", __FILE__, __LINE__);
return false;
} else {
wait_us(3000000);
printf("%s", p_ftp_buf);
}
if (FTPClientControlSock.recv(p_ftp_buf, FTP_BUF_SIZE) <= 0) {
printf("LINE: %drn", __LINE__);
printf("%s", p_ftp_buf);
printf("ERROR: %s(%d)rn", __FILE__, __LINE__);
return false;
} else {
wait_us(3000000);
printf("%s", p_ftp_buf);
}
// check login was successful
if (strncmp(p_ftp_buf, "230", 3) != 0) {
printf("ERROR: %s(%d)rn", __FILE__, __LINE__);
return false;
}
memset(p_ftp_buf, 0, strlen(p_ftp_buf));
_login = true;
return true;
}
我得到输出:
220---------- Welcome to Pure-FTPd [privsep] [TLS] ----------
220-You are user number 4 of 500 allowed.
220-Local time is now 03:25. Server port: 21.
220-This is a private system - No anonymous login
220 You will be disconnected after 15 minutes of inactivity.
331 User user OK. Password required
230-Your bandwidth usage is restricted
230-OK. Current restricted directory is /
230 1941 Kbytes used (0%) - authorized: 2048000 Kb
FTP Connection successful
谢谢你帮我。