您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 信息化管理 > FTP客户端C++版
(1)头文件:FTPClient.h#includeconio.h#defineMAX_SIZE4096#includeiostream#includewinsock2.h#includestring#includefstream#pragmacomment(lib,ws2_32.lib)usingnamespacestd;classFTPClient{public:boolRecvReply();//控制连接接收boolSendCommand();//向FTP服务器发送命令boolDataConnect(char*ServerAddr);//向FTP服务器发送命令boolmkdirectory();//FTP服务器发送MKD命令boolchangedir();//FTP服务器发送CWD命令boolFTPConnection(char*FTPIP,intport);//建立与Socket库绑定booluseuser();//向FTP服务器发送USER命令boolusepass();//向FTP服务器发送PASS命令voidsubcommend(string&filepath,string&filename);//输入和转换IP地址voidstorfile(char*FTPIP);//上传文件voidretrfile(char*FTPIP);//下载文件voidlistftp(char*FTPIP);//列出FTP服务器目录voiddeletefile();//删除文件voidquitftp();//退出客户端voidhelp();//帮助voidabout();//aboutboolishavedetail;charCmdBuf[MAX_SIZE];charCommand[MAX_SIZE];charReplyMsg[MAX_SIZE];private:intnReplycode;boolbConnected;boolbuser;SOCKETSocketControl;SOCKETSocketData;};(2)源文件:FTPClient.cpp#includeconio.h#defineMAX_SIZE4096#includeiostream#includewinsock2.h#includestring#includefstream#includeFTPClient.husingnamespacestd;#pragmacomment(lib,ws2_32.lib)boolFTPClient::RecvReply()//控制连接接收{intnRecv;memset(ReplyMsg,0,MAX_SIZE);nRecv=recv(SocketControl,ReplyMsg,MAX_SIZE,0);if(nRecv==SOCKET_ERROR){coutSocketreceiveerror!endl;closesocket(SocketControl);returnfalse;}//获取相应信息与响应码if(nRecv4){char*ReplyCodes=newchar[3];memset(ReplyCodes,0,3);memcpy(ReplyCodes,ReplyMsg,3);nReplycode=atoi(ReplyCodes);}returntrue;}boolFTPClient::SendCommand()//向FTP服务器发送命令{//控制连接发送数据intnSend;nSend=send(SocketControl,Command,strlen(Command),0);if(nSend==SOCKET_ERROR){coutSocketsenderror!endl;returnfalse;}returntrue;}boolFTPClient::DataConnect(char*ServerAddr){//向FTP服务器发送PASV命令memset(Command,0,MAX_SIZE);memcpy(Command,PASV,strlen(PASV));memcpy(Command+strlen(PASV),\r\n,2);if(!SendCommand())returnfalse;//获取PASV命令的应答信息if(RecvReply()){if(nReplycode!=227){coutPASVresponseerror!endl;closesocket(SocketControl);returnfalse;}}//分离PASV命令应答信息char*part[6];if(strtok(ReplyMsg,()){for(inti=0;i5;i++){part[i]=strtok(NULL,,);if(!part[i])returnfalse;}part[5]=strtok(NULL,));if(!part[5])returnfalse;}else{returnfalse;}//获取FTP服务器数据端口、unsignedshortServerPort;ServerPort=unsignedshort((atoi(part[4])8)+atoi(part[5]));//创建数据连接SocketSocketData=socket(AF_INET,SOCK_STREAM,0);if(SocketData==INVALID_SOCKET){coutCreatesocketerror!endl;returnfalse;}//定义Socket地址和端口sockaddr_inserveraddr2;memset(&serveraddr2,0,sizeof(serveraddr2));serveraddr2.sin_family=AF_INET;serveraddr2.sin_port=htons(ServerPort);serveraddr2.sin_addr.S_un.S_addr=inet_addr(ServerAddr);//向FTP服务器发送Connect请求intnConnect;nConnect=connect(SocketData,(sockaddr*)&serveraddr2,sizeof(serveraddr2));if(nConnect==SOCKET_ERROR){coutendlServerconnecterror!endl;returnfalse;}returntrue;}boolFTPClient::mkdirectory()//FTP服务器发送MKD命令{if(!ishavedetail){cout请输入你要创建的文件夹名:;memset(CmdBuf,0,MAX_SIZE);cin.getline(CmdBuf,MAX_SIZE,'\n');}memset(Command,0,MAX_SIZE);memcpy(Command,MKD,strlen(MKD));memcpy(Command+strlen(MKD),CmdBuf,strlen(CmdBuf));memcpy(Command+strlen(MKD)+strlen(CmdBuf),\r\n,2);//coutCommand:Commandendl;if(!SendCommand())returnfalse;//-----------获得MKD命令的应答信息------------------------if(RecvReply()){if(nReplycode==257)//257:coutReplyMsgendl;else{coutMKDresponseerror!endl;closesocket(SocketControl);returnfalse;}}returntrue;}boolFTPClient::changedir()//FTP服务器发送CWD命令{if(!ishavedetail){cout请输入你要进入的文件夹路径:;memset(CmdBuf,0,MAX_SIZE);cin.getline(CmdBuf,MAX_SIZE,'\n');}memset(Command,0,MAX_SIZE);memcpy(Command,CWD,strlen(CWD));memcpy(Command+strlen(CWD),CmdBuf,strlen(CmdBuf));memcpy(Command+strlen(CWD)+strlen(CmdBuf),\r\n,2);if(!SendCommand())returnfalse;//-----------获得CWD命令的应答信息------------------------if(RecvReply()){coutnReplyCode:nReplycodeendl;if(nReplycode==250)//257:coutReplyMsgendl;else{coutCWDresponseerror!endl;closesocket(SocketControl);returnfalse;}}returntrue;}boolFTPClient::FTPConnection(char*FTPIP,intport)//建立与Socket库绑定{WSADATAWSAData;//WSADAT:WSADATA结构被用来储存调用AfxSocketInit全局函数返回的WindowsSockets初始化信息if(WSAStartup(MAKEWORD(2,2),&WSAData)!=0)//WSAStartup:初始化当前线程通信环境MAKEWORD:合并短整数{coutWSAStartuperror!endl;returnfalse;}//创建控制连接SocketSocketControl=socket(AF_INET,SOCK_STREAM,0);if(SocketControl==INVALID_SOCKET){coutCreatsocketerror!endl;returnfalse;}//定义Socket地址和端口sockaddr_inserveraddr;memset(&serveraddr,0,sizeof(serveraddr));serveraddr.sin_family=AF_INET;//serveraddr.sin_port=htons(port);//端口serveraddr.sin_addr.S_un.S_addr=inet_addr(FTPIP);//地址//向FTP服务器发送Connect请求coutFTPControlconnect...endl;intnConnect=connect(SocketControl,(sockaddr*)&serveraddr,sizeof(serveraddr));if(nConnect==SOCKET_ERROR){coutServerconnecterror!endl;returnfalse;}//获得Connect应答信息if(RecvReply()){if(nReplycode==220)coutReplyMsgendl;else{coutConnetresponseerror!endl;closesocket(SocketControl);returnfalse;}}bConnected=true;returntrue;}boolFTPClient::useuser()//向FTP服务器发送USER命令{coutFTP用户名:;memset(CmdBuf,0,MAX_SIZE);cin.getline(CmdBuf,MAX_SIZE,'\n');memset(Command,0,MAX_SIZE);me
本文标题:FTP客户端C++版
链接地址:https://www.777doc.com/doc-3901950 .html