-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path12-4client.c
More file actions
432 lines (380 loc) · 11.6 KB
/
12-4client.c
File metadata and controls
432 lines (380 loc) · 11.6 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
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
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include "client.h"
int main(int argc, const char *argv[])
{
int sockfd;
struct sockaddr_in serveraddr;
MSG msg;
/*创建流式套接字*/
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
perror("socket error");
return -1;
}
/*填充网络信息结构体*/
bzero(&serveraddr, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_addr.s_addr = inet_addr(argv[1]);
serveraddr.sin_port = htons(atoi(argv[2]));
/*连接服务器*/
if(connect(sockfd, (struct sockaddr *)&serveraddr,\
sizeof(serveraddr)) < 0){
perror("connect error");
return -1;
}
/*登录界面,使用循环,保证操作错误时可以再次返回该界面*/
while(1){
puts("==============================================");
puts("+++++++++++++++++++Login++++++++++++++++++++++");
puts("==============================================");
/*输入登录信息 名字+密码*/
printf("Please input your name >");
fgets(msg.name, N, stdin);
msg.name[strlen(msg.name) -1] = '\0';
printf("Please input your password >");
fgets(msg.passwd, N, stdin);
msg.passwd[strlen(msg.passwd) - 1] = '\0';
msg.type = LOAD;
/*发送消息给服务器,进行登陆验证*/
send(sockfd, &msg, sizeof(MSG), 0);
printf("---load type %d\n", msg.type);
/*接收服务器的反馈消息*/
recv(sockfd, &msg, sizeof(MSG), 0);
/*根据服务器端返回的标志进行判断*/
/*登陆成功*/
if(msg.sign == SUCCESS){
/*进入业主登陆界面*/
if(msg.info.type == STAFF){
goto User;
}
/*进入物业管理界面*/
else if(msg.info.type == ADM){
goto Admin;
}
}
/*登陆失败*/
if(msg.sign == FAILED){
printf("%s\n", msg.data);
continue;
}
}
/*跳转到物业管理界面*/
Admin:
while(1){
/*管理员权限*/
puts("===========================================================");
puts("1:add user 2:delete user 3:modify info 4:select info 5:exit");
puts("===========================================================");
printf("please input your command > ");//输入对应的操作数字
/*输入错误命令情况处理*/
int result;
int command;
char clear[N];
if(scanf("%d", &command) == 0){
fgets(clear, N, stdin);
continue;
}
switch(command){
case 1:
/*添加业主信息*/
result = do_adduser(sockfd, &msg);
if(result == SUCCESS){
puts("注册业主信息成功");
}
else if(result == FAILED){
printf("%s\n", msg.data);
continue;
}
break;
case 2:
/*删除业主信息*/
result = do_deluser(sockfd, &msg);
if(result == SUCCESS){
puts("删除业主信息成功");
}
else if(result == FAILED){
printf("%s\n", msg.data);
puts("删除业主信息失败");
continue;
}
break;
case 3:
/*修改业主信息*/
result = do_modifyuser(sockfd, &msg);
if(result == SUCCESS){
puts("修改业主信息成功");
}
else if(result == FAILED){
printf("%s\n", msg.data);
puts("修改业主信息失败");
continue;
}
break;
case 4:
/*查询业主信息*/
result = do_selectuser(sockfd, &msg);
if(result == SUCCESS){
printf("姓名:%s\n", msg.info.name);
printf("业主详细地址:%s\n", msg.info.addr);
printf("停车费年卡办理时间:%s\n", msg.info.time_start);
printf("停车卡有效截止日期:%s\n", msg.info.time_end);
printf("车位编号:%d\n", msg.info.location);
printf("业主编号:%d\n", msg.info.no);
printf("年卡费用:%d\n", msg.info.card);
printf("手机号:%s\n", msg.info.phone);
printf("用户类型:%d\n", msg.info.type);
printf("车牌号:%s\n", msg.info.car_num);
}
else if(result == FAILED){
printf("%s\n", msg.data);
puts("业主信息不存在");
continue;
}
break;
case 5:
msg.type = QUIT;
send(sockfd, &msg, sizeof(MSG), 0);
goto Exit;
}
}
/*跳转到业主界面*/
User:
while(1){
/*普通业主权限*/
puts("==============================================");
puts("+++++1:select info 2:modify passwd 3:exit+++++");
puts("==============================================");
printf("please input your command > ");
/*处理输入错误命令的情况*/
int command;
char clear[N];
/****************************************************
* 如果终端输入的内容未被成功读取,则返回值为零;
* 说明本次输入的选择,不合规则,无法读取;
* continue跳过本次循环,重新让业主选择
***************************************************/
if(scanf("%d", &command) == 0){
fgets(clear, N, stdin);
continue;
}
switch(command){
case 1:
msg.type = READ;
strcpy(msg.info.name, msg.name);
printf("请输入编号 > ");
input_no:
if(scanf("%d", &(msg.info.no)) == 0){
printf("input type error, exp 1001\n");
fgets(clear, N, stdin);
goto input_no;
}
/*发送查询消息*/
send(sockfd, &msg, sizeof(MSG), 0);
/*接收服务器的反馈消息*/
recv(sockfd, &msg, sizeof(MSG), 0);
/*打印用户自身消息*/
printf("姓名:%s\n", msg.info.name);
printf("业主详细地址:%s\n", msg.info.addr);
printf("办停车卡时间:%s\n", msg.info.time_start);
printf("停车卡有效期:%s\n", msg.info.time_end);
printf("车位编号:%d\n", msg.info.location);
printf("业主编号:%d\n", msg.info.no);
printf("年卡费用:%d\n", msg.info.card);
printf("手机号:%s\n", msg.info.phone);
printf("用户类型:%d\n", msg.info.type);
printf("车牌号:%s\n", msg.info.car_num);
break;
case 2:
/*向服务器端确认需要修改密码的用户的编号以及名字*/
strcpy(msg.info.name, msg.name);
getchar();
printf("请输入业主编号 > ");
input_no1:
if(scanf("%d", &(msg.info.no)) == 0){
printf("input type error, exp 1001\n");
fgets(clear, N, stdin);
goto input_no1;
}
getchar();
printf("请输入新的登录密码 > ");
fgets(msg.passwd, N, stdin);
msg.passwd[strlen(msg.passwd) - 1] = '\0';
msg.type = CHANGE;
send(sockfd, &msg, sizeof(MSG), 0);
break;
case 3:
msg.type = QUIT;
send(sockfd, &msg, sizeof(MSG), 0);
goto Exit;
}
}
Exit:
close(sockfd);
return 0;
}
/*添加业主信息*/
int do_adduser(int sockfd, MSG *msg){
printf("请输入业主的姓名 > ");
getchar();
fgets((msg->info).name, N, stdin);
(msg->info).name[strlen((msg->info).name) - 1] = '\0';
printf("请输入业主的详细地址 > ");
fgets((msg->info).addr, N, stdin);
(msg->info).addr[strlen((msg->info).addr) - 1] = '\0';
printf("请输入业主的手机号码 > ");
fgets((msg->info).phone, N, stdin);
(msg->info).phone[strlen((msg->info).phone) - 1] = '\0';
printf("请输入业主的车牌号 > ");
fgets((msg->info).car_num, N, stdin);
(msg->info).car_num[strlen((msg->info).car_num) - 1] = '\0';
printf("请输入业主办理停车位起始日期 > ");
fgets((msg->info).time_start, N, stdin);
(msg->info).time_start[strlen((msg->info).time_start) - 1] = '\0';
printf("请输入业主停车位使用截止日期 > ");
fgets((msg->info).time_end, N, stdin);
(msg->info).time_end[strlen((msg->info).time_end) - 1] = '\0';
char clear[N];
input_location:
printf("请输入业主停车位编号 > ");
if(scanf("%d", &(msg->info.location)) == 0){
printf("input type error, exp 1001\n");
fgets(clear, N, stdin);
goto input_location;
}
getchar();
input_no:
printf("请输入业主编号 > ");
if(scanf("%d", &(msg->info.no)) == 0){
printf("input type error, exp 1001\n");
fgets(clear, N, stdin);
goto input_no;
}
getchar();
input_card:
printf("请输入停车位年卡费用 > ");
if(scanf("%d", &(msg->info.card)) == 0){
printf("input type error, exp 1200\n");
fgets(clear, N, stdin);
goto input_card;
}
getchar();
input_type:
printf("请输入业主类型 > ");
if(scanf("%d", &(msg->info.type)) == 0){
printf("input type error\n");
printf("类型选择:1.物业管理 2.小区常住业主 3.小区租户\n");
fgets(clear, N, stdin);
goto input_card;
}
getchar();
printf("请输入业主系统登陆密码 > ");
fgets(msg->passwd, N, stdin);
msg->passwd[strlen(msg->passwd) - 1] = '\0';
/*发送操作类型给服务器*/
msg->type = ADD;
/*发送给服务器的结构体类型,必须和客户端一致*/
send(sockfd, msg, sizeof(MSG), 0);
recv(sockfd, msg, sizeof(MSG), 0);
return msg->sign;//返回服务器端的处理信息
}
/*删除业主信息*/
int do_deluser(int sockfd, MSG *msg){
printf("请输入删除业主的姓名 > ");
getchar();
fgets((msg->info).name, N, stdin);
(msg->info).name[strlen((msg->info).name) - 1] = '\0';
printf("请输入删除业主的编号 > ");
if(scanf("%d", &(msg->info.no)) == 0){
msg->info.no = 0;
}
msg->type = DELETE;
send(sockfd, msg, sizeof(MSG), 0);
recv(sockfd, msg, sizeof(MSG), 0);
return msg->sign;//返回服务器端的处理信息
}
/*修改业主信息*/
int do_modifyuser(int sockfd, MSG *msg){
char clear[N];
printf("请输入被修改业主的姓名 > ");
getchar();
fgets((msg->info).name, N, stdin);
(msg->info).name[strlen((msg->info).name) - 1] = '\0';
input_no:
printf("请输入被修改业主编号 > ");
if(scanf("%d", &(msg->info.no)) == 0){
printf("input type error, exp 1001\n");
fgets(clear, N, stdin);
goto input_no;
}
getchar();
printf("请输入业主新的详细地址 > ");
fgets((msg->info).addr, N, stdin);
(msg->info).addr[strlen((msg->info).addr) - 1] = '\0';
printf("请输入业主新的手机号码 > ");
fgets((msg->info).phone, N, stdin);
(msg->info).phone[strlen((msg->info).phone) - 1] = '\0';
printf("请输入业主新的车牌号 > ");
fgets((msg->info).car_num, N, stdin);
(msg->info).car_num[strlen((msg->info).car_num) - 1] = '\0';
printf("请输入业主新办理停车位起始日期 > ");
fgets((msg->info).time_start, N, stdin);
(msg->info).time_start[strlen((msg->info).time_start) - 1] = '\0';
printf("请输入业主新停车位使用截止日期 > ");
fgets((msg->info).time_end, N, stdin);
(msg->info).time_end[strlen((msg->info).time_end) - 1] = '\0';
input_location:
printf("请输入业主新停车位编号 > ");
if(scanf("%d", &(msg->info.location)) == 0){
printf("input type error, exp 1001\n");
fgets(clear, N, stdin);
goto input_location;
}
getchar();
input_card:
printf("请输入停车位年卡费用 > ");
if(scanf("%d", &(msg->info.card)) == 0){
printf("input type error, exp 1200\n");
fgets(clear, N, stdin);
goto input_card;
}
getchar();
input_type:
printf("请输入业主类型 > ");
if(scanf("%d", &(msg->info.type)) == 0){
printf("input type error, exp 2\n");
fgets(clear, N, stdin);
goto input_card;
}
getchar();
printf("请输入业主系统登陆密码 > ");
fgets(msg->passwd, N, stdin);
msg->passwd[strlen(msg->passwd) - 1] = '\0';
/*发送操作类型给服务器*/
msg->type = CHANGE;
/*发送给服务器的结构体类型,必须和客户端一致*/
send(sockfd, msg, sizeof(MSG), 0);
recv(sockfd, msg, sizeof(MSG), 0);
return msg->sign;//返回服务器端的处理信息
}
/*查询业主信息*/
int do_selectuser(int sockfd, MSG *msg){
printf("请输入业主的姓名 > ");
getchar();
fgets((msg->info).name, N, stdin);
(msg->info).name[strlen((msg->info).name) - 1] = '\0';
/*当输入其他字符时,默认要查询的no值为0*/
printf("请输入业主编号 > ");
if(scanf("%d", &(msg->info.no)) == 0){
msg->info.no = 0;
}
msg->type = READ;
send(sockfd, msg, sizeof(MSG), 0);
recv(sockfd, msg, sizeof(MSG), 0);
return msg->sign;
}