首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在嵌入式设备上设置FTP客户端连接?

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

Stack Overflow用户
提问于 2020-11-10 21:30:06
回答 2查看 173关注 0票数 0

我在一个mbed项目的FTP脚本上工作。我使用B-L475E-IOT01A开发板,尝试将文件发送到FTP-Server。因此,我不能用这个库https://os.mbed.com/users/dkato/code/ftp-client/#e069c405c934建立与服务器的连接。不幸的是,客户端并没有像它应该工作的那样工作。我试着像这样建立连接:

代码语言:javascript
复制
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;
}

在终端上,之后我在控制台中得到如下输出:

代码语言:javascript
复制
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服务器进行清晰的通信。你能帮我吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-11-11 06:20:36

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

这对于保持您与服务器的同步是必不可少的……否则,您将开始收到与您认为的命令不同的响应。

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

您的命令行还必须(根据协议,这是强制性的)以序列\r\n (按该顺序)结束,否则您不能这样做,或者您可以到达不理解您的服务器。

有关ftp协议如何工作的详细信息,请查看RFC-959 - File Transfer Protocol。传输通常在一个新的、不同的TCP连接中处理,因此您通常必须管理控制连接以及与其并行运行的一系列数据传输。

票数 0
EN

Stack Overflow用户

发布于 2020-11-12 18:39:13

好了,我终于建立了连接。同步服务器和客户端是正确的。此外,我需要时不时地释放缓冲区,现在输出不那么混乱了。下面是我的代码:

代码语言:javascript
复制
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;
}

我得到了输出:

代码语言:javascript
复制
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

谢谢你对我的照顾。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64769886

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档