freecplus framework -ftp client

A source code description

freecplus is a C / C ++ open source framework under a Linux system, go to the C language source code Technology Network (www.freecplus.net) download.

This article describes the ftp client Cftp class freecplus frame.

Cftp class declaration file is freecplus / _ftp.h.

Cftp class definition file is freecplus / _ftp.cpp.

Sample program located freecplus / demo directory.

Compiled rules file is freecplus / demo / makefile.

Cftp ftplib class is based on open source library of secondary packaging made, ftplib open source library header files freecplus / ftplib.h, function definition file is freecplus / ftplib.c.

ftplib C code, compiled with gcc first into the lib library files, then g ++ and _ftp.cpp and object files compiled together.

Second, an overview

This article does not cover the basics and ftp protocol ftp command, before reading this article, you must be familiar with and ftp protocol command to read the contents of this article.

Three, Cftp class

Class declaration:

class Cftp
{
public:
  netbuf *m_ftpconn;   // ftp连接句柄。
  unsigned int m_size; // 文件的大小,单位:字节。
  char m_mtime[21];    // 文件的修改时间,格式:yyyymmddhh24miss。

  // 以下三个成员变量用于存放login方法登录失败的原因。
  bool m_connectfailed;    // 连接失败。
  bool m_loginfailed;      // 登录失败,用户名和密码不正确,或没有登录权限。
  bool m_optionfailed;     // 设置传输模式失败。

  Cftp();  // 类的构造函数。
 ~Cftp();  // 类的析构函数。

  void initdata();   // 初始化m_size和m_mtime成员变量。

  // 登录ftp服务器。
  // host:ftp服务器ip地址和端口,中间用":"分隔,如"192.168.1.1:21"。
  // username:登录ftp服务器用户名。
  // password:登录ftp服务器的密码。
  // imode:传输模式,FTPLIB_PASSIVE是被动模式,FTPLIB_PORT是主动模式,缺省是被动模式。
  bool login(const char *host,const char *username,const char *password,const int imode=FTPLIB_PASSIVE);
  
  // 注销。
  bool logout();

  // 获取ftp服务器上文件的时间。
  // remotefilename:待获取的文件名。
  // 返回值:false-失败;true-成功,获取到的文件时间存放在m_mtime成员变量中。
  bool mtime(const char *remotefilename);

  // 获取ftp服务器上文件的大小。
  // remotefilename:待获取的文件名。
  // 返回值:false-失败;true-成功,获取到的文件大小存放在m_size成员变量中。
  bool size(const char *remotefilename);

  // 改变ftp服务器的当前工作目录。
  // remotedir:ftp服务器上的目录名。
  // 返回值:true-成功;false-失败。
  bool chdir(const char *remotedir);

  // 在ftp服务器上创建目录。
  // remotedir:ftp服务器上待创建的目录名。
  // 返回值:true-成功;false-失败。
  bool mkdir(const char *remotedir);

  // 删除ftp服务器上的目录。
  // remotedir:ftp服务器上待删除的目录名。
  // 返回值:true-成功;false-失败。
  bool rmdir(const char *remotedir);

  // 发送NLST命令列出ftp服务器的目录和文件名。
  // remotedir:ftp服务器的目录名。
  // listfilename:用于保存从服务器返回的目录和文件名列表。
  // 返回值:true-成功;false-失败。
  // 注意:如果列出的是ftp服务器当前目录,remotedir用"","*","."都可以,但是,不规范的ftp服务器可能有差别。
  bool nlist(const char *remotedir,const char *listfilename);

  // 从ftp服务器上获取文件。
  // remotefilename:待获取ftp服务器上的文件名。
  // localfilename:保存到本地的文件名。
  // bCheckMTime:文件传输完成后,是否核对远程文件传输前后的时间,保证文件的完整性。
  // 返回值:true-成功;false-失败。
  // 注意:文件在传输的过程中,采用临时文件命名的方法,即在localfilename后加".tmp",在传输
  // 完成后才正式改为localfilename。
  bool get(const char *remotefilename,const char *localfilename,const bool bCheckMTime=true);

