FTP的C++实现
被动方式 ,连接FTP,然后上传下载文件。
linux下连接,源码如下:
FTP.h
[cpp]
#ifndef FTP_H_
#define FTP_H_
#include <sys/types.h>
#include <sys/socket.h>
class CFTP
{
public:
CFTP(void);
~CFTP(void);
//连接ftp服务器
int ftp_connect(const char* ip);
//登录ftp服务器
int ftp_login(char* user,char* pass);
//显示当前目录
int ftp_pwd(char* buff);
//更改目录
int ftp_cd(char* dir);
//返回上层目录
int ftp_cdup();
//创建目录
int ftp_mkdir(char* dir);
//删除目录
int ftp_rmdir(char* dir);
//数据传输模式
int ftp_setpasv();
//上传文件
int ftp_upload(char* localfile,char* remotepath,char* remotefilename);
//下载文件
int ftp_download(char* localfile,char* remotefile);
//退出登录
int ftp_quit();
private:
int m_sockctrl;//控制连接socket
int m_sockdata;//数据连接socket
char m_cmd[256];//存放指令
char m_resp[256];//存放返回语句
char m_ip[64];//保存ip
int ftp_sendcmd();//发送指令
int ftp_checkresp(char expresp);//接收返回状态,检测是否成功
int ftp_mkdirSingle(char* dir);
};
#endif
FTP.cpp
[cpp]
#include <iostream>
#include <netinet/in.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "FTP.h"
using namespace std;
CFTP::CFTP(void)
{
}
CFTP::~CFTP(void)
{
}
int CFTP::ftp_checkresp(char expresp)
{
int len = recv(m_sockctrl,m_resp,256,0);
if(-1 == len)return -1;
m_resp[len]=0;
puts(m_resp);//应该保存ftp运行日志
if(m_resp[0]!=expresp)return -1;
return 0;
}
int CFTP::ftp_sendcmd()
{
int ret = send(m_sockctrl,m_cmd,strlen(m_cmd),0);
if(-1 == ret)return -1;
return 0;
}
int CFTP::ftp_connect(const char* ip)
{
m_sockctrl = socket(AF_INET,SOCK_STREAM,0);
if(0==m_sockctrl)return -1;
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(21);
addr.sin_addr.s_addr = inet_addr(ip);
int err = connect(m_sockctrl,(sockaddr*)&addr,sizeof(addr));
if(err)return -1;
err = ftp_checkresp('2');
if(err)return -1;
return 0;
}
int CFTP::ftp_login(char* user,char* pass)
{
sprintf(m_cmd,"USER %s\r\n",user);
int err = ftp_sendcmd();
if(err)return -1;
err = ftp_checkresp('3');
if(err)return -1;
sprintf(m_cmd,"PASS %s\r\n",pass);
err = ftp_sendcmd();
if(err)return -1;
err = ftp_checkresp('2');
if(err)return -1;
return 0;
}
int CFTP::ftp_quit()
{
sprintf(m_cmd,"QUIT\r\n");
int err = ftp_sendcmd();
if(err)return -1;
err = ftp_checkresp('2');
if(err)return -1;
return 0;
}
int CFTP::ftp_cd(char* dir)
{
sprintf(m_cmd,"CWD %s\r\n",dir);
int err = ftp_sendcmd();
if(err)return -1;
err = ftp_checkresp('2');
if(err)return -1;
return 0;
}
int CFTP::ftp_cdup()
{
sprintf(m_cmd,"CDUP\r\n");
int err = ftp_sendcmd();
if(err)return -1;
err = ftp_checkresp('2');
if(err)return -1;
return 0;
}
int CFTP::ftp_pwd(char* buff)
{
sprintf(m_cmd,"PWD\r\n");
int err = ftp_sendcmd();
if(err)return -1;
err = ftp_checkresp('2');
if(err)return -1;
char* p=m_resp;
while(*p)
{
if(*p++ == '"')
while(*p!='"')
*buff++=*p++;
}
*buff=0;
printf("current work directory is : %s\n",buff);
return 0;
}
int CFTP::ftp_mkdirSingle(char* dir)
{
sprintf(m_cmd,"MKD %s\r\n",dir);
int err = ftp_sendcmd();
if(err)return -1;
err = ftp_checkresp('2');
if(err)return -1;
return 0;
}
int CFTP::ftp_mkdir(char* dir)
{
char path[300];
int err = ftp_cd("/");
if(err)return -1;
int i,j;
// printf("strlen(dir):%d",strlen(dir));
for(i=1,j=0;i<strlen(dir);i++) //第一个字节是根目录
{
&nb
补充:软件开发 , C++ ,