如何在嵌入式设备上设置 FTP-Client 连接?

How Can I Setup a FTP-Client Connection on embedded device?

我正在为 mbed 项目编写 FTP 脚本。我使用 B-L475E-IOT01A 开发板并尝试将文件发送到 FTP-Server。因此,我无法使用此库 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)\r\n", __FILE__, __LINE__);
        return false;
    }

    //recieve ftp server message
    if (FTPClientControlSock.recv(p_ftp_buf, FTP_BUF_SIZE) <= 0) {
        printf("ERROR: %s(%d)\r\n", __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)\r\n", __FILE__, __LINE__);
        return false;
    }

    //store user info in ftp communication and send it
    sprintf(p_ftp_buf, "USER %s\r\n", 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)\r\n", __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)\r\n", __FILE__, __LINE__);
        return false;
    }

    //store password in string and send it to server
    sprintf(p_ftp_buf, "PASS %s\r\n", 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)\r\n", __FILE__, __LINE__);
        return false;
    }

    //check login was successful
    if (strncmp(p_ftp_buf, "230", 3) != 0) {
        printf("ERROR: %s(%d)\r\n", __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,来自服务器的所有响应都遵循一个模式。您需要等待来自服务器的 \r\n 序列。您需要过滤 TELNET 协议转义序列(以字节 0xff 或 0xfe 开头,我不记得了)并且代码必须始终位于一行的开头。此外,带有 - 而不是 space 的数字表示消息更大,您应该期待另一行(每条消息的最后一行都有一个数字代码,后跟 space)

这对于保持您与服务器的同步至关重要....否则您将开始收到对与您想象的不同的命令的响应。

从输出中不清楚您尝试做什么,因为您唯一显示的是服务器的登录部分。

你的命令行也有(这是协议强制要求的)以序列结束 \r\n(按那个顺序)你不能这样做,否则你可以到达一个不理解你的服务器.

检查 RFC-959 - File Transfer Protocol 以了解有关 ftp 协议如何工作的详细信息。传输通常在一个新的、不同的 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)\r\n", __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)\r\n", __FILE__, __LINE__);
    return false;
  }
  if (strncmp(p_ftp_buf, "220", 3) != 0) {
    printf("ERROR: %s(%d)\r\n", __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)\r\n", __FILE__, __LINE__);
    return false;
  }
  if (strncmp(p_ftp_buf, "220", 3) != 0) {
    printf("ERROR: %s(%d)\r\n", __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 %s\r\n", 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)\r\n", __FILE__, __LINE__);
    return false;
  }
  if (strncmp(p_ftp_buf, "331", 3) != 0) {
    printf("ERROR: %s(%d)\r\n", __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 %s\r\n", 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: %d\r\n", __LINE__);
    printf("%s", p_ftp_buf);
    printf("ERROR: %s(%d)\r\n", __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: %d\r\n", __LINE__);
    printf("%s", p_ftp_buf);
    printf("ERROR: %s(%d)\r\n", __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)\r\n", __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

谢谢你帮助我。