FTP ACE

1、ffcs_ftp_client.h

  1. #ifndef    FTP_CLIENT_ACE_H  
  2. #define    FTP_CLIENT_ACE_H  
  3.   
  4. #include <stdio.h>  
  5.   
  6. #include <string>  
  7. #include <sstream>  
  8. #include <iostream>  
  9.   
  10. #include <ace/OS.h>  
  11. #include <ace/Reactor.h>  
  12. #include <ace/SOCK_Acceptor.h>  
  13. #include <ace/SOCK_Connector.h>  
  14. #include <ace/SOCK_Stream.h>  
  15. #include <ace/Acceptor.h>  
  16. #include <ace/Connector.h>  
  17.   
  18. #define MAX_CONN_TIMEOUT_SECOND      3 /*TCP connection timeout in second.*/  
  19. #define MAX_CONN_TIMEOUT_MILLISECOND 0 /*TCP connection timeout in ms.*/  
  20.   
  21. #define MAX_RECV_TIMEOUT_SECOND      1 /*TCP connection timeout in second.*/  
  22. #define MAX_RECV_TIMEOUT_MILLISECOND 0 /*TCP connection timeout in ms.*/  
  23.   
  24. #define MAX_BUFSIZE 1024  
  25.   
  26. /************************************************************************/  
  27. /* ACE库实现FTP功能,支持发送和接受文件                                 */  
  28. /************************************************************************/  
  29. class FTPClient  
  30. {  
  31. public:  
  32.     FTPClient(const std::string &remote_ip, const u_short remote_port,  
  33.               const std::string &user_name, const std::string &pass_word,  
  34.               int os_type = OS_DEFAULT);  
  35.     virtual ~FTPClient();  
  36.   
  37.     bool LogoIn();  
  38.     bool LogoOut();  
  39.   
  40.     bool GetSysInfo();  
  41.     bool Noop();  
  42.   
  43.     bool ChangeLocalDir(const std::string &dirname);  
  44.     bool ChangeRemoteDir(const std::string &dirname);  
  45.   
  46.     bool PutFile(const std::string &filename);  
  47.     bool GetFile(const std::string &filename);  
  48.   
  49.     bool List(std::string &pathlist, const std::string &pathname = "");  
  50.     bool NList(std::string &pathlist, const std::string &pathname = "");  
  51.   
  52.     bool ReName(const std::string &srcname, const std::string &dstname);  
  53.   
  54. private:  
  55.     bool Recv(std::string &response);  
  56.     bool Send(const std::string &command);  
  57.   
  58.     std::string    user_name_, pass_word_;  
  59.     ACE_INET_Addr  remote_addr_;  
  60.     ACE_SOCK_Connector connector_;  
  61.     ACE_SOCK_Stream    peer_;  
  62.   
  63.     int os_type_;  
  64.   
  65. public:  
  66.     enum  
  67.     {  
  68.         OS_DEFAULT = 0,  
  69.         OS_LINUX   = 1,  
  70.         OS_WIN32   = 2,  
  71.         OS_AIX     = 3,  
  72.         OS_HP_UNIX = 4,  
  73.         OS_SUNOS   = 5  
  74.     };  
  75. };  
  76.   
  77. #endif  

 

