FTP客戶端C++版_第1頁
FTP客戶端C++版_第2頁
FTP客戶端C++版_第3頁
FTP客戶端C++版_第4頁
FTP客戶端C++版_第5頁
已閱讀5頁,還剩12頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、(1)頭文件:FTPClient.h#include<conio.h>#define MAX_SIZE 4096#include<iostream>#include<winsock2.h>#include<string>#include<fstream>#pragma comment(lib,"ws2_32.lib")using namespace std;class FTPClientpublic:bool RecvReply(); /控制連接接收bool SendCommand(); /向FTP服務器發(fā)送命令b

2、ool DataConnect(char* ServerAddr); /向FTP服務器發(fā)送命令bool mkdirectory(); /FTP服務器發(fā)送MKD命令bool changedir(); /FTP服務器發(fā)送CWD命令bool FTPConnection(char* FTPIP,int port); /建立與Socket庫綁定bool useuser(); /向FTP服務器發(fā)送USER命令bool usepass(); /向FTP服務器發(fā)送PASS命令void subcommend(string& filepath,string& filename); /輸入和轉換IP

3、地址void storfile(char* FTPIP); /上傳文件void retrfile(char* FTPIP); /下載文件void listftp(char* FTPIP); /列出FTP服務器目錄void deletefile(); /刪除文件void quitftp(); /退出客戶端void help(); /幫助void about(); /aboutbool ishavedetail;char CmdBufMAX_SIZE;char CommandMAX_SIZE;char ReplyMsgMAX_SIZE;private:int nReplycode;bool bCo

4、nnected;bool buser;SOCKET SocketControl;SOCKET SocketData;(2)源文件:FTPClient.cpp#include<conio.h>#define MAX_SIZE 4096#include<iostream>#include<winsock2.h>#include<string>#include<fstream>#include"FTPClient.h"using namespace std;#pragma comment(lib,"ws2_32

5、.lib")bool FTPClient:RecvReply() /控制連接接收int nRecv;memset(ReplyMsg, 0, MAX_SIZE);nRecv = recv(SocketControl, ReplyMsg, MAX_SIZE, 0);if (nRecv = SOCKET_ERROR)cout << "Socket receive error!" << endl;closesocket(SocketControl);return false;/獲取相應信息與響應碼if (nRecv > 4)char *Rep

6、lyCodes = new char3;memset(ReplyCodes, 0, 3);memcpy(ReplyCodes, ReplyMsg, 3);nReplycode = atoi(ReplyCodes);return true;bool FTPClient:SendCommand() /向FTP服務器發(fā)送命令/控制連接發(fā)送數(shù)據int nSend;nSend = send(SocketControl, Command, strlen(Command), 0);if (nSend = SOCKET_ERROR)cout << "Socket send error!&