  // 向ftp服务器发送文件。
  // localfilename:本地待发送的文件名。
  // remotefilename:发送到ftp服务器上的文件名。
  // bCheckSize:文件传输完成后,是否核对本地文件和远程文件的大小,保证文件的完整性。
  // 返回值:true-成功;false-失败。
  // 注意:文件在传输的过程中,采用临时文件命名的方法,即在remotefilename后加".tmp",在传输
  // 完成后才正式改为remotefilename。
  bool put(const char *localfilename,const char *remotefilename,const bool bCheckSize=true);

  // 删除ftp服务器上的文件。
  // remotefilename:待删除的ftp服务器上的文件名。
  // 返回值:true-成功;false-失败。
  bool ftpdelete(const char *remotefilename);

  // 重命名ftp服务器上的文件。
  // srcremotefilename:ftp服务器上的原文件名。
  // dstremotefilename:ftp服务器上的目标文件名。
  // 返回值:true-成功;false-失败。
  bool ftprename(const char *srcremotefilename,const char *dstremotefilename);

  /* 以下三个方法如果理解不了就算了,可以不启用。 */
  // 发送LIST命令列出ftp服务器目录中的文件。
  // 参数和返回值与nlist方法相同。
  bool dir(const char *remotedir,const char *listfilename);

  // 向ftp服务器发送site命令。
  // command:命令的内容。
  // 返回值:true-成功;false-失败。
  bool site(const char *command);

  // 获取服务器返回信息的最后一条(return a pointer to the last response received)。
  char *response();
};

Fourth, prepare the test environment

Before the demo program, I think you are already a professional C / C ++ programmers, familiar with the Linux operating system, familiar with the ftp protocol.

1. Create a Linux operating system users

Freecplus create users, user groups for the bin, the user root directory is / home / freecplus, password freecpluspwd

Here Insert Picture Description
2, an ftp server Installation and Configuration

For details, see C Language Technology Network (www.freecplus.net) related article, or search related articles on the Internet.

3, configure the firewall

For details, see C Language Technology Network (www.freecplus.net) related article, or search related articles on the Internet.

4, ready to test files

Copy the source code into the frame freecplus / home / freecplus directory, as follows:

Here Insert Picture Description

Fifth, the sample program

1, to obtain a list of file servers, time, and size

Example (demo50.cpp)

/*
 *  程序名:demo50.cpp,此程序演示采用freecplus框架的Cftp类获取服务器文件列表、时间和大小。
 *  作者:C语言技术网(www.freecplus.net) 日期:20190525
*/
#include "../_ftp.h"

int main(int argc,char *argv[])
{
  Cftp ftp;

  // 登录远程FTP服务器,请改为您自己服务器的ip地址。
  if (ftp.login("172.16.0.15:21","freecplus","freecpluspwd",FTPLIB_PASSIVE) == false)
  {
    printf("ftp.login(172.16.0.15:21(freecplus/freecpluspwd)) failed.\n"); return -1;
  }

  // 获取服务器上/home/freecplus/*.h文件列表,保存在本地的/tmp/list/tmp.list文件中。
  // 如果/tmp/list目录不存在,就创建它。
  if (ftp.nlist("/home/freecplus/*.h","/tmp/list/tmp.list")==false) 
  { printf("ftp.nlist() failed.\n"); return -1; }

  CFile File;    // 采用freecplus框架的CFile类来操作list文件。
  char strFileName[301];

  File.Open("/tmp/list/tmp.list","r");  // 打开list文件。

  while(true)    // 获取每个文件的时间和大小。
  {
    if (File.Fgets(strFileName,300,true)==false) break;

    ftp.mtime(strFileName); // 获取文件时间。
    ftp.size(strFileName);  // 获取文件大小。
  
    printf("filename=%s,mtime=%s,size=%d\n",strFileName,ftp.m_mtime,ftp.m_size);   
  }
}