2、ffcs_ftp_client.cpp

  1. /*================================================================================================== 
  2.  * 项目名称: FTP操作库 
  3.  *     功能: 提供类和方法,实现FTP基本功能 
  4.  *     作者: huangjf 
  5.  *     联系: huangjf@ffcs.cn 
  6.  * 最近修改: 2010-9-1 
  7.  *     版本: v1.0.2 
  8.   ==================================================================================================*/  
  9.   
  10. #include <stdio.h>  
  11. #include <stdlib.h>  
  12. #include <ace/OS.h>  
  13. #include <ace/FILE_Addr.h>  
  14. #include <ace/FILE_Connector.h>  
  15. #include <ace/FILE_IO.h>  
  16. #include <ace/OS_NS_string.h>  
  17. #include <ace/OS_NS_stdio.h>  
  18.   
  19. #include "ffcs_ftp_client.h"  
  20.   
  21. FTPClient::FTPClient(const std::string &remote_ip, const u_short remote_port,  
  22.                      const std::string &user_name, const std::string &pass_word,  
  23.                      int os_type)  
  24. {  
  25.     this->user_name_ = user_name;  
  26.     this->pass_word_ = pass_word;  
  27.     this->remote_addr_.set((u_short)remote_port, remote_ip.c_str());  
  28.   
  29.     this->os_type_ = os_type;  
  30. }  
  31.   
  32. FTPClient::~FTPClient()  
  33. {  
  34.     peer_.close_writer();  
  35.     peer_.close_reader();  
  36.     peer_.close();  
  37. }  
  38.   
  39. /*接收FTP应答*/  
  40. bool FTPClient::Recv(std::string &response)  
  41. {  
  42.     std::string     line;  
  43.     ACE_Time_Value  tv(MAX_RECV_TIMEOUT_SECOND, MAX_RECV_TIMEOUT_MILLISECOND);  
  44.   
  45.     response.clear();  
  46.   
  47.     while (true)  
  48.     {  
  49.         char c;  
  50.         switch (this->peer_.recv_n(&c, 1, &tv))  
  51.         {  
  52.         case   0:  
  53.         case  -1:  
  54.             return false;  
  55.         default:  
  56.             line.append(1, c);  
  57.             break;  
  58.         }  
  59.   
  60.         if ('/n' == c)  
  61.         {  
  62.             response = line;  
  63.             break;  
  64.         }  
  65.     }  
  66.     std::cout << response << std::endl;  
  67.     return true;  
  68. }  
  69.   
  70. /*发送FTP命令*/  
  71. bool FTPClient::Send(const std::string &command)  
  72. {  
  73.     std::cout << command;  
  74.     if (static_cast<ssize_t>(command.length()) == this->peer_.send_n(command.c_str(), command.length()))  
  75.     {  
  76.         return true;  
  77.     }  
  78.     else  
  79.     {  
  80.         return false;  
  81.     }  
  82. }  
  83.   
  84. /*登录*/  
  85. bool FTPClient::LogoIn()  
  86. {  
  87.     ACE_Time_Value  tv(MAX_CONN_TIMEOUT_SECOND, MAX_CONN_TIMEOUT_MILLISECOND);  
  88.     std::stringstream USER, PASS, SYST;  
  89.     std::string  ftp_resp;  
  90.   
  91.     if (this->connector_.connect(this->peer_, this->remote_addr_, &tv) == -1)  
  92.     {  
  93.         ACE_ERROR_RETURN((LM_ERROR, ACE_TEXT("(%P|%t) %p/n"), ACE_TEXT("connection failed")), false);  
  94.     }  
  95.   
  96.     ACE_DEBUG((LM_DEBUG, ACE_TEXT("(%P|%t) connected to (%s:%d)/n/n"), this->remote_addr_.get_host_addr(), this->remote_addr_.get_port_number()));  
  97.   
  98.     if (!this->Recv(ftp_resp) || ftp_resp.substr(0, 3) != "220")  
  99.     {  
  100.         return false;  
  101.     }  
  102.   
  103.     USER << "USER " << this->user_name_ << "/r/n";  
  104.     if (this->Send(USER.str()))  
  105.     {  
  106.         if (!this->Recv(ftp_resp) || ftp_resp.substr(0, 3) != "331")  
  107.         {  
  108.             return false;  
  109.         }  
  110.     }  
  111.   
  112.     PASS << "PASS " << this->pass_word_ << "/r/n";  
  113.     if (this->Send(PASS.str()))  
  114.     {  
  115.         if (!this->Recv(ftp_resp) || ftp_resp.substr(0, 3) != "230")  
  116.         {  
  117.             return false;  
  118.         }  
  119.   
  120.         /*AIX返回的Login指令有3个*/  
  121.         if (this->os_type_ == OS_AIX)  
  122.         {  
  123.             if (!this->Recv(ftp_resp) || ftp_resp.substr(0, 3) != "230")  
  124.             {  
  125.                 return false;  
  126.             }  
  127.             if (!this->Recv(ftp_resp) || ftp_resp.substr(0, 3) != "230")  
  128.             {  
  129.                 return false;  
  130.             }  
  131.         }  
  132.     }  
  133.   
  134.     return true;  
  135. }  
  136.   
  137. /*退出*/  
  138. bool FTPClient::LogoOut()  
  139. {  
  140.     std::stringstream QUIT;  
  141.     std::string  ftp_resp;  
  142.   
  143.     QUIT << "QUIT" << "/r/n";  
  144.     if (this->Send(QUIT.str()))  
  145.     {  
  146.         if (!this->Recv(ftp_resp) || ftp_resp.substr(0, 3) != "221")  
  147.         {  
  148.             return false;  
  149.         }  
  150.     }  
  151.   
  152.     return true;  
  153. }  
  154.   
  155. /*获得远程系统信息*/  
  156. bool FTPClient::GetSysInfo()  
  157. {  
  158.     std::stringstream SYST;  
  159.     std::string  ftp_resp;  
  160.   
  161.     SYST << "SYST" << "/r/n";  
  162.     if (this->Send(SYST.str()))  
  163.     {  
  164.         if (!this->Recv(ftp_resp) || ftp_resp.substr(0, 3) != "215")  
  165.         {  
  166.             return false;  
  167.         }  
  168.     }  
  169.   
  170.     return true;  
  171. }  
  172.   
  173. /*防空闲*/  
  174. bool FTPClient::Noop()  
  175. {  
  176.     std::stringstream NOOP;  
  177.     std::string  ftp_resp;  
  178.   
  179.     NOOP << "NOOP" << "/r/n";  
  180.     if (this->Send(NOOP.str()))  
  181.     {  
  182.         if (!this->Recv(ftp_resp) || ftp_resp.substr(0, 3) != "200")  
  183.         {  
  184.             return false;  
  185.         }  
  186.     }  
  187.   
  188.     return true;  
  189. }  
  190.   
  191. /*修改本地路径,影响下载的文件所保存的路径 或者 上传的本地文件所在路径*/  
  192. bool FTPClient::ChangeLocalDir(const std::string &dirname)  
  193. {  
  194.     return ACE_OS::chdir(dirname.c_str()) < 0 ? false : true;  
  195. }  
  196.   
  197. /*修改远程路径,影响下载的远程文件所在路径 或者 上传的文件所保存的路径*/  
  198. bool FTPClient::ChangeRemoteDir(const std::string &dirname)  
  199. {  
  200.     std::stringstream CWD, PWD;  
  201.     std::string  ftp_resp;  
  202.   
  203.     CWD << "CWD " << dirname << "/r/n";  
  204.     if (this->Send(CWD.str()))  
  205.     {  
  206.         if (!this->Recv(ftp_resp) || ftp_resp.substr(0, 3) != "250")  
  207.         {  
  208.             return false;  
  209.         }  
  210.     }  
  211.   
  212.     PWD << "PWD" << "/r/n";  
  213.     if (this->Send(PWD.str()))  
  214.     {  
  215.         if (!this->Recv(ftp_resp) || ftp_resp.substr(0, 3) != "257")  
  216.         {  
  217.             return false;  
  218.         }  
  219.     }  
  220.   
  221.     return true;  
  222. }  
  223.   
  224. /*上传文件*/  
  225. bool FTPClient::PutFile(const std::string &filename)  
  226. {  
  227.     std::stringstream TYPE, PASV, STOR;  
  228.     std::string  ftp_resp;  
  229.   
  230.     ACE_Time_Value  tv(MAX_CONN_TIMEOUT_SECOND, MAX_CONN_TIMEOUT_MILLISECOND);  
  231.   
  232.     int d0, d1, d2, d3, p0, p1;  
  233.     std::stringstream ip;  
  234.     ACE_INET_Addr ftp_data_addr;  
  235.   
  236.     ACE_SOCK_Stream     stream;  
  237.     ACE_SOCK_Connector  connector;  
  238.   
  239.     ACE_FILE_Info file_info;  
  240.     ACE_FILE_IO file_put;  
  241.     ACE_FILE_Connector file_con;  
  242.     char file_cache[MAX_BUFSIZE];  
  243.     int file_size, all_size;  
  244.   
  245.     if (ACE_OS::access(filename.c_str(), F_OK) < 0) return false/*文件不存在*/  
  246.   
  247.     /*修改类型*/  
  248.     TYPE << "TYPE I" << "/r/n";  
  249.     if (this->Send(TYPE.str()))  
  250.     {  
  251.         if (!this->Recv(ftp_resp) || ftp_resp.substr(0, 3) != "200")  
  252.         {  
  253.             return false;  
  254.         }  
  255.     }  
  256.   
  257.     PASV << "PASV" << "/r/n";  
  258.     if (this->Send(PASV.str()))  
  259.     {  
  260.         if (!this->Recv(ftp_resp) || ftp_resp.substr(0, 3) != "227")  
  261.         {  
  262.             return false;  
  263.         }  
  264.     }  
  265.   
  266.     ftp_resp = ftp_resp.substr(ftp_resp.find_last_of('(')+1, (ftp_resp.find_last_of(')')-ftp_resp.find_last_of('(')-1));  
  267.   
  268.     if (sscanf(ftp_resp.c_str(), "%d,%d,%d,%d,%d,%d", &d0, &d1, &d2, &d3, &p0, &p1) == -1) return false;  
  269.     ip << d0 << "." << d1 << "." << d2 << "." << d3;  
  270.     ftp_data_addr.set((p0 << 8) + p1, ip.str().c_str());  
  271.   
  272.     if (connector.connect(stream, ftp_data_addr, &tv) == -1)  
  273.     {  
  274.         ACE_ERROR_RETURN((LM_ERROR, ACE_TEXT("(%P|%t) %p/n"), ACE_TEXT("connection failed")), false);  
  275.     }  
  276.   
  277.     ACE_DEBUG((LM_DEBUG, ACE_TEXT("(%P|%t) connected to (%s:%d)/n/n"), this->remote_addr_.get_host_addr(), this->remote_addr_.get_port_number()));  
  278.   
  279.     STOR << "STOR " << filename << "/r/n";  
  280.     if (this->Send(STOR.str()))  
  281.     {  
  282.         if (!this->Recv(ftp_resp) || ftp_resp.substr(0, 3) != "150")  
  283.         {  
  284.             return false;  
  285.         }  
  286.     }  
  287.   
  288.     if (file_con.connect(file_put, ACE_FILE_Addr(filename.c_str()), &tv, ACE_Addr::sap_any, 0, O_RDONLY, ACE_DEFAULT_FILE_PERMS) == -1)  
  289.         ACE_ERROR_RETURN((LM_ERROR, ACE_TEXT("%p/n to %s"), ACE_TEXT("open"), filename), false);  
  290.   
  291.     if (file_put.get_info (&file_info) == -1)  
  292.         ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT("%p/n"), ACE_TEXT("get_info")), false);  
  293.   
  294.     all_size = 0;  
  295.     while ((all_size < file_info.size_) && ((file_size=file_put.recv(file_cache, sizeof(file_cache))) > 0))  
  296.     {  
  297.         all_size += stream.send_n(file_cache, file_size);  
  298.     }  
  299.     file_put.close();  
  300.   
  301.     stream.close_writer();  
  302.     stream.close_reader();  
  303.     stream.close();  
  304.   
  305.     if (!this->Recv(ftp_resp) || ftp_resp.substr(0, 3) != "226")  
  306.     {  
  307.         return false;  
  308.     }  
  309.   
  310.     if (all_size != file_info.size_)  
  311.     {  
  312.         std::cout << "Send " << filename << "(" << file_info.size_ << ") fail! Only sent (" << all_size <<")" << std::endl;  
  313.         return false;  
  314.     }  
  315.   
  316.     std::cout << "Send " << filename << "(" << all_size << ") OK!" << std::endl << std::endl;  
  317.     return true;  
  318. }  
  319.   
  320. /*下载文件*/  
  321. bool FTPClient::GetFile(const std::string &filename)  
  322. {  
  323.     std::stringstream PASV, RETR;  
  324.     std::string  ftp_resp;  
  325.   
  326.     ACE_Time_Value  tv(MAX_CONN_TIMEOUT_SECOND, MAX_CONN_TIMEOUT_MILLISECOND);  
  327.   
  328.     int d0, d1, d2, d3, p0, p1;  
  329.     std::stringstream ip;  
  330.     ACE_INET_Addr ftp_data_addr;  
  331.   
  332.     ACE_SOCK_Stream     stream;  
  333.     ACE_SOCK_Connector  connector;  
  334.   
  335.     ACE_FILE_IO file_put;  
  336.     ACE_FILE_Connector file_con;  
  337.     char file_cache[MAX_BUFSIZE];  
  338.     int file_size, all_size;  
  339.   
  340.     PASV << "PASV" << "/r/n";  
  341.     if (this->Send(PASV.str()))  
  342.     {  
  343.         if (!this->Recv(ftp_resp) || ftp_resp.substr(0, 3) != "227")  
  344.         {  
  345.             return false;  
  346.         }  
  347.     }  
  348.   
  349.     ftp_resp = ftp_resp.substr(ftp_resp.find_last_of('(')+1, (ftp_resp.find_last_of(')')-ftp_resp.find_last_of('(')-1));  
  350.   
  351.     if (sscanf(ftp_resp.c_str(), "%d,%d,%d,%d,%d,%d", &d0, &d1, &d2, &d3, &p0, &p1) == -1) return false;  
  352.     ip << d0 << "." << d1 << "." << d2 << "." << d3;  
  353.     ftp_data_addr.set((p0 << 8) + p1, ip.str().c_str());  
  354.   
  355.     if (connector.connect(stream, ftp_data_addr, &tv) == -1)  
  356.     {  
  357.         ACE_ERROR_RETURN((LM_ERROR, ACE_TEXT("(%P|%t) %p/n"), ACE_TEXT("connection failed")), false);  
  358.     }  
  359.   
  360.     ACE_DEBUG((LM_DEBUG, ACE_TEXT("(%P|%t) connected to (%s:%d)/n/n"), this->remote_addr_.get_host_addr(), this->remote_addr_.get_port_number()));  
  361.   
  362.     RETR << "RETR " << filename << "/r/n";  
  363.     if (this->Send(RETR.str()))  
  364.     {  
  365.         if (!this->Recv(ftp_resp) || ftp_resp.substr(0, 3) != "150")  
  366.         {  
  367.             return false;  
  368.         }  
  369.     }  
  370.   
  371.     if (file_con.connect(file_put, ACE_FILE_Addr(filename.c_str()), &tv, ACE_Addr::sap_any, 0, O_RDWR|O_CREAT, ACE_DEFAULT_FILE_PERMS) == -1)  
  372.         ACE_ERROR_RETURN((LM_ERROR, ACE_TEXT("%p/n to %s"), ACE_TEXT("open"), filename), false);  
  373.   
  374.     all_size = 0;  
  375.     while ((file_size = stream.recv(file_cache, sizeof(file_cache))) > 0)  
  376.     {  
  377.         all_size += file_put.send_n(file_cache, file_size);  
  378.     }  
  379.     file_put.close();  
  380.   
  381.     stream.close_writer();  
  382.     stream.close_reader();  
  383.     stream.close();  
  384.   
  385.     if (!this->Recv(ftp_resp) || ftp_resp.substr(0, 3) != "226")  
  386.     {  
  387.         return false;  
  388.     }  
  389.   
  390.     std::cout << "Recv " << filename << "(" << all_size << ") OK!" << std::endl << std::endl;  
  391.     return true;  
  392. }  
  393.   
  394. /*名字列表*/  
  395. bool FTPClient::NList(std::string &pathlist, const std::string &pathname)  
  396. {  
  397.     std::stringstream PASV, NLST;  
  398.     std::string  ftp_resp;  
  399.   
  400.     ACE_Time_Value  tv(MAX_CONN_TIMEOUT_SECOND, MAX_CONN_TIMEOUT_MILLISECOND);  
  401.   
  402.     int d0, d1, d2, d3, p0, p1;  
  403.     std::stringstream ip;  
  404.     ACE_INET_Addr ftp_data_addr;  
  405.   
  406.     ACE_SOCK_Stream     stream;  
  407.     ACE_SOCK_Connector  connector;  
  408.   
  409.     char file_cache[MAX_BUFSIZE] = {0};  
  410.     int file_size;  
  411.   
  412.     PASV << "PASV" << "/r/n";  
  413.     if (this->Send(PASV.str()))  
  414.     {  
  415.         if (!this->Recv(ftp_resp) || ftp_resp.substr(0, 3) != "227")  
  416.         {  
  417.             return false;  
  418.         }  
  419.     }  
  420.   
  421.     ftp_resp = ftp_resp.substr(ftp_resp.find_last_of('(')+1, (ftp_resp.find_last_of(')')-ftp_resp.find_last_of('(')-1));  
  422.   
  423.     if (sscanf(ftp_resp.c_str(), "%d,%d,%d,%d,%d,%d", &d0, &d1, &d2, &d3, &p0, &p1) == -1) return false;  
  424.     ip << d0 << "." << d1 << "." << d2 << "." << d3;  
  425.     ftp_data_addr.set((p0 << 8) + p1, ip.str().c_str());  
  426.   
  427.     if (connector.connect(stream, ftp_data_addr, &tv) == -1)  
  428.     {  
  429.         ACE_ERROR_RETURN((LM_ERROR, ACE_TEXT("(%P|%t) %p/n"), ACE_TEXT("connection failed")), false);  
  430.     }  
  431.   
  432.     ACE_DEBUG((LM_DEBUG, ACE_TEXT("(%P|%t) connected to (%s:%d)/n/n"), this->remote_addr_.get_host_addr(), this->remote_addr_.get_port_number()));  
  433.   
  434.     if (pathname.empty())  
  435.     {  
  436.         NLST << "NLST" << "/r/n";  
  437.     }  
  438.     else  
  439.     {  
  440.         NLST << "NLST " << pathname << "/r/n";  
  441.     }  
  442.     if (this->Send(NLST.str()))  
  443.     {  
  444.         if (!this->Recv(ftp_resp) || ftp_resp.substr(0, 3) != "150")  
  445.         {  
  446.             return false;  
  447.         }  
  448.     }  
  449.   
  450.     pathlist.clear();  
  451.     while ((file_size = stream.recv(file_cache, sizeof(file_cache)-1)) > 0)  
  452.     {  
  453.         pathlist.append(file_cache);  
  454.   
  455.         ACE_OS::memset(file_cache, 0x00, sizeof(file_cache));  
  456.     }  
  457.   
  458.     stream.close_writer();  
  459.     stream.close_reader();  
  460.     stream.close();  
  461.   
  462.     if (!this->Recv(ftp_resp) || ftp_resp.substr(0, 3) != "226")  
  463.     {  
  464.         return false;  
  465.     }  
  466.   
  467.     return true;  
  468. }  
  469.   
  470. /*列表*/  
  471. bool FTPClient::List(std::string &pathlist, const std::string &pathname)  
  472. {  
  473.     std::stringstream PASV, LIST;  
  474.     std::string  ftp_resp;  
  475.   
  476.     ACE_Time_Value  tv(MAX_CONN_TIMEOUT_SECOND, MAX_CONN_TIMEOUT_MILLISECOND);  
  477.   
  478.     int d0, d1, d2, d3, p0, p1;  
  479.     std::stringstream ip;  
  480.     ACE_INET_Addr ftp_data_addr;  
  481.   
  482.     ACE_SOCK_Stream     stream;  
  483.     ACE_SOCK_Connector  connector;  
  484.   
  485.     char file_cache[MAX_BUFSIZE] = {0};  
  486.     int file_size;  
  487.   
  488.     PASV << "PASV" << "/r/n";  
  489.     if (this->Send(PASV.str()))  
  490.     {  
  491.         if (!this->Recv(ftp_resp) || ftp_resp.substr(0, 3) != "227")  
  492.         {  
  493.             return false;  
  494.         }  
  495.     }  
  496.   
  497.     ftp_resp = ftp_resp.substr(ftp_resp.find_last_of('(')+1, (ftp_resp.find_last_of(')')-ftp_resp.find_last_of('(')-1));  
  498.   
  499.     if (sscanf(ftp_resp.c_str(), "%d,%d,%d,%d,%d,%d", &d0, &d1, &d2, &d3, &p0, &p1) == -1) return false;  
  500.     ip << d0 << "." << d1 << "." << d2 << "." << d3;  
  501.     ftp_data_addr.set((p0 << 8) + p1, ip.str().c_str());  
  502.   
  503.     if (connector.connect(stream, ftp_data_addr, &tv) == -1)  
  504.     {  
  505.         ACE_ERROR_RETURN((LM_ERROR, ACE_TEXT("(%P|%t) %p/n"), ACE_TEXT("connection failed")), false);  
  506.     }  
  507.   
  508.     ACE_DEBUG((LM_DEBUG, ACE_TEXT("(%P|%t) connected to (%s:%d)/n/n"), this->remote_addr_.get_host_addr(), this->remote_addr_.get_port_number()));  
  509.   
  510.     LIST << "LIST " << pathname << "/r/n";  
  511.     if (this->Send(LIST.str()))  
  512.     {  
  513.         if (!this->Recv(ftp_resp) || ftp_resp.substr(0, 3) != "150")  
  514.         {  
  515.             return false;  
  516.         }  
  517.     }  
  518.   
  519.     pathlist.clear();  
  520.     while ((file_size = stream.recv(file_cache, sizeof(file_cache))) > 0)  
  521.     {  
  522.         pathlist.append(file_cache);  
  523.   
  524.         ACE_OS::memset(file_cache, 0x00, sizeof(file_cache));  
  525.     }  
  526.   
  527.     stream.close_writer();  
  528.     stream.close_reader();  
  529.     stream.close();  
  530.   
  531.     if (!this->Recv(ftp_resp) || ftp_resp.substr(0, 3) != "226")  
  532.     {  
  533.         return false;  
  534.     }  
  535.   
  536.     return true;  
  537. }  
  538.   
  539. /*更名*/  
  540. bool FTPClient::ReName(const std::string &srcname, const std::string &dstname)  
  541. {  
  542.     std::stringstream RNFR, RNTO;  
  543.     std::string  ftp_resp;  
  544.   
  545.     RNFR << "RNFR " << srcname << "/r/n";  
  546.     if (this->Send(RNFR.str()))  
  547.     {  
  548.         if (!this->Recv(ftp_resp) || ftp_resp.substr(0, 3) != "350")  
  549.         {  
  550.             return false;  
  551.         }  
  552.     }  
  553.   
  554.     RNTO << "RNTO " << dstname << "/r/n";  
  555.     if (this->Send(RNTO.str()))  
  556.     {  
  557.         if (!this->Recv(ftp_resp) || ftp_resp.substr(0, 3) != "250")  
  558.         {  
  559.             return false;  
  560.         }  
  561.     }  
  562.   
  563.     return true;  
  564. }  

 

