Skip to content

Commit 03af018

Browse files
committed
second commit
1 parent 4cd7a26 commit 03af018

File tree

9 files changed

+44
-23
lines changed

9 files changed

+44
-23
lines changed

Clientmain.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ int main(int argc,char* argv[])
4141
printf("username : ");
4242
scanf("%s",name);
4343
fflush(stdin);
44-
int nwrite = sprintf(line,"USER %s\r\n",name);
44+
int nwrite = sprintf(line,"%s %s\r\n",ClientCMD[User],name);
4545
write(connSock,line,nwrite);
4646

4747
printf("password : ");
4848
scanf("%s",passwd);
4949
fflush(stdin);
50-
nwrite = sprintf(line,"PASS %s\r\n",passwd);
50+
nwrite = sprintf(line,"%s %s\r\n",ClientCMD[Pass],passwd);
5151
write(connSock,line,strlen(line));
5252

5353
int nread = read(connSock,line,MAXLINE);
@@ -102,26 +102,26 @@ int main(int argc,char* argv[])
102102

103103
switch(i)
104104
{
105-
case 3:
105+
case Ls:
106106
showFileVec(connSock);
107107
break;
108-
case 4:
108+
case Get:
109109
downloadFiles(connSock);
110110
break;
111-
case 5:
111+
case Put:
112112
if (!splitFiles(spaceptr,filename,numtodeal))
113113
printf("some files are not found!\n");
114114
else
115115
uploadFiles(connSock,numtodeal);
116116
break;
117-
case 6:
117+
case Quit:
118118
close(connSock);
119119
flag = false;
120120
break;
121-
case 7:
121+
case Lcd:
122122
printLocalFile(clientfilevec);
123123
break;
124-
case 8:
124+
case Cd:
125125
str = str.substr(spacepos+1);
126126
if (!changeLocalDir(str))
127127
printf("invalid directory or permission denied!\n");

FTP.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ extern const int cmdport; //服务器端的命令套接字端口
1212
extern const char rightmsg[10];
1313
extern const char wrongmsg[10];
1414
extern const char* ServerCMD[];
15-
15+
enum {USER = 0,PASS,PASV,SHOW,GET,PUT,QUIT};
1616
void err_sys(const char*);
1717
unsigned long getFileSize(const std::string&);
1818
void setFileVec(std::vector<std::string>&,char* path);

FTPClient.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
using std::string;
1515
using std::vector;
16+
1617
const char* ClientCMD[ClientCMDNUM] = {"USER","PASS","PASV","ls","get","put","bye","lcd","cd"};
1718
char clientpath[MAXLINE] = ".";
1819
vector<string> clientfilevec;

FTPClient.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ extern const char* ClientCMD[ClientCMDNUM]; //客户端的命令列表
99
extern char clientpath[MAXLINE]; //客户端的当前路径
1010
extern std::vector<std::string> clientfilevec; //客户端当前路径下的文件名列表
1111

12+
enum {User = 0,Pass,Pasv,Ls,Get,Put,Quit,Lcd,Cd};
13+
1214
void showFileVec(int);
1315
int passPasvCmd(int,char*);
1416
void downloadFiles(int);

FTPServ.cpp

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ void communicateWithClient(int connfd) //处理客户端命令端口套
8787
}
8888

8989
line[nread] = '\0';
90-
//printf("%s",line);
9190