running result

Here Insert Picture Description

2, upload files to the server

Example (demo51.cpp)

/*
 *  程序名:demo51.cpp,此程序演示采用freecplus框架的Cftp类上传文件。
 *  作者:C语言技术网(www.freecplus.net) 日期:20190525
*/
#include "../_ftp.h"

int main(int argc,char *argv[])
{
  Cftp ftp;

  // 登录远程FTP服务器,请改为您自己服务器的ip地址。
  if (ftp.login("172.16.0.15:21","freecplus","freecpluspwd",FTPLIB_PASSIVE) == false)
  {
    printf("ftp.login(172.16.0.15:21(freecplus/freecpluspwd)) failed.\n"); return -1;
  }

  // 在ftp服务器上创建/home/freecplus/tmp,注意,如果目录已存在,会返回失败。
  if (ftp.mkdir("/home/freecplus/tmp")==false) { printf("ftp.mkdir() failed.\n"); return -1; }
  
  // 把ftp服务器上的工作目录切换到/home/freecplus/tmp
  if (ftp.chdir("/home/freecplus/tmp")==false) { printf("ftp.chdir() failed.\n"); return -1; }

  // 把本地的demo51.cpp上传到ftp服务器的当前工作目录。
  ftp.put("demo51.cpp","demo51.cpp");

  // 如果不调用chdir切换工作目录,以下代码也可以直接上传文件。
  // ftp.put("demo51.cpp","/home/freecplus/tmp/demo51.cpp");
  
  printf("put demo51.cpp ok.\n");  
}

3, download files from the server

Example (demo52.cpp)

/*
 *  程序名:demo52.cpp,此程序演示采用freecplus框架的Cftp类下载文件。
 *  作者:C语言技术网(www.freecplus.net) 日期:20190525
*/
#include "../_ftp.h"

int main(int argc,char *argv[])
{
  Cftp ftp;

  // 登录远程FTP服务器,请改为您自己服务器的ip地址。
  if (ftp.login("172.16.0.15:21","freecplus","freecpluspwd",FTPLIB_PASSIVE) == false)
  {
    printf("ftp.login(172.16.0.15:21(freecplus/freecpluspwd)) failed.\n"); return -1;
  }

  // 把服务器上的/home/freecplus/tmp/demo51.cpp下载到本地,存为/tmp/test/demo51.cpp。
  // 如果本地的/tmp/test目录不存在,就创建它。
  if (ftp.get("/home/freecplus/tmp/demo51.cpp","/tmp/test/demo51.cpp")==false)
  { printf("ftp.get() failed.\n"); return -1; }

  printf("get /home/freecplus/tmp/demo51.cpp ok.\n");  

  // 删除服务上的/home/freecplus/tmp/demo51.cpp文件。
  if (ftp.ftpdelete("/home/freecplus/tmp/demo51.cpp")==false) 
  { printf("ftp.ftpdelete() failed.\n"); return -1; }

  printf("delete /home/freecplus/tmp/demo51.cpp ok.\n");  

  // 删除服务器上的/home/freecplus/tmp目录,如果目录非空,删除将失败。
  if (ftp.rmdir("/home/freecplus/tmp")==false) { printf("ftp.rmdir() failed.\n");return -1; }
}

Sixth, Copyright

C Language Technology Network original article, reproduced please indicate the source link to the article, the author and original.
Source: C Language Technology Network (www.freecplus.net)
Author: Ethics code Agriculture

If the article typos, or content errors, or other suggestions and comments, please correct me message, thank you very much! ! !

Published 159 original articles · won praise 458 · views 120 000 +

Guess you like

Origin blog.csdn.net/wucz122140729/article/details/105187660