-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathclient.cpp
372 lines (349 loc) Β· 7.54 KB
/
client.cpp
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
#include<iostream>
#include<unistd.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<assert.h>
#include<stdio.h>
#include<errno.h>
#include<signal.h>
#include<sys/wait.h>
#include<sys/stat.h>
#include<md5c.h>
using namespace std;
class userinfo
{
private:
string id;
string username;
public:
userinfo(char *m_id,char *name)
{
id(m_id);
username(name);
}
};
//REGISTERED=0,LOGIN,UPLOAD_F,UPLOAD_S,DOWNLOAD,ADDFRIEND,SHAREDTOFRIEND,SNOPSHOT
class start_process
{
void RecvFriendAndFileList();
void deal_registered();
void RecvRetId();
void error_login();
void deal_login();
void start();
void file_start();
void deal_upload();
public:
void run()
{
start();
file_start();
}
private:
userinfo m_user;
int sockfd;
};
char *mystrtok(char *tmp,char *s)
{
char *p;
char *temp=NULL;
p = strtok(tmp, s);
while (p != NULL)
{
temp = p;
p = strtok(NULL, s);
}
return temp;
}
*/
#include"client.h"
#include"MD5.h"
#define FILEMAX 20000000000
class userinfo;
class start_process;
char *mystrtok(char *tmp,char *s);
enum tag_login{REGISTERED=0,LOGIN,UPLOAD_F,UPLOAD_S,DOWNLOAD,ADDFRIEND,SHAREDTOFRIEND,SNOPSHOT};
enum ser_ret{REGISTERED_RET=0,LOGIN_SUCCESS,FRIEND_LIST,FILE_LIST,ERROR_PASSWD,RESUME,SECONDUPLOADSUCCESS,NOT_HAVE_THIS_FILE,FILENOFIND,STARTLOADFILE};
void getmd5(char *buff,char *path);
void start_process::deal_shared()
{
cout<<"shared to friend id:"<<endl;
char fid[20]={0};
char filename[30]={0};
cin.getline(fid,20);
cout<<"shared filename:"<<endl;
cin.getline(filename,30);
char buff[50]={0};
strncpy(buff,fid,20);
strncpy(buff,filename,30);
int tag=SHAREDTOFRIEND;
send(sockfd,&tag,4,0);
send(sockfd,buff,50,0);
}
//////////////////////////////////////////////////////////////
void start_process::deal_addfriend()
{
cout<<"input your friend id:"<<end;
char buff[30]={0};
cin.getline(buff,30);
int tag = ADDFRIEND;
send(sockfd,&tag,4,0);
send(sockfd,buff,30,0);
}
///////////////////////////////////////////////////////////////
void start_process::deal_snopshot()
{
int tag=SNOPSHOT;
send(sockfd,&tag,4,0);
}
///////////////////////////////////////////////////////////////
void start_process::deal_download()
{
char buff[30]={0};
cout<<"input what you want download:"<<endl;
cin.getline(buff,30);
int tag=DOWNLOAD;
send(sockfd,&tag,4,0);
send(sockfd,buff,30,0);
recv(sockfd,&tag,4,0);
if(tag==FILENOTFIND)
{
cout<<"NOT HAVE THIS FILE"<<endl;
}
else if(tag == STARTLOADFILE)
{
cout<<"input where you want store(include filename):"<<endl;
char pathname[50]={0};
cin.getline(pathname,50);
int filefd=open(pathname,O_WRONLY|O_CREAT,0600);
int filesize=0;
recv(sockfd,&size,4,0);
while(filesize>0)
{
ret=splice(sockfd,NULL,pfd[1],NULL,32768,SPLICE_F_MORE|SPLICE_F_MOVE);
filesize-=ret;
if(ret>0)
{
splice(pfd[0],NULL,filefd,0,ret,SPLICE_F_MORE|SPLICE_F_MOVE);
}
}
}
}
//REGISTERED_RET=0,LOGIN_SUCCESS,FRIEND_LIST,FILE_LIST,ERROR_PASSWD,RESUME,SECONDUPLOADSUCCESS,NOT_HAVE_THIS_FILE,FILENOFIND,STARTLOADFILE
void start_process::deal_resumes_transmission(char *path)
{
int tag=UPLOAD_S;
send(sockfd,&tag,4,0);
int size=0;
recv(sockfd,&size,4,0);
int filefd=open(path,O_RDONLY);
assert(filefd>0);
lseek(filefd,size,SEEK_SET);
struct stat stat_buf;
fstat(filefd,&stat_buf);
int fsize=stat_buf.st_size;
fsize-=size;
send(sockfd,&fsize,4,0);
int ret=0;
while(fsize<0)
{
ret=sendfile(filefd,sockfd,NULL,FILEMAX);
fsize-=ret;
}
}
void start_process::upload_file_to_server(char *path)
{
int tag=UPLOAD_S;
send(sockfd,&tag,4,0);
int filefd=open(path,O_RDONLY);
assert(filefd>0);
struct stat stat_buf;
fstat(filefd,&stat_buf);
int fsize=stat_buf.st_size;
send(sockfd,&fsize,4,0);
int ret=0;
while(fsize<0)
{
ret=sendfile(filefd,sockfd,NULL,FILEMAX);
fsize-=ret;
}
}
void start_process::deal_upload()
{
char buff[50]={0};
cout<<"input path of file: ";
cin.getline(buff,50);
char path[50]={0};
strcpy(path,buff);
char md5[17]={0};
getmd5(buff,md5);
char *p=my_strtok(buff,"/");
char sendmsg[40]={0};
strcpy(sendmsg,md5);
strcpy(sendmsg+20,p);
int tags=UPLOAD_F;
send(sockfd,&tags,4,0)
send(sockfd,sendmsg,40,0);
int tag=0;
recv(sockfd,&tag,4,0);
switch(tag)
{
case SECONDUPLOADSUCCESS:
cout<<"UPLOAD SUCCESS"<<endl;
break;
case RESUME:
deal_resumes_transmission(path);
break;
case NOT_HAVE_THIS_FILE:
upload_file_to_server(path);
break;
default:
break;
}
}
///////////////////////////////////////////////
void start_process::file_start()
{
while(1)
{
cout<<"1.upload file"<<endl;
cout<<"2.download file"<<endl;
cout<<"3.snopshot"<<endl;
cout<<"4.add friend"<<endl;
cout<<"5.shared file"<<endl;
cout<<"6.exit"<<endl;
cout<<"input your choice"<<endl;
int tag=0;
cin>>tag;
switch(tag)
{
case 1:
deal_upload();
break;
case 2:
deal_download();
break;
case 3:
deal_snopshot();
break;
case 4:
deal_addfriend();
break;
case 5:
deal_shared();
break;
case 6:
exit(1);
default:
break;
}
}
}
///////////////////////////////////////////////
void start_peocess::start()
{
cout<<"1. login"<<endl;
cout<<"2. registered"<<endl;
cout<<"3. exit"<<endl;
int tag;
cin>>tag;
switch(tag)
{
case 1:
deal_login();
break;
case 2:
deal_registered();
break;
case 3:
exit(1);
break;
default:
break;
}
int revc_tag;
recv(sockfd,&recv_tag,4,0);
//REGISTERED_RET=0,FRIEND_LIST,FILE_LIST,ERROR_PASSWD,RESUME,LOGIN_SUCCESS
switch(revc_tag)
{
case LOGIN_SUCCESS:
RecvFriendAndFileList();
break;
case REGISTERED_RET:
RecvRetId();
break;
case ERROR_PASSWD:
error_login();
default:
break;
}
}
///////////////////////////////////////////////////////////
void start_process::deal_login()
{
char buff[30] = { 0 };
cout<<"input id: ";
cin.getline(buff, 15);
cout<<"input passwd: "<<endl;
cin.getline(buff + 15, 15);
m_user.id(buff);
int send_tag=LOGIN;
send(sockfd,&send_tag,4,0);
send(sockfd,buff,30,0);
}
void start_peocess::deal_registered()
{
char buff[30]={0};
cout<<"input name: ";
cin.getline(buff, 15);
cout<<"input passwd: ";
cin.getline(buff + 15, 15);
int send_tag=REGISTERED;
send(sockfd,&send_tag,4,0);
send(sockfd,buff,30,0);
}
void start_process::RecvFriendAndFileList()
{
cout<<"login success!"<<endl;
int friendsize=0;;
recv(sockfd,&friendsize,4,0);
char *friend_list=new char[15*friendsize];
assert(friend_list!=NULL);
recv(sockfd,friend_list,15*friendsize,0);
assert(file_list!=NULL);
int filenumber=0;
recv(sockfd,&filenumber,4,0);
char *file_list=new char[20*filenumber];
//////cout<<jiuchadayinyibian
}
void start_process::RecvRetId()
{
char buff[15]={0};
recv(sockfd,buff,15,0);
cout<<"this is your id: ";
cout<<buff<<endl;
start();
}
void start_process::error_login()
{
cout<<"id or passwd error"<<endl;
start();
}
int main()
{
int sockfd=socket(AF_INET,SOCK_STREAM,0);
sockaddr_in saddr;
memset(&saddr,0,sizeof(saddr));
saddr.sin_family=AF_INET;
saddr.sin_port=htons(6500);
saddr.sin_addr.s_addr=inet_addr("127.0.0.1");
int res=connect(sockfd,(struct sockaddr*)&saddr,sizeof(saddr));
assert(res!=-1);
start_process start(sockfd);
start.run();
return 0;
}