Skip to content

Commit 232ad57

Browse files
committed
first commit
1 parent fd9f122 commit 232ad57

File tree

12 files changed

+1128
-0
lines changed

12 files changed

+1128
-0
lines changed

Clientmain.cpp

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
#include "FTPClient.h"
2+
#include <iostream>
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
#include <string.h>
6+
#include <string>
7+
#include <sys/socket.h>
8+
#include <arpa/inet.h>
9+
#include <unistd.h>
10+
11+
using std::cin;
12+
using std::string;
13+
14+
int main(int argc,char* argv[])
15+
{
16+
if (argc < 2)
17+
err_sys("usage: ftp <ip> [path]");
18+
in_addr_t serv_ip;
19+
if ((argc >= 2 && (serv_ip = inet_addr(argv[1]) == INADDR_NONE)))
20+
err_sys("invalid ip!");
21+
22+
if (argc >= 3)
23+
strcpy(clientpath,argv[2]);
24+
//初始化套接字信息,获得本地路径及文件名列表
25+
setFileVec(clientfilevec,clientpath);
26+
27+
int connSock = socket(AF_INET,SOCK_STREAM,0);
28+
struct sockaddr_in servaddr;
29+
servaddr.sin_family = AF_INET;
30+
servaddr.sin_addr.s_addr = htonl(serv_ip);
31+
servaddr.sin_port = htons(cmdport);
32+
33+
if(connect(connSock,(struct sockaddr*)&servaddr,sizeof(servaddr)) < 0)
34+
err_sys("connect error!");
35+
36+
char line[MAXLINE];
37+
while (true) //进行用户名和密码的身份验证
38+
{
39+
char name[20];
40+
char passwd[20];
41+
printf("username : ");
42+
scanf("%s",name);
43+
fflush(stdin);
44+
int nwrite = sprintf(line,"USER %s\r\n",name);
45+
write(connSock,line,nwrite);
46+
47+
printf("password : ");
48+
scanf("%s",passwd);
49+
fflush(stdin);
50+
nwrite = sprintf(line,"PASS %s\r\n",passwd);
51+
write(connSock,line,strlen(line));
52+
53+
int nread = read(connSock,line,MAXLINE);
54+
line[nread] = '\0';
55+
if (strstr(line,"WRONG") == line)
56+
printf("wrong username or password!\n");
57+
else
58+
break;
59+
}
60+
61+
while (cin.get() != '\n');
62+
63+
bool flag = true;
64+
while (flag) //处理相关命令
65+
{
66+
int numtodeal = 0;
67+
string filename;
68+
fflush(stdin);
69+
string str;
70+
getline(cin,str);
71+
72+
str.erase(str.begin()+str.find_last_not_of(' ')+1,str.end()); //去除str尾部的空格
73+
int spacepos = str.find(' ',0);
74+
75+
string cmd; //输入的命令名
76+
if (spacepos == string::npos)
77+
cmd = str;
78+
else
79+
cmd = str.substr(0,spacepos);
80+
81+
int i;
82+
for (i = 0; i != ClientCMDNUM; ++i)
83+
if (cmd == ClientCMD[i])
84+
break;
85+
if (i == ClientCMDNUM)
86+
{
87+
printf("invalid command!\n");
88+
continue;
89+
}
90+
char* spaceptr;
91+
if (i < ClientCMDNUM-2) //发送给服务器端的命令处理
92+
{
93+
int nwrite = 0;
94+
if (spacepos == string::npos)
95+
nwrite = sprintf(line,"%s \r\n",ServerCMD[i]);
96+
else
97+
nwrite = sprintf(line,"%s %s\r\n",ServerCMD[i],str.substr(spacepos+1).c_str());
98+
line[nwrite] = '\0';
99+
spaceptr = strchr(line,' ')+1;
100+
write(connSock,line,nwrite);
101+
}
102+
103+
switch(i)
104+
{
105+
case 3:
106+
showFileVec(connSock);
107+
break;
108+
case 4:
109+
downloadFiles(connSock);
110+
break;
111+
case 5:
112+
if (!splitFiles(spaceptr,filename,numtodeal))
113+
printf("some files are not found!\n");
114+
else
115+
uploadFiles(connSock,numtodeal);
116+
break;
117+
case 6:
118+
close(connSock);
119+
flag = false;
120+
break;
121+
case 7:
122+
printLocalFile(clientfilevec);
123+
break;
124+
case 8:
125+
str = str.substr(spacepos+1);
126+
if (!changeLocalDir(str))
127+
printf("invalid directory or permission denied!\n");
128+
break;
129+
130+
default:
131+
printf("can't be here!\n");
132+
break;
133+
}
134+
}
135+
136+
return 0;
137+
}

