繁体   English   中英

如何在嵌入式设备上设置 FTP 客户端连接?

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

我正在为 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)\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 服务器进行清晰的通信。 你能帮我吗?

朱利安,来自服务器的所有响应都遵循一个模式......你需要等待来自服务器的\\r\\n序列。 您需要过滤 TELNET 协议转义序列(以字节 0xff 或 0xfe 开头,我不记得了)并且代码必须始终位于行首。 此外,带有-而不是空格的数字表示消息较大,您应该期待另一行(每条消息的最后一行都有一个数字代码,后跟一个空格)

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

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

您的命令行也有(这是协议强制性的)以序列\\r\\n (按该顺序)结束,您不能这样做,或者您可以访问不理解您的服务器。

检查RFC-959 - 文件传输协议以了解有关 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

感谢你们对我的帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM