-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclientWork.c
566 lines (526 loc) · 18.8 KB
/
clientWork.c
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
#include "clientWork.h"
#include "lib_use.h"
void work_register(int sockid ){
if(isLogin ==1){
printf("请退出登录后再注册\n");
return;
}
msgToServer msg_s;
msgToClient msg_c;
char buffer[BUFFER_SIZE];
memset(buffer, 0, sizeof(buffer));
memset(msg_s.userInfo.name, 0, sizeof(msg_s.userInfo.name));
memset(msg_s.userInfo.pwd, 0, sizeof(msg_s.userInfo.pwd));
printf("输入用户名:\n");
scanf("%s", msg_s.userInfo.name);
printf("输入密码:\n");
scanf("%s", msg_s.userInfo.pwd);
msg_s.action = Register;
formatMsgToJson_msgToServer(msg_s, buffer);
write(sockid, buffer, strlen(buffer));
memset(buffer, 0, sizeof(buffer));
recv(sockid, buffer, sizeof(buffer), 0);
parseJsonData_Client(&msg_c, buffer);
if(msg_c.result == 1){
printf("注册成功,你的帐号id是:%d\n", atoi(msg_c.message));
}else{
printf("注册失败, 错误码:%d,请联系管理员处理\n");
}
}
void work_login(int sockid){
if(isLogin == 1){
printf("已登录,请勿重复操作");
return;
}
msgToServer msg_s;
msgToClient msg_c;
char buffer[BUFFER_SIZE];
char *pwd;
memset(buffer, 0, sizeof(buffer));
memset(msg_s.userInfo.name, 0, sizeof(msg_s.userInfo.name));
memset(msg_s.userInfo.pwd, 0, sizeof(msg_s.userInfo.pwd));
printf("输入帐号id:\n");
scanf("%d", &msg_s.userInfo.id);
pwd = getpass("输入密码\n");
strncpy(msg_s.userInfo.pwd, pwd, strlen(pwd));
msg_s.action = Login;
formatMsgToJson_msgToServer(msg_s, buffer);
write(sockid, buffer, strlen(buffer));
memset(buffer, 0, sizeof(buffer));
recv(sockid, buffer, sizeof(buffer), 0);
parseJsonData_Client(&msg_c, buffer);
//printf("recv: %s", buffer);
if(msg_c.result == 1){
printf("登录成功:\n");
isLogin = 1;
int pid = fork();
if(pid == 0){//登录成功创建一个进程来接收来自服务段的信息;
while(1){
messReceive(sockid);
}
}
}else{
switch(msg_c.reasonCode){
case 1: printf("登录失败,密码错误\n"); break;
case 0: printf("登录失败, 帐号不存在,请先注册\n");break;
default : break;
}
}
}
void work_chat(int sockid){
if(isLogin != 1){
printf("请先登录再操作\n");
return;
}
int option;
printf("请选择:\n1.私聊\n2.群发消息\n");
scanf("%d",&option);
msgToServer msg_s;
char buffer[BUFFER_SIZE];
memset(buffer, 0, sizeof(buffer));
memset(msg_s.userInfo.name, 0, sizeof(msg_s.userInfo.name));
msg_s.action = Chat;
msg_s.actionOption = option-1;//聊天动作选项:0.私聊,1.群聊
if(option == 1){
printf("输入私聊对象:\n");
scanf("%s", msg_s.userInfo.name);
}
printf("输入消息内容:\n");
memset(msg_s.message, 0, sizeof(msg_s.message));
scanf("%s", msg_s.message);
formatMsgToJson_msgToServer(msg_s, buffer);
write(sockid, buffer, strlen(buffer));
}
void work_sendFile(int sockid){
if(isLogin != 1){
printf("请先登录再操作\n");
return;
}
msgToServer msg_s;
FileInfo fileInfo;
struct stat filestat;
char buffer[BUFFER_SIZE];
char filePath[FILEPATHLEN];
struct msgbuf msg;
printf("输入文件路径:\n");
bzero(filePath, sizeof(filePath));
scanf("%s", filePath);
int fd = open(filePath, O_RDONLY);
if(fd == -1){
perror("open file error");
return ;
}
//从路径中截取文件名
char *p = strrchr(filePath, '/') + 1;
bzero(fileInfo.name, sizeof(fileInfo.name));
fstat(fd ,&filestat);
strncpy(fileInfo.name, p, strlen(p));
fileInfo.size = filestat.st_size;
fileInfo.count = 8;
fileInfo.blockSize = FILEDIVLEN;
bzero(msg_s.message, sizeof(msg_s.message));
formatFileInfoToJson(fileInfo, msg_s.message);
printf("输入文件发送对象:\n");
bzero(msg_s.userInfo.name, sizeof(msg_s.userInfo.name));
scanf("%s", msg_s.userInfo.name);
memset(buffer, 0, sizeof(buffer));
bzero(msg.mtext, sizeof(msg.mtext));
strncpy(msg.mtext, filePath, strlen(filePath));
msg.mtype = 1;
if(msgsnd(msgid, &msg, strlen(msg.mtext), IPC_NOWAIT) == -1) {
perror("msg send error");
return;
}
msg_s.action = fileOperation;
msg_s.actionOption = 0;// 0, 请求文件发送, 1, 接受文件, 2,拒绝接受文件
formatMsgToJson_msgToServer(msg_s, buffer);
write(sockid, buffer, strlen(buffer));
close(fd);
}
void work_showOnline(int sockid){
msgToServer msg_s;
char buffer[BUFFER_SIZE];
msg_s.action = showOnline;
memset(buffer, 0, sizeof(buffer));
formatMsgToJson_msgToServer(msg_s, buffer);
write(sockid, buffer, strlen(buffer));
}
void work_superOperate(int sockid){
if(isLogin != 1){
printf("请先登录再操作\n");
return;
}
msgToServer msg_s;
char buffer[BUFFER_SIZE];
int option;
printf("请选择:\n");
printf("1.禁言\n2.解禁\n3.踢人\n");
scanf("%d", &option);
printf("请输入操作对象\n");
memset(msg_s.userInfo.name, 0, sizeof(msg_s.userInfo.name));
scanf("%s", msg_s.userInfo.name);
msg_s.actionOption = option-1;
msg_s.action = superOperation;
memset(buffer, 0, sizeof(buffer));
formatMsgToJson_msgToServer(msg_s, buffer);
write(sockid, buffer, strlen(buffer));
}
void messReceive(int sockid){
msgToClient msg_c;
char buffer[BUFFER_SIZE];
memset(buffer, 0, sizeof(buffer));
if(recv(sockid, buffer, sizeof(buffer), 0) <= 0){
printf("服务器挂了.......\n");
kill(getppid(), SIGINT);
exit(1);
}
parseJsonData_Client(&msg_c, buffer);//会给msg_s.names申请内存,需要手动释放;
//printf("recv: %s", buffer);
if(msg_c.result == 0){//封装......
switch(msg_c.action){
case chatResultReturn:{
switch(msg_c.reasonCode){
case 0: printf("信息发送失败,目标用户不在线\n");break;
case 1: printf("信息发送失败,你已被禁言\n"); break;
case 2: printf("请不要对自己发信息\n"); break;
default : break;
}
break;
}
case fileOperationResultReturn:{
switch(msg_c.reasonCode){
case 0: printf("发送失败,对方不在线\n"); break;
case 1: {
struct msgbuf msg;
printf("对方拒绝了你的文件传送请求\n");
msgrcv(msgid, &msg, sizeof(msg.mtext), 1, 0);
break;
}
case 2: printf("文件发送用户下线,文件传输取消\n"); break;
default: break;
}
break;
}
case superOperationResultReturn:{
switch(msg_c.reasonCode){
case 0: printf("管理员操作,权限不足,充会员说不定可以解决......\n");break;
case 1: printf("操作失败,目标用户不在线\n"); break;
default : break;
}
break;
}
default : break;
}
if(msg_c.nameNum !=0){
free(msg_c.names);
}
return;
}
switch(msg_c.action){
case messagePost:{
printf("用户:%s发来信息:%s\n", msg_c.names[0], msg_c.message);
break;
}
case showUsers:{
printf("在线用户:\n");
for(int i = 0; i <msg_c.nameNum; i++){
printf("%s\n", msg_c.names[i]);
}
break;
}
case sendFileRequest:{
switch(msg_c.reasonCode){
case 0: printf("你收到了一个来自用户:%s的文件传输请求, 键入0以刷新显示\n", msg_c.names[0]);break;
case 1: printf("对方接受了你的文件传送请求,键入0开始文件传输.......\n");break;
}
struct msgbuf msg;
bzero(&msg, sizeof(msg));
strncpy(msg.mtext, buffer, strlen(buffer));
msg.mtype = 2;
msgsnd(msgid, &msg, strlen(msg.mtext), IPC_NOWAIT);
break;
}
case superOperationResultReturn:{
switch(msg_c.reasonCode){
case 0: printf("你已被禁言......\n"); break;
case 1: printf("禁言状态取消\n"); break;
case 2: {
printf("你已被管理员强制下线\n");
kill(getppid(), SIGALRM);
shutdown(sockid, SHUT_RDWR);
exit(0);
break;
}
case 3: printf("用户:%s已被禁言\n", msg_c.message); break;
case 4: printf("用户:%s已被解禁\n", msg_c.message); break;
case 5: printf("用户:%s已被强制下线\n", msg_c.message); break;
default : break;
}
break;
}
default : break;
}
if(msg_c.nameNum != 0){
free(msg_c.names);
}
}
void fileSendHelper(int sockid, const msgToClient *pmsg, const char *filepath){
int option ;
msgToServer msg_s;
char buffer[BUFFER_SIZE];
if(pmsg->reasonCode == 0){
printf("1.同意\n2.拒绝\n");
scanf("%d", &option);
bzero(msg_s.userInfo.name, sizeof(msg_s.userInfo.name));
strncpy(msg_s.userInfo.name, pmsg->names[0], strlen(pmsg->names[0]));
msg_s.action = fileOperation;
if(option == 1){
printf("文件接收开始\n");
msg_s.actionOption = 2;
bzero(buffer, sizeof(buffer));
formatMsgToJson_msgToServer(msg_s, buffer);
send(sockid, buffer, strlen(buffer), 0);
//开启tcp监听,接受数据.......
FileInfo fileInfo;
parseJsonData_FileInfo(&fileInfo, pmsg->message);
receiveFile(sockid, &fileInfo);
return;
}
msg_s.actionOption = 1;
bzero(buffer, sizeof(buffer));
formatMsgToJson_msgToServer(msg_s, buffer);
send(sockid, buffer, strlen(buffer), 0);
return;
}
sendFile(pmsg->message, filepath);
}
void* checkFilesendRequest(void* psockid){
struct msgbuf msg;
int sockid = *(int*)psockid;
bzero(&msg, sizeof(msg));
if(msgrcv(msgid, &msg, sizeof(msg.mtext), 2, IPC_NOWAIT) > 0) {
char filePath[FILEPATHLEN];
msgToClient msg_c;
parseJsonData_Client(&msg_c, msg.mtext);
//printf("msgToClient:%s\n", msg.mtext);
bzero(&msg, sizeof(struct msgbuf));
msgrcv(msgid, &msg, sizeof(msg.mtext), 1, IPC_NOWAIT);
bzero(filePath, sizeof(filePath));
strncpy(filePath, msg.mtext, strlen(msg.mtext));
fileSendHelper(sockid, &msg_c, filePath);
bzero(&msg, sizeof(struct msgbuf));
}
}
void receiveFile(int sockid, FileInfo *pfileInfo){
int listenFd = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in hostAddr, peerAddr;
socklen_t addrLen = sizeof(struct sockaddr_in);
int opt = 1;
char buffer[BUFFER_SIZE];
char *fileBegin;
bzero(&hostAddr, sizeof(hostAddr));
getsockname(sockid, (struct sockaddr*)&hostAddr, (socklen_t*)&addrLen);
createfile(pfileInfo->name, pfileInfo->size);
int fd = open(pfileInfo->name, O_RDWR);
if(fd < 0){
perror("file create fail");
}
//hostAddr.sin_port = htons(11235);
fileBegin= (char*)mmap(NULL, pfileInfo->size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
setsockopt(listenFd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
if(bind(listenFd, (struct sockaddr *)&hostAddr, sizeof(hostAddr)) != 0){
perror("listenfd bind failed");
}
if(listen(listenFd, 9) != 0){
perror(" listen failed");
}
for(int i = 0; i < 8; i++){
bzero(&peerAddr, sizeof(peerAddr));
int temp = i;
int connfd = accept(listenFd, (struct sockaddr*)&peerAddr, &addrLen);
if(connfd == -1){
perror("accept error");
return;
}
int pid = fork();
if(pid < 0){
perror("fork error");
exit(1);
}
if(pid == 0){
//子进程接收文件分块
//printf udpfd = socket(AF_INET, SOCK_DGRAM, 0);
int count = pfileInfo->blockSize;
int len ;
int partFileSize;
PartOfFile part;
char tempStr[40];
/* getsockname(connfd, (struct sockaddr *)&hostAddr, &addrLen);
setsockopt(udpfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
bind(udpfd, (struct sockaddr *)&hostAddr, sizeof(hostAddr)); */
//printf("connfd %d\n", connfd);
len = recv(connfd, buffer, sizeof(buffer), 0);
if(len <= 0){
//perror("");
printf("tcp error\n");
return;
}
bzero(tempStr, sizeof(tempStr));
strncpy(tempStr, buffer, sizeof(tempStr));
part.seq = atoi(tempStr);
partFileSize = part.seq < 7 ? (pfileInfo->size/8) : pfileInfo->size - (pfileInfo->size/8)*7;
//printf("part.seq: %d\n", part.seq);
int seq = 0;
while(partFileSize > 0){
bzero(buffer, sizeof(buffer));
len = recv(connfd, buffer, sizeof(buffer), 0);
if(len == 0){
printf("recv 0\n");
break;
}
bzero(tempStr, sizeof(tempStr));
strncpy(tempStr, buffer, sizeof(tempStr));
part.offset = atoi(tempStr);
bzero(tempStr, sizeof(tempStr));
strncpy(tempStr, buffer+40, sizeof(tempStr));
part.realsize = atoi(tempStr);
//printf("part:%d seq: %d offset:%d, realsize:%d\n",temp, seq, part.offset, part.realsize);
//parseJsonData_PartOfFile(&part, buffer);
seq++;
partFileSize -= part.realsize;
memcpy(fileBegin+part.offset, buffer+80, part.realsize);
if(partFileSize == 0 ){
printf("part %d seq: %dcopmlete ------------------- ------\n",temp, seq);
}
}
shutdown(connfd, SHUT_RDWR);
close(connfd);
exit(1);
}
//close(connfd);
}
for(int i = 0; i < 8; i++){
wait(NULL);
}
printf("文件接受完成\n");
shutdown(listenFd, SHUT_RDWR);
close(fd);
munmap(fileBegin, pfileInfo->size);
}
void sendFile(const char* goalAddr, const char* filePath){
int fd = open(filePath, O_RDONLY);
int opt = 1;
struct sockaddr_in addr, addr2;
struct stat filestat;
socklen_t addrLen = sizeof(struct sockaddr_in);
char buffer[BUFFER_SIZE];
char *fileBegin;
if(fd < 0){
perror("open file error");
return ;
}
bzero(&addr, sizeof(addr));
parseIPADDR(goalAddr, &addr);
addr.sin_family = AF_INET;
//addr.sin_port = htons(11235);
//printf("%s:%d\n", inet_ntoa(addr.sin_addr), addr.sin_port);
fstat(fd ,&filestat);
fileBegin= (char*)mmap(NULL, filestat.st_size, PROT_READ,MAP_SHARED, fd, 0);
//printf("filesize: %d\n", filestat.st_size);
for(int i = 0; i < 8; i++){
int temp = i;
int connfd = socket(AF_INET, SOCK_STREAM, 0);
if(connect(connfd, (struct sockaddr *)&addr, sizeof(addr)) != 0){
perror("connect failed");
exit(1);
}
int pid = fork();
if(pid == 0){
PartOfFile part;
int partFileSize = 0;
int blockSize = FILEDIVLEN;
int udpfd;
//printf("connfd %d\n", connfd);
bzero(buffer, sizeof(buffer));
myItoA(temp, buffer);
send(connfd, buffer, sizeof(buffer), 0);
/* udpfd = socket(AF_INET, SOCK_DGRAM, 0);
bzero(&addr2, sizeof(addr2));
getpeername(connfd, (struct sockaddr *)&addr2, &addrLen); */
part.offset = temp * (filestat.st_size/8);
part.seq = temp;
partFileSize = temp < 7 ? (filestat.st_size/8) : filestat.st_size - (filestat.st_size/8)*7;
/* printf("----------------\n");
printf("i:%d partSize:%d\n", temp, partFileSize);
printf("----------------\n"); */
int seq = 0;
while(partFileSize > 0){
bzero(buffer, sizeof(buffer));
part.realsize = partFileSize > blockSize ? blockSize : partFileSize;
partFileSize -= part.realsize;
myItoA(part.offset, buffer);
myItoA(part.realsize, buffer+40);
memcpy(buffer+80, fileBegin+part.offset, part.realsize);
//printf("part:%d seq:%d offset: %d realsize %d remainSize: %d buffer: %s %s\n", temp, seq, part.offset, part.realsize, partFileSize, buffer, buffer+40);
//formatPartOfFileInfoToJson(part, buffer);
send(connfd, buffer, sizeof(buffer), 0);
seq++;
//sendto(udpfd ,buffer, strlen(buffer), 0, (struct sockaddr *)&addr2, sizeof(addr2));
part.offset += part.realsize;
if(partFileSize == 0){
printf("part %d seq:%d copmlete ------------------- ------\n", temp, seq);
}
usleep(10);
}
shutdown(connfd, SHUT_RDWR);
close(connfd);
exit(1);
}
//close(connfd);
}
for(int i = 0; i < 8; i++){
wait(NULL);
}
printf("文件发送完成\n");
munmap(fileBegin, filestat.st_size);
}
void parseIPADDR(const char* addrString, struct sockaddr_in *addr){
char temp[IPV4ADDRLEN];
int i = 0;
bzero(temp, sizeof(temp));
while(addrString[i] != ':'){
temp[i] = addrString[i];
i++;
}
i++;
addr->sin_addr.s_addr = inet_addr(temp);
addr->sin_port = atoi(addrString+i);
}
int createfile(char *filename, int size)
{
int fd = open(filename, O_RDWR | O_CREAT| O_TRUNC, 0666);
int pos = lseek(fd, size-1, SEEK_SET);
write(fd, "", 1);
close(fd);
return 0;
}
void myItoA(int a, char *string){
int temp = a;
int i = 0;
int j=0;
if(temp == 0){
string[0] = '0';
return;
}
while(temp != 0){
string[i] = temp%10 + '0';
temp /=10;
i++;
}
i--;
for(; j < i; j++,i--){
string[j] = string[i] + string[j];
string[i] = string[j] - string[i];
string[j] = string[j] - string[i];
}
}