FTP.cpp

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#include "FTP.h"
2+
#include <string>
3+
#include <vector>
4+
#include <string.h>
5+
#include <stdio.h>
6+
#include <stdlib.h>
7+
#include <sys/stat.h>
8+
#include <sys/types.h>
9+
#include <dirent.h>
10+
#include <unistd.h>
11+
#include <fcntl.h>
12+
using std::string;
13+
using std::vector;
14+
const int cmdport = 5600;
15+
16+
const char rightmsg[10] = "OK\r\n";
17+
const char wrongmsg[10] = "WRONG\r\n";
18+
const char* ServerCMD[ServerCMDNUM] = {"USER","PASS","PASV","SHOW","GET","PUT","QUIT"}; //客户端发送给服务器端的命令类型
19+
20+
21+
void setFileVec(vector<string>& filevec,char* path) //将路径切换到path,同时将新路径下的文件填入filevec
22+
{
23+
DIR* dir;
24+
if (strcmp(path,".") != 0)
25+
{
26+
if ((dir = opendir(path)) == NULL)
27+
err_sys("invalid pathname or permission denied!");
28+
else
29+
chdir(path);
30+
}
31+
else
32+
{
33+
char* pwd = getcwd(path,MAXLINE);
34+
if (pwd == NULL)
35+
err_sys("fail to get current directory!");
36+
else
37+
{
38+
dir = opendir(path);
39+
chdir(path);
40+
}
41+
}
42+
struct dirent* item;
43+
struct stat statbuf;
44+
while ((item = readdir(dir)) != NULL)
45+
{
46+
lstat(item->d_name,&statbuf);
47+
if (S_ISDIR(statbuf.st_mode))
48+
{
49+
if (strcmp(".",item->d_name) == 0 ||
50+
strcmp("..",item->d_name) == 0)
51+
continue;
52+
else
53+
filevec.push_back(string(item->d_name));
54+
}
55+
else
56+
filevec.push_back(item->d_name);
57+
}
58+
}
59+
60+
unsigned long getFileSize(const string& file) //获取某文件的大小(字节数)
61+
{
62+
unsigned long filesize;
63+
struct stat statbuff;
64+
lstat(file.c_str(),&statbuff);
65+
filesize = statbuff.st_size;
66+
67+
return filesize;
68+
}
69+
70+
void printLocalFile(const vector<string>& filevec) //输出当前路径下的文件名列表
71+
{
72+
int len = filevec.size();
73+
for (int i = 0;i != len; ++i)
74+
printf("%s\n",filevec[i].c_str());
75+
}
76+
77+
void err_sys(const char* msg) //打印错误信息并终止程序
78+
{
79+
printf("%s\n",msg);
80+
exit(1);
81+
}

FTP.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#ifndef _FTP_H
2+
#define _FTP_H
3+
4+
#include <string>
5+
#include <vector>
6+
7+
#define MAXLINE 50
8+
#define MAXSIZE 1000
9+
#define ServerCMDNUM 7
10+
extern const int cmdport; //服务器端的命令套接字端口
11+
12+
extern const char rightmsg[10];
13+
extern const char wrongmsg[10];
14+
extern const char* ServerCMD[];
15+
16+
void err_sys(const char*);
17+
unsigned long getFileSize(const std::string&);
18+
void setFileVec(std::vector<std::string>&,char* path);
19+
void printLocalFile(const std::vector<std::string>&);
20+
21+
#endif

0 commit comments

Comments
 (0)