3、main.cpp

  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3.   
  4. #include <string>  
  5.   
  6. #include <ace/OS.h>  
  7.   
  8. #include "ffcs_ftp_client.h"  
  9.   
  10. int ACE_TMAIN(int argc, ACE_TCHAR* argv[])  
  11. {  
  12.     std::string ftp_host = "10.10.10.100";  
  13.             int ftp_port = 21;  
  14.     std::string ftp_user = "huangjf";  
  15.     std::string ftp_pwd  = "12345678";  
  16.   
  17.     std::string ftp_local_dir = "D://workspace//library//c++//ffcs_ftp_ace//ffcs_ftp_ace";  
  18.     std::string ftp_remote_dir = "/home/huangjf/tmp";  
  19.   
  20.     std::string ftp_local_file_tmp = "main.cpp";  
  21.     std::string ftp_local_file = "main.cpp.cpp";  
  22.   
  23.     try  
  24.     {  
  25.         FTPClient ftp_client(ftp_host, ftp_port, ftp_user, ftp_pwd);  
  26.   
  27.         ftp_client.LogoIn();  
  28.   
  29.         ftp_client.ChangeLocalDir(ftp_local_dir);  
  30.   
  31.         ftp_client.ChangeRemoteDir(ftp_remote_dir);  
  32.   
  33.         ftp_client.PutFile(ftp_local_file_tmp);  
  34.   
  35.         ftp_client.ReName(ftp_local_file_tmp, ftp_local_file);  
  36.   
  37.         ftp_client.LogoOut();  
  38.     }  
  39.     catch (std::exception& exc)  
  40.     {  
  41.         printf("[%d:%s]:%s/n", __LINE__, __FILE__, exc.what());  
  42.   
  43.         return 0;  
  44.     }  
  45.   
  46.     getchar();  
  47.     return 0;  
  48. }  

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值