-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.cpp
More file actions
285 lines (264 loc) · 8.28 KB
/
client.cpp
File metadata and controls
285 lines (264 loc) · 8.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#include <WinSock2.h>
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>
#include <direct.h>
#include <io.h>
#pragma warning(disable:4996)
#pragma comment(lib,"ws2_32.lib")
using namespace std;
WSADATA wsa_data;
int ret = WSAStartup(MAKEWORD(2, 2), &wsa_data);
SOCKET sock = socket(AF_INET, SOCK_DGRAM, 0);
sockaddr_in addrServer;
int len = sizeof(SOCKADDR);
//获取当前目录下的文件
vector<string> getFiles(string cate_dir)
{
vector<string> files;
_finddata_t file;
long lf;
if ((lf = _findfirst(cate_dir.c_str(), &file)) == -1) {
cout << cate_dir << " not found!!!" << endl;
}
else {
while (_findnext(lf, &file) == 0) {
if (strcmp(file.name, ".") == 0 || strcmp(file.name, "..") == 0)
continue;
files.push_back(file.name);
}
}
_findclose(lf);
sort(files.begin(), files.end());
return files;
}
//获取当前目录(弃用)
vector<string> getCurrentDirFiles() {
char current_address[1024];
memset(current_address, 0, 1024);
getcwd(current_address, 1024);
strcat(current_address, "\\*");
//string current = (string)current_address;
vector<string> files = getFiles((string)current_address);
return files;
}
//获取当前目录
string getCurrentDir() {
char current_address[1024];
memset(current_address, 0, 1024);
getcwd(current_address, 1024);
string current = (string)current_address;
return current;
}
//输出文件列表
void giveCurrentDirFiles(vector<string> &files) {
cout << endl;
for (vector<string>::iterator i = files.begin(); i < files.end(); i++) {
cout << *i << endl;
}
}
//从服务器获取文件
void getFileFromServer(string &filename) {
ofstream wlog("ClientLog.txt", ios::app);
if (filename.find(" ") == string::npos) {
cout << ">Illegal input format : " << filename << endl;
return;
}
else
{
filename = filename.substr(filename.find(" ") + 1);
}
FILE *fp = fopen(filename.c_str(), "wb");
if (!fp) {
cout << ">Failed to create such file :" << filename << endl;
return;
}
//sendto(sock, filename.c_str() , strlen(filename.c_str()), 0, (SOCKADDR*)&addrServer, sizeof(SOCKADDR));
char buffer[1024];
memset(buffer, 0x0, 1024);
int nCount;
int seq = 0;
while ((nCount = recvfrom(sock, buffer, 1024, 0, (SOCKADDR*)&addrServer, &len)) > 0) {
if (strcmp(buffer, "FileOpenFailed") == 0) {
cout << ">Server dir doesn't exist such file : " << filename << endl;
wlog << "Server dir doesn't exist such file : " << filename << endl;
fclose(fp);
remove(filename.c_str());
return;
}
if (strcmp(buffer, "EndTrans") == 0)
break;
if (strncmp(buffer, "SEQ", 3) == 0) {
continue;
}
fwrite(buffer, nCount, 1, fp);
memset(buffer, 0x0, 1024);
sendto(sock, "ACK", strlen("ACK"), 0, (SOCKADDR*)&addrServer, sizeof(SOCKADDR));
seq++;
}
fclose(fp);
if (FALSE) {
sendto(sock, "Resend", strlen("Resend"), 0, (SOCKADDR*)&addrServer, sizeof(SOCKADDR));
getFileFromServer(filename);
}
else
{
sendto(sock, "Success", strlen("Success"), 0, (SOCKADDR*)&addrServer, sizeof(SOCKADDR));
cout << ">Successfully received " << filename << endl;
wlog << "Successfully received " << filename << endl;
}
}
//获取服务器当前目录文件列表
void getServerDirFiles() {
ofstream wlog("ClientLog.txt", ios::app);
char buffer[1024];
memset(buffer, 0x0, 1024);
int nCount;
cout << endl;
while ((nCount = recvfrom(sock, buffer, 1024, 0, (SOCKADDR*)&addrServer, &len)) > 0) {
if (strcmp(buffer, "EndOfFileList") == 0)
break;
cout << buffer << endl;
memset(buffer, 0x0, 1024);
}
wlog << "Listed server current dir files." << endl;
}
//上传文件
void sendFileToServer(string &filename) {
ofstream wlog("ClientLog.txt", ios::app);
if (filename.find(" ") == string::npos) {
cout << ">Illegal input format : " << filename << endl;
return;
}
else
{
filename = filename.substr(filename.find(" ") + 1);
}
FILE *fp = fopen(filename.c_str(), "rb");
//sendto(sock, filename.c_str(), strlen(filename.c_str()), 0, (SOCKADDR*)&addrServer, sizeof(SOCKADDR));
char buffer[1024];
memset(buffer, 0x0, 1024);
recvfrom(sock, buffer, 1024, 0, (SOCKADDR*)&addrServer, &len);
if (strcmp(buffer, "FileAlreadyExist") == 0) {
cout << ">Server current dir already exist a same name file. Upload file failed." << endl;
wlog << "Server current dir already exist a same name file. Upload file failed." << endl;
return;
}
if (!fp) {
cout << ">No Such File :" << filename << endl;
wlog << "Client has no such file :" << filename << endl;
sendto(sock, "NoSuchFile", strlen("NoSuchFile"), 0, (SOCKADDR*)&addrServer, sizeof(SOCKADDR));
return;
}
memset(buffer, 0x0, 1024);
int nCount;
int seq = 0;
while ((nCount = fread(buffer, 1, 1024, fp)) > 0) {
resend:
char num[1024] = { 0 };
char seq_buff[1024] = { 0 };
strcat(seq_buff, "SEQ");
strcat(seq_buff, itoa(seq, num, 10));
sendto(sock, seq_buff, strlen(seq_buff), 0, (SOCKADDR*)&addrServer, sizeof(SOCKADDR));
sendto(sock, buffer, nCount, 0, (SOCKADDR*)&addrServer, sizeof(SOCKADDR));
memset(buffer, 0x0, 1024);
recvfrom(sock, buffer, 1024, 0, (SOCKADDR*)&addrServer, &len);
if (strcmp(buffer, "ACK") == 0) {
seq++;
continue;
}
else
goto resend;
}
sendto(sock, "EndTrans", strlen("EndTrans"), 0, (SOCKADDR*)&addrServer, sizeof(SOCKADDR));
memset(buffer, 0x0, 1024);
recvfrom(sock, buffer, 1024, 0, (SOCKADDR*)&addrServer, &len);
fclose(fp);
if (strcmp(buffer,"Success")!=0) {
sendFileToServer(filename);
}
else
{
cout << ">Successfully uploaded " << filename << endl;
wlog << "Successfully uploaded " << filename << endl;
}
}
//改变服务器当前目录
void changeServerDir(string &input) {
ofstream wlog("ClientLog.txt", ios::app);
//sendto(sock, input.c_str(), strlen(input.c_str()), 0, (SOCKADDR*)&addrServer, sizeof(SOCKADDR));
char buffer[1024];
memset(buffer, 0x0, 1024);
recvfrom(sock, buffer, 1024, 0, (SOCKADDR*)&addrServer, &len);
if (strcmp(buffer, "IllegalInput") == 0) {
cout << ">Illegal input." << endl;
return;
}
else if (strcmp(buffer, "IllegalDir") == 0) {
cout << ">Illegal dir." << endl;
return;
}
else {
cout << ">Successfully changed dir" << endl;
wlog << "Successfully changed dir" << endl;
}
}
int main()
{
addrServer.sin_family = AF_INET;
addrServer.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
addrServer.sin_port = htons(8001);
//sendto(sock, "HELLO", strlen("HELLO"), 0, (SOCKADDR*)&addrServer, len);
while (1) {
char buff[1024];
cout << "|-----------------Client----------------|" << endl;
cout << "|ls --------------list server dir files|" << endl;
cout << "|cls --------------list client dir files|" << endl;
cout << "|get FILENAME ----get a file from server|" << endl;
cout << "|send FILENAME ----send a file to server|" << endl;
cout << "|cd DIR -----------enter a server folder|" << endl;
cout << "|cd .. ------------go back to parent dir|" << endl;
cout << "|---------------------------------------|" << endl;
cout << ">";
memset(buff, 0x0, 1024);
cin.getline(buff, 1024);
int sendlen = sendto(sock, buff, 1024, 0, (SOCKADDR*)&addrServer, len);
if (sendlen < 0)
{
int error = WSAGetLastError();
printf("error = %d\n", error);
}
char recvBuf[1024];
if (strncmp(buff, "get",3) == 0) {
string filename = buff;
getFileFromServer(filename);
}
else if (strncmp(buff, "send",4) == 0) {
string filename = buff;
sendFileToServer(filename);
}
else if (strcmp(buff, "ls") == 0) {
getServerDirFiles();
}
else if (strcmp(buff, "cls") == 0) {
giveCurrentDirFiles(getCurrentDirFiles());
}
else if (strncmp(buff, "cd",2) == 0) {
string filename = buff;
changeServerDir(filename);
}
else
{
cout << "Illegal Input..." << endl;
}
memset(buff, 0x0, 1024);
cout << endl;
}
closesocket(sock);
WSACleanup();
system("pause");
return 0;
}