9291
char* spacepos = strchr(line,' ');
9392
if (spacepos == NULL)
@@ -110,10 +109,10 @@ void communicateWithClient(int connfd) //处理客户端命令端口套
110109
char* pos = strstr(spacepos+1,"\r\n");
111110
switch (i)
112111
{
113-
case 0:
112+
case USER:
114113
legaluser = strncmp(spacepos+1,USERNAME,pos-spacepos-1);
115114
break;
116-
case 1:
115+
case PASS:
117116
legalpasswd = strncmp(spacepos+1,PASSWD,pos-spacepos-1);
118117
if (legaluser == 0 && legalpasswd == 0)
119118
{
@@ -123,23 +122,23 @@ void communicateWithClient(int connfd) //处理客户端命令端口套
123122
else
124123
write(connfd,wrongmsg,strlen(wrongmsg));
125124
break;
126-
case 2:
125+
case PASV:
127126
char dataport[10];
128127
initDataSock(&dataSock,&dataCltAddr);
129128
// printf("dataport : %d\n",dataCltAddr.sin_port);
130129
nwrite = sprintf(dataport,"%d \r\n",dataCltAddr.sin_port);
131130
dataport[nwrite] = '\0';
132131
write(connfd,dataport,strlen(dataport));
133-
132+
134133
switch (passtype)
135134
{
136-
case 3:
135+
case SHOW:
137136
passFileVec(dataSock);
138137
break;
139-
case 4:
138+
case GET:
140139
sendFiles(dataSock,filename,numtodeal);
141140
break;
142-
case 5:
141+
case PUT:
143142
recvFiles(dataSock,filename,numtodeal);
144143
break;
145144
default:
@@ -148,7 +147,7 @@ void communicateWithClient(int connfd) //处理客户端命令端口套
148147
}
149148
close(dataSock);
150149
break;
151-
case 3:
150+
case SHOW:
152151
if (pos != spacepos+1)
153152
write(connfd,wrongmsg,strlen(wrongmsg));
154153
else
@@ -157,7 +156,7 @@ void communicateWithClient(int connfd) //处理客户端命令端口套
157156
passtype = i;
158157
}
159158
break;
160-
case 4:
159+
case GET:
161160
filename.clear();
162161
numtodeal = splitFiles(spacepos+1,filename,1);
163162
printf("numtodeal %d\n",numtodeal);
@@ -170,7 +169,7 @@ void communicateWithClient(int connfd) //处理客户端命令端口套
170169
passtype = i;
171170
}
172171
break;
173-
case 5:
172+
case PUT:
174173
filename.clear();
175174
numtodeal = splitFiles(spacepos+1,filename,0);
176175
printf("numtodeal %d\n",numtodeal);
@@ -182,7 +181,7 @@ void communicateWithClient(int connfd) //处理客户端命令端口套
182181
passtype = i;
183182
}
184183
break;
185-
case 6:
184+
case QUIT:
186185
close(connfd);
187186
flag = false;
188187
printf("client %d logged out\n",connfd);
@@ -227,7 +226,7 @@ void passFileVec(int datafd) //传输本地文件名列表
227226
strcat(data,string(servfilevec[i]+'\n').c_str());
228227
datalen += servfilevec[i].size()+1;
229228
}
230-
pthread_mutex_lock(&filevec_mutex);
229+
pthread_mutex_unlock(&filevec_mutex);
231230
int nwrite = sprintf(line,"%d \r\n",datalen);
232231
line[nwrite] = '\0';
233232

Makefile

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
all: ftpserv ftpclient clean
2+
ftpserv: Servmain.o FTPServ.o FTP.o
3+
g++ -o ftpserv Servmain.o FTPServ.o FTP.o -lpthread
4+
Servmain.o: Servmain.cpp FTPServ.h
5+
g++ -c -D_REENTRANT Servmain.cpp
6+
FTPServ.o: FTPServ.cpp FTPServ.h
7+
g++ -c -D_REENTRANT FTPServ.cpp
8+
FTP.o: FTP.cpp FTP.h
9+
g++ -c FTP.cpp
10+
11+
ftpclient: Clientmain.o FTPClient.o FTP.o
12+
g++ -o ftpclient Clientmain.o FTPClient.o FTP.o
13+
Clientmain.o: Clientmain.cpp FTPClient.h
14+
g++ -c Clientmain.cpp
15+
FTPClient.o: FTPClient.cpp FTP.h
16+
g++ -c FTPClient.cpp
17+
clean:
18+
-rm -f *.o
19+

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# lightftp
2-
本程序是一个模拟ftp的简单的文件传输程序,ftpserv和ftpclient分别是服务器端和客户端的可执行文件。
2+
本程序是在Linux环境下开发的一个模拟ftp的简单的文件传输程序,ftpserv和ftpclient分别是服务器端和客户端的可执行文件。
33
服务器端利用多线程的方式分别对连接的每个客户端进行服务,采取预先启动一定数量的线程的方式,提高了效率,同时主程序采用I/O复用的方式监测来自监听套接字和标准输入的数据;
44
客户端采用单线程的方式进行文件的上传和下载,支持的命令包括:列出服务器端当前路径的文件列表, 本地当前路径列表,更改本地路径,
55
下载、上传文件,关闭客户端等操作;服务器端支持的命令目前包括,列出本地路径下的文件,关闭服务器等操作。

ftpclient

0 Bytes
Binary file not shown.

ftpserv

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)