forked from jihao/AddressBook
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserver.cpp
More file actions
146 lines (130 loc) · 2.45 KB
/
server.cpp
File metadata and controls
146 lines (130 loc) · 2.45 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
#include "server.h"
#include "contact.h"
#include "cs.h"
using namespace std;
Server::Server()
{
//configue server
Initial();
}
void Server::accept()
{
Accept(msg,sizeof(msg));
}
void Server::response()
{
//add cmd
if(msg[0]=='a')
{
Contact c;
int i=2;
//name
while(msg[i]!=';')
{
c.name.append(1,msg[i]);
i++;
}
i++;
//number
while(msg[i]!=';')
{
c.number.append(1,msg[i]);
i++;
}
i++;
//address
while(msg[i]!='\0')
{
c.address.append(1,msg[i]);
i++;
}
if(ad.add(c))
stringTochar("Add successful!");
else
stringTochar("Add fail!");
}
//delete cmd
else if(msg[0]=='d')
{
string s;
int i=2;
while(msg[i]!='\0')
{
s.append(1,msg[i]);
i++;
}
if(ad.remove(s))
stringTochar("Delete successful!");
else
stringTochar("Delete fail");
}
//search cmd
else if(msg[0]=='s')
{
string s;
int i=2;
while(msg[i]!='\0')
{
s.append(1,msg[i]);
i++;
}
pList it;
if(ad.search(s,it))
{
string s;
s.append("name");
s.append(1,':');
s.append(it->name);
s.append(1,'\n');
s.append("number");
s.append(1,':');
s.append(it->number);
s.append(1,'\n');
s.append("address");
s.append(1,':');
s.append(it->address);
stringTochar(s);
}
else
{
stringTochar("The person don't exit");
}
}
//help or quit cmd
else if(msg[0]=='!')
{
if(msg[1]=='h')
{//content of help
stringTochar(" ");
}
else if(msg[1]=='q')
{
ad.isQuit=1;
stringTochar("Quit successfuly!");
}
}
Response(msg,sizeof(msg));
}
void Server::stringTochar(const string &s)
{
int l=s.length();
int i;
for(i=0;i<l;i++)
msg[i]=s[i];
msg[i]='\0';
}
Server::~Server()
{
//End connection
EndSesstion();
}
int main()
{
Server server;
while(!server.ad.isQuit)
{
server.accept();
server.response();
}
return 0;
}