7、quot; << endl;return false;return true;bool FTPClient:DataConnect(char* ServerAddr)/向FTP服務器發(fā)送PASV命令memset(Command, 0, MAX_SIZE);memcpy(Command, "PASV", strlen("PASV");memcpy(Command + strlen("PASV"), "rn", 2);if (!SendCommand()return false;/獲取PASV命令的應答信息

8、if (RecvReply()if (nReplycode != 227)cout << "PASV response error!" << endl;closesocket(SocketControl);return false;/分離PASV命令應答信息char* part6;if (strtok(ReplyMsg, "(")for (int i = 0; i < 5; i+)parti = strtok(NULL, ",");if (!parti)return false;part5 = strto

9、k(NULL, ")");if (!part5)return false;elsereturn false;/獲取FTP服務器數(shù)據端口、unsigned short ServerPort;ServerPort = unsigned short(atoi(part4) << 8) + atoi(part5);/創(chuàng)建數(shù)據連接SocketSocketData = socket(AF_INET, SOCK_STREAM, 0);if (SocketData = INVALID_SOCKET)cout << "Create socket error!

10、" << endl;return false;/定義Socket地址和端口sockaddr_in serveraddr2;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服務器發(fā)送Connect請求int nConnect;nConnect = connec

11、t(SocketData, (sockaddr*)&serveraddr2, sizeof(serveraddr2);if (nConnect = SOCKET_ERROR)cout << endl << "Server connect error!" << endl;return false;return true;bool FTPClient:mkdirectory() /FTP服務器發(fā)送MKD命令if (!ishavedetail)cout << "請輸入你要創(chuàng)建的文件夾名:"memset(C

12、mdBuf, 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), "rn", 2);/cou

13、t<<"Command:"<<Command<<endl;if (!SendCommand()return false;/-獲得MKD命令的應答信息-if (RecvReply()if (nReplycode = 257)/257:cout << ReplyMsg << endl;elsecout << "MKD response error!" << endl;closesocket(SocketControl);return false;return true;boo

14、l FTPClient:changedir() /FTP服務器發(fā)送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 "), Cm

15、dBuf, strlen(CmdBuf);memcpy(Command + strlen("CWD ") + strlen(CmdBuf), "rn", 2);if (!SendCommand()return false;/-獲得CWD命令的應答信息-if (RecvReply()cout << "nReplyCode:" << nReplycode << endl;if (nReplycode = 250)/257:cout << ReplyMsg << endl;else

16、cout << "CWD response error!" << endl;closesocket(SocketControl);return false;return true;bool FTPClient:FTPConnection(char* FTPIP, int port) /建立與Socket庫綁定WSADATA WSAData; /WSADAT:WSADATA結構被用來儲存調用AfxSocketInit全局函數(shù)返回的Windows Sockets初始化信息if (WSAStartup(MAKEWORD(2, 2), &WSADat

17、a) != 0) /WSAStartup:初始化當前線程通信環(huán)境 MAKEWORD:合并短整數(shù)cout << "WSAStartup error!" << endl;return false;/創(chuàng)建控制連接SocketSocketControl = socket(AF_INET, SOCK_STREAM, 0);if (SocketControl = INVALID_SOCKET)cout << "Creat socket error!" << endl;return false;/定義Socket地址和端

18、口sockaddr_in serveraddr;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服務器發(fā)送Connect請求cout << "FTP>Control connect." << endl;int nConnect = connect(

19、SocketControl, (sockaddr*)&serveraddr, sizeof(serveraddr);if (nConnect = SOCKET_ERROR)cout << "Server connect error!" << endl;return false;/獲得Connect應答信息if (RecvReply()if (nReplycode = 220)cout << ReplyMsg << endl;elsecout << "Connet response error!&q

20、uot; << endl;closesocket(SocketControl);return false;bConnected = true;return true;bool FTPClient:useuser() /向FTP服務器發(fā)送USER命令cout << "FTP>用戶名:"memset(CmdBuf, 0, MAX_SIZE);cin.getline(CmdBuf, MAX_SIZE, 'n');memset(Command, 0, MAX_SIZE);memcpy(Command, "USER "

21、, strlen("USER ");memcpy(Command + strlen("USER "), CmdBuf, strlen(CmdBuf);memcpy(Command + strlen("USER ") + strlen(CmdBuf), "rn", 2);cout << "Command:" << Command << endl;if (!SendCommand()return false;/獲得USER命令的應答信息if (RecvReply(

22、)if (nReplycode = 331)/230:User logged in,proceed;/331:User Name okay,need password;cout << ReplyMsg << endl;elsecout << "USER response error!" << endl;closesocket(SocketControl);return false;buser = true;return true;bool FTPClient:usepass() /向FTP服務器發(fā)送PASS命令if (buse

23、r)buser = false;cout << "FTP>密 碼:"memset(CmdBuf, 0, MAX_SIZE);cout.flush();for (int i = 0; i < MAX_SIZE; i+)CmdBufi = getch();if (CmdBufi = 'r')CmdBufi = '0'break;elsecout << '*'cout << endl;memset(Command, 0, MAX_SIZE);memcpy(Command, "

24、PASS ", strlen("PASS ");memcpy(Command + strlen("PASS "), CmdBuf, strlen(CmdBuf);memcpy(Command + strlen("PASS ") + strlen(CmdBuf), "rn", 2);if (!SendCommand()return false;/獲得PASS命令的應答信息if (RecvReply()if (nReplycode = 230)/230:User logged in,proceed;cout

25、<< ReplyMsg << endl;elsecout << "PASS respanse error!" << endl;return false;return true;void FTPClient:storfile(char* FTPIP)if (!ishavedetail)cout << "請輸入上傳文件名:"memset(CmdBuf, 0, MAX_SIZE);cin.getline(CmdBuf, MAX_SIZE, 'n');ifstream f2;f2.ope

26、n(CmdBuf, ios:in);if (!f2)cout << "Cannot open file!" << endl;return;string strPath(CmdBuf);string filepath, filename;int nPos = strPath.rfind('');if (-1 != nPos)filename = strPath.substr(nPos + 1, strPath.length() - nPos - 1);filepath = strPath.substr(0, nPos);cout <

27、;< "filename:" << filename << endl;cout << "filepath:" << filepath << endl;memset(CmdBuf, 0, MAX_SIZE);memcpy(CmdBuf, filename.data(), strlen(filename.data();char FtpStorMAX_SIZE;memset(FtpStor, 0, MAX_SIZE);memcpy(FtpStor, FTPIP, strlen(FTPIP);if

28、 (!DataConnect(FtpStor)return;memset(Command, 0, MAX_SIZE);memcpy(Command, "STOR ", strlen("STOR ");memcpy(Command + strlen("STOR "), CmdBuf, strlen(CmdBuf);memcpy(Command + strlen("STOR ") + strlen(CmdBuf), "rn", 2);if (!SendCommand()return;if (Recv

29、Reply()if (nReplycode = 125 | nReplycode = 150 | nReplycode = 226)cout << ReplyMsg;else cout << "STOR respanse error!" << endl;closesocket(SocketControl);return;char ListBuf2MAX_SIZE;while (true)memset(ListBuf2, 0, MAX_SIZE);f2.read(ListBuf2, MAX_SIZE);int nStor = send(So

30、cketData, ListBuf2, MAX_SIZE, 0);if (nStor = SOCKET_ERROR)cout << endl << "Socket send error!" << endl;closesocket(SocketData);return;break;f2.close();closesocket(SocketData);if (RecvReply()if (nReplycode = 226)cout << ReplyMsg;elsecout << "STOR response

31、error!" << endl;closesocket(SocketControl);return;void FTPClient:retrfile(char* FTPIP)if (!ishavedetail)cout << "請輸入下載文件名:" << endl;memset(CmdBuf, 0, MAX_SIZE);cin.getline(CmdBuf, MAX_SIZE, 'n');char FtpRetrMAX_SIZE;memset(FtpRetr, 0, MAX_SIZE);memcpy(FtpRetr

32、, FTPIP, strlen(FTPIP);if (!DataConnect(FtpRetr)return;memset(Command, 0, MAX_SIZE);memcpy(Command, "RETR ", strlen("RETR ");memcpy(Command + strlen("RETR "), CmdBuf, strlen(CmdBuf);memcpy(Command + strlen("RETR ") + strlen(CmdBuf), "rn", 2);cout <

33、;< "請輸入保存的文件名:"memset(CmdBuf, 0, MAX_SIZE);cin.getline(CmdBuf, MAX_SIZE, 'n');if (!SendCommand()return;if (RecvReply()cout << "nReplyCode:" << nReplycode;if (nReplycode = 125 | nReplycode = 150 | nReplycode = 226)cout << ReplyMsg;else cout << &q

34、uot;RETR respanse error!" << endl;closesocket(SocketControl);return;ofstream f1(CmdBuf);if (!f1)cout << "file can not open" << endl;return;int nRetr;char ListBuf1MAX_SIZE;while (true)memset(ListBuf1, 0, MAX_SIZE);nRetr = recv(SocketData, ListBuf1, MAX_SIZE, 0);f1.wri

35、te(ListBuf1, MAX_SIZE);if (nRetr = SOCKET_ERROR)cout << endl << "Socket receive error!" << endl;closesocket(SocketData);return;if (nRetr <= 0)break;f1.close();closesocket(SocketData);if (RecvReply()if (nReplycode = 226)cout << ReplyMsg;elsecout << "RET

36、R response error!" << endl;closesocket(SocketControl);return;void FTPClient:listftp(char* FTPIP) /列出FTP服務器目錄/FTP服務器發(fā)送LIST命令char FtpServerMAX_SIZE;memset(FtpServer, 0, MAX_SIZE);memcpy(FtpServer, FTPIP, strlen(FTPIP);if (!DataConnect(FtpServer)/數(shù)據連接return;memset(Command, 0, MAX_SIZE);memcp

37、y(Command, "LIST", strlen("LIST");memcpy(Command + strlen("LIST"), "rn", 2);if (!SendCommand()return;/-獲得LIST命令的應答信息-if (RecvReply()/125:Data connection alreadly open;transfer staring./150:File status okay, about to data connection;/226: closing data connectio

38、n;/150:Opening ASCII mode data connection for /bin/ls;if (nReplycode = 125 | nReplycode = 150 | nReplycode = 226)cout << ReplyMsg;else cout << "LIST respanse error!" << endl;closesocket(SocketControl);return;/獲得LIST命令的目錄信息int nRecv;char ListBufMAX_SIZE;while (true)memset(

39、ListBuf, 0, MAX_SIZE);nRecv = recv(SocketData, ListBuf, MAX_SIZE, 0);if (nRecv = SOCKET_ERROR)cout << endl << "Socket receive error!" << endl;closesocket(SocketData);return;if (nRecv <= 0)break;cout << ListBuf;closesocket(SocketData);/-獲得LIST命令的應答信息-if (RecvReply

40、()if (nReplycode = 226)/226: closing data connection;transfer complete;cout << ReplyMsg;elsecout << "LIST response error!" << endl;closesocket(SocketControl);return;void FTPClient:deletefile() /if (!ishavedetail)cout << "請輸入你要刪除的文件名:"memset(CmdBuf, 0, MAX_

41、SIZE);cin.getline(CmdBuf, MAX_SIZE, 'n');memset(Command, 0, MAX_SIZE);memcpy(Command, "DELE ", strlen("DELE ");memcpy(Command + strlen("DELE "), CmdBuf, strlen(CmdBuf);memcpy(Command + strlen("DELE ") + strlen(CmdBuf), "rn", 2);if (!SendComma

42、nd()return;/-獲得DELE命令的應答信息-if (RecvReply()if (nReplycode = 250)/230:User logged in,proceed;/331:User Name okay,need password;cout << ReplyMsg << endl;elsecout << "DELE response error!" << endl;closesocket(SocketControl);return;void FTPClient:quitftp()/-向FTP服務器發(fā)送QUIT

43、命令-memset(Command, 0, MAX_SIZE);memcpy(Command, "QUIT", strlen("QUIT");memcpy(Command + strlen("QUIT"), "rn", 2);if (!SendCommand()return;/-獲得QUIT命令的應答信息-if (RecvReply()if (nReplycode = 221)/221:goodbay,closing session;cout << ReplyMsg;bConnected = false

44、;/buser=false;closesocket(SocketControl);return;elsecout << endl << "QUIT response error!" << endl;closesocket(SocketControl);return;WSACleanup();void FTPClient:subcommend(string& filepath, string& filename) /輸入和轉換IP地址memset(CmdBuf, 0, MAX_SIZE);cin.getline(CmdBuf

45、, MAX_SIZE, 'n');string strPath(CmdBuf);int nPos = strPath.rfind(' ');if (-1 != nPos)filename = strPath.substr(nPos + 1, strPath.length() - nPos - 1);filepath = strPath.substr(0, nPos);elsefilepath = CmdBuf;void FTPClient:about()cout << "-關于FTP客戶端控制臺版-" << endl;

46、cout << endl;cout << "程序名稱:FTP客戶端控制臺版" << endl;cout << "程序語言:C+" << endl;cout << "小組成員:劉銳翊 楊潤 牟林 李良俊 陳秋燕 毛虹" << endl;cout << "編譯日期:2015年12月30日" << endl;cout << endl;cout << "-" <<

47、; endl;cout << endl;void FTPClient:help()cout << "-FTP客戶端控制臺版幫助-" << endl;cout << endl;cout << "ls 列出所有目錄與文件" << endl;cout << "stor 上傳文件" << endl;cout << "retr 下載文件" << endl;cout << "dele 刪除文件" << endl;cout << "mkd 創(chuàng)建文件" << endl;cout << "cwd 進入指定目錄" << endl;cout << "help 幫助" << endl;cout << "about

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論