C++项目 | 集群聊天服务器 | 客户端好友功能、群组功能开发以及退出功能实现

1.服务器支持的命令

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
// "help" command handler
void help(int fd = 0, string str = "");
// "chat" command handler
void chat(int, string);
// "addfriend" command handler
void addfriend(int, string);
// "creategroup" command handler
void creategroup(int, string);
// "addgroup" command handler
void addgroup(int, string);
// "groupchat" command handler
void groupchat(int, string);
// "loginout" command handler
void loginout(int, string);

// 系统支持的客户端命令列表
unordered_map<string, string> commandMap = {
{"help", "显示所有支持的命令,格式help"},
{"chat", "一对一聊天,格式chat:friendid:message"},
{"addfriend", "添加好友,格式addfriend:friendid"},
{"creategroup", "创建群组,格式creategroup:groupname:groupdesc"},
{"addgroup", "加入群组,格式addgroup:groupid"},
{"groupchat", "群聊,格式groupchat:groupid:message"},
{"loginout", "注销,格式loginout"}};

// 注册系统支持的客户端命令处理
unordered_map<string, function<void(int, string)>> commandHandlerMap = {
{"help", help},
{"chat", chat},
{"addfriend", addfriend},
{"creategroup", creategroup},
{"addgroup", addgroup},
{"groupchat", groupchat},
{"loginout", loginout}
};

2.好友功能相关

实现chat函数时遇到的问题:

json解析时出现问题,本来应该接收到一个int结果却是null

是因为发送方和接受方的json格式没有对应

聊天业务中发送方的json中是to,而接收方json中是toid,导致出现解析问题

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
// "help" command handler
void help(int, string)
{
cout << "show command list >>> " << endl;
for (auto &p : commandMap)
{
cout << p.first << " : " << p.second << endl;
}
cout << endl;
}
// "addfriend" command handler
void addfriend(int clientfd, string str)
{
int friendid = atoi(str.c_str());
json js;
js["msgid"] = ADD_FRIEND_MSG;
js["id"] = g_currentUser.getId();
js["friendid"] = friendid;
string buffer = js.dump();

int len = send(clientfd, buffer.c_str(), strlen(buffer.c_str()) + 1, 0);
if (-1 == len)
{
cerr << "send addfriend msg error -> " << buffer << endl;
}
}

// "chat" command handler
void chat(int clientfd, string str)
{
int idx = str.find(":"); // friendid:message
if (-1 == idx)
{
cerr << "chat command invalid!" << endl;
return;
}

int friendid = atoi(str.substr(0, idx).c_str());
string message = str.substr(idx + 1, str.size() - idx);

json js;
js["msgid"] = ONE_CHAT_MSG;
js["id"] = g_currentUser.getId();
js["name"] = g_currentUser.getName();
js["toid"] = friendid;
js["msg"] = message;
js["time"] = getCurrentTime();
string buffer = js.dump();

int len = send(clientfd, buffer.c_str(), strlen(buffer.c_str()) + 1, 0);
if (-1 == len)
{
cerr << "send chat msg error -> " << buffer << endl;
}
}

3.群组功能相关

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

// "creategroup" command handler groupname:groupdesc
void creategroup(int clientfd, string str)
{
int idx = str.find(":");
if (-1 == idx)
{
cerr << "creategroup command invalid!" << endl;
return;
}

string groupname = str.substr(0, idx);
string groupdesc = str.substr(idx + 1, str.size() - idx);

json js;
js["msgid"] = CREATE_GROUP_MSG;
js["id"] = g_currentUser.getId();
js["groupname"] = groupname;
js["groupdesc"] = groupdesc;
string buffer = js.dump();

int len = send(clientfd, buffer.c_str(), strlen(buffer.c_str()) + 1, 0);
if (-1 == len)
{
cerr << "send creategroup msg error -> " << buffer << endl;
}
}
// "addgroup" command handler
void addgroup(int clientfd, string str)
{
int groupid = atoi(str.c_str());
json js;
js["msgid"] = ADD_GROUP_MSG;
js["id"] = g_currentUser.getId();
js["groupid"] = groupid;
string buffer = js.dump();

int len = send(clientfd, buffer.c_str(), strlen(buffer.c_str()) + 1, 0);
if (-1 == len)
{
cerr << "send addgroup msg error -> " << buffer << endl;
}
}

// "groupchat" command handler groupid:message
void groupchat(int clientfd, string str)
{
int idx = str.find(":");
if (-1 == idx)
{
cerr << "groupchat command invalid!" << endl;
return;
}

int groupid = atoi(str.substr(0, idx).c_str());
string message = str.substr(idx + 1, str.size() - idx);

json js;
js["msgid"] = GROUP_CHAT_MSG;
js["id"] = g_currentUser.getId();
js["name"] = g_currentUser.getName();
js["groupid"] = groupid;
js["msg"] = message;
js["time"] = getCurrentTime();
string buffer = js.dump();

int len = send(clientfd, buffer.c_str(), strlen(buffer.c_str()) + 1, 0);
if (-1 == len)
{
cerr << "send groupchat msg error -> " << buffer << endl;
}
}

4.注销功能

加入了全局变量isMainMenuRunning表示用户是否在线,退出登录后就变为false,在线就是true

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// "loginout" command handler
void loginout(int clientfd, string)
{
json js;
js["msgid"] = LOGINOUT_MSG;
js["id"] = g_currentUser.getId();
string buffer = js.dump();

int len = send(clientfd, buffer.c_str(), strlen(buffer.c_str()) + 1, 0);
if (-1 == len)
{
cerr << "send loginout msg error -> " << buffer << endl;
}
else
{
//代表用户退出登录了
isMainMenuRunning=false;
}
}

ChatService.cpp

记得要先绑定

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//处理注销业务
void ChatService::loginout(const TcpConnectionPtr& conn,json &js,Timestamp time)
{
int userid=js["id"].get<int>();

{
lock_guard<mutex> lock(_connMutex);
auto it=_userConnMap.find(userid);
if(it!=_userConnMap.end())
{
_userConnMap.erase(it);
}
}

//更新用户状态信息
User user(userid,",","offline");
_userModel.updateState(user);
}

5.完整的客户端代码

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
567
568
#include"json.hpp"
#include<iostream>
#include<thread>
#include<string>
#include<vector>
#include<chrono>
#include<ctime>
using namespace std;

using json=nlohmann::json;

#include<unistd.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<arpa/inet.h>

#include"group.hpp"
#include"user.hpp"
#include"public.hpp"

//控制主菜单页面程序 用户输入loginout要退出的时候改为false
bool isMainMenuRunning=false;

//记录当前系统登录的用户信息
User g_currentUser;

//记录当前登录用户的好友列表信息
vector<User> g_currentUserFriendList;

//记录当前登录用户的群组列表信息
vector<Group> g_currentUserGroupList;

//显示当前登录成功用户的基本信息
void showCurrentUserData();

//接收线程
void readTaskHandler(int clientfd);

//获取系统时间 (聊天信息需要添加时间信息)
string getCurrentTime();

//主页面程序
void mainMenu(int clientfd);

//聊天客户端程序实现,main线程用作发送消息的线程,子线程用作接收消息的线程
int main(int argc,char **argv)
{
if(argc<3)
{
cerr<<"command invalid!example: ./ChatClient 127.0.0.1 6000"<<endl;
exit(-1);
}

//解析通过命令行参数传递的ip和port
char *ip=argv[1];
uint16_t port=atoi(argv[2]);

//创建client端的socket
int clientfd=socket(AF_INET,SOCK_STREAM,0);
if(-1==clientfd)
{
cerr<<"socket create error"<<endl;
exit(-1);
}

//填写client需要连接的server信息ip+port
sockaddr_in server;
memset(&server,0,sizeof(sockaddr_in));

server.sin_family=AF_INET;
server.sin_port=htons(port);
server.sin_addr.s_addr=inet_addr(ip);

//client和server连接
if(-1==connect(clientfd,(sockaddr *)&server,sizeof(sockaddr_in)))
{
cerr<<"connect server error"<<endl;
close(clientfd);
exit(-1);
}

//main线程用于接收用户输入负责发送数据
for(;;)
{
//显示首页页面菜单 登录、注册、退出
cout<<"======================="<<endl;
cout<<"1.login"<<endl;
cout<<"2.register"<<endl;
cout<<"3.quit"<<endl;
cout<<"======================="<<endl;
cout<<"choice:";
int choice = 0;
cin>>choice;
cin.get();//读掉缓冲区残留的回车

switch(choice)
{
case 1://login业务
{
int id=0;
char pwd[50]={0};
cout<<"userid:";
cin>>id;
cin.get();//读掉缓冲区残留的回车
cout<<"userpassword:";
cin.getline(pwd,50);

json js;
js["msgid"]=LOGIN_MSG;
js["id"]=id;
js["password"]=pwd;
string request=js.dump();

int len=send(clientfd,request.c_str(),strlen(request.c_str())+1,0);
if(len==-1)
{
cerr<<"send login msg error:"<<request<<endl;
}
else
{
char buffer[1024]={0};
len=recv(clientfd,buffer,1024,0);
if(len==-1)
{
cerr<<"recv login msg error:"<<request<<endl;
}
else
{
json responsejs=json::parse(buffer);
if(0!=responsejs["errno"].get<int>())
{
cerr<<responsejs["errmsg"]<<endl;
}
else//登陆成功
{
//记录当前用户的id和name
g_currentUser.setId(responsejs["id"].get<int>());
g_currentUser.setName(responsejs["name"]);

//记录当前的好友列表
//如果这个人有好友
if(responsejs.contains("friends"))
{
//初始化
g_currentUserFriendList.clear();

vector<string> vec=responsejs["friends"];
for(string &str:vec)
{
json js=json::parse(str);
User user;
user.setId(js["id"].get<int>());
user.setName(js["name"]);
user.setState(js["state"]);
g_currentUserFriendList.push_back(user);
}
}

//记录当前用户的群组列表信息
if(responsejs.contains("groups"))
{
//初始化 出去原来已经拉入过来的数据
g_currentUserGroupList.clear();

vector<string> vec1 = responsejs["groups"];
for (string &groupstr : vec1)
{
json grpjs = json :: parse(groupstr);
Group group;
group.setId(grpjs["id"].get<int>());
group.setName(grpjs["groupname"]);
group.setDesc(grpjs["groupdesc"]);
vector<string> vec2 = grpjs["users"];
for (string &userstr : vec2)
{
GroupUser user;
json js = json :: parse(userstr);
user.setId(js["id"].get<int>());
user.setName(js["name"]);
user.setState(js["state"]);
user.setRole(js["role"]);
group.getUsers().push_back(user);
}
g_currentUserGroupList.push_back(group);
}
}

//显示登录用户的基本信息
showCurrentUserData();

//显示当前用户的离线消息 个人聊天信息或者群组消息
if(responsejs.contains("offlinemsg"))
{
vector<string> vec=responsejs["offlinemsg"];
for(string &str:vec)
{
json js=json::parse(str);
int msgtype=js["msgid"].get<int>();
if (ONE_CHAT_MSG == msgtype)
{
cout << js["time"].get<string>() << " [" << js["id"] << "]" << js["name"].get<string>()
<< " said: " << js["msg"].get<string>() << endl;

}
if (GROUP_CHAT_MSG == msgtype)
{
cout << "群消息[" << js["groupid"] << "]:" << js["time"].get<string>() << " [" << js["id"] << "]" << js["name"].get<string>()
<< " said: " << js["msg"].get<string>() << endl;
}
}
}
//登陆成功,启动接收线程负责接收数据
static int readthreadNumber=0;
if(readthreadNumber==0)
{
std::thread readTask(readTaskHandler,clientfd);
readTask.detach();
readthreadNumber++;
}
//进入主菜单页面
isMainMenuRunning=true;
mainMenu(clientfd);
}
}
}
}
break;
case 2://register业务
{
char name[50]={0};
char pwd[50]={0};
cout<<"usernam:";
cin.getline(name,50);
cout<<"userpassword:";
cin.getline(pwd,50);

json js;
js["msgid"]=REG_MSG;
js["name"]=name;
js["password"]=pwd;
string request=js.dump();

int len=send(clientfd,request.c_str(),strlen(request.c_str())+1,0);
if(len==-1)
{
cerr<<"send reg msg error:"<<request<<endl;
}
else
{
char buffer[1024]={0};
len=recv(clientfd,buffer,1024,0);
if(-1==len)
{
cerr<<"recv reg response error"<<endl;
}
else
{
json responsejs=json::parse(buffer);
if(0!=responsejs["errno"].get<int>())//注册失败
{
cerr<<name<<"is already exist,register error!"<<endl;
}
else//注册成功
{
cout<<name<<"register success,userid is "<<responsejs["id"]<<",do not forget it!"<<endl;
}
}
}
}
break;
case 3://quit业务
close(clientfd);
exit(0);
default:
cerr<<"invalid input!"<<endl;
break;
}
}
return 0;
}

//显示当前登录成功用户的基本信息
void showCurrentUserData()
{
cout<<"==================login user=================="<<endl;
cout<<"current login user id:"<<g_currentUser.getId()<<" name:"<<g_currentUser.getName()<<endl;
cout<<"------------------friend list-----------------"<<endl;
if(!g_currentUserFriendList.empty())
{
for(User &user:g_currentUserFriendList)
{
cout<<user.getId()<<" "<<user.getName()<<" "<<user.getState()<<endl;
}
}
cout<<"------------------group list------------------"<<endl;
if(!g_currentUserGroupList.empty())
{
for(Group &group:g_currentUserGroupList)
{
cout<<group.getId()<<" "<<group.getName()<<" "<<group.getDesc()<<endl;
for(GroupUser &user:group.getUsers())
{
cout<<user.getId()<<" "<<user.getName()<<" "<<user.getState()<<" "<<user.getRole()<<endl;
}
}
}
cout<<"=============================================="<<endl;
}

// "help" command handler
void help(int fd = 0, string str = "");
// "chat" command handler
void chat(int, string);
// "addfriend" command handler
void addfriend(int, string);
// "creategroup" command handler
void creategroup(int, string);
// "addgroup" command handler
void addgroup(int, string);
// "groupchat" command handler
void groupchat(int, string);
// "loginout" command handler
void loginout(int, string);

// 系统支持的客户端命令列表
unordered_map<string, string> commandMap = {
{"help", "显示所有支持的命令,格式help"},
{"chat", "一对一聊天,格式chat:friendid:message"},
{"addfriend", "添加好友,格式addfriend:friendid"},
{"creategroup", "创建群组,格式creategroup:groupname:groupdesc"},
{"addgroup", "加入群组,格式addgroup:groupid"},
{"groupchat", "群聊,格式groupchat:groupid:message"},
{"loginout", "注销,格式loginout"}};

// 注册系统支持的客户端命令处理
unordered_map<string, function<void(int, string)>> commandHandlerMap = {
{"help", help},
{"chat", chat},
{"addfriend", addfriend},
{"creategroup", creategroup},
{"addgroup", addgroup},
{"groupchat", groupchat},
{"loginout", loginout}
};

//获取系统时间 (聊天信息需要添加时间信息)
string getCurrentTime()
{
auto tt = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
struct tm *ptm = localtime(&tt);
char date[60] = {0};
sprintf(date, "%d-%02d-%02d %02d:%02d:%02d",
(int)ptm->tm_year + 1900, (int)ptm->tm_mon + 1, (int)ptm->tm_mday,
(int)ptm->tm_hour, (int)ptm->tm_min, (int)ptm->tm_sec);
return std::string(date);
}

//主页面程序
void mainMenu(int clientfd)
{
help();

char buffer[1024] = {0};
while(isMainMenuRunning)
{
cin.getline(buffer, 1024);
string commandbuf(buffer);
string command; // 存储命令
int idx = commandbuf.find(":");
if (-1 == idx)
{
command = commandbuf;
}
else
{
command = commandbuf.substr(0, idx);
}
auto it = commandHandlerMap.find(command);
if (it == commandHandlerMap.end())
{
cerr << "invalid input command!" << endl;
continue;
}

// 调用相应命令的事件处理回调,mainMenu对修改封闭,添加新功能不需要修改该函数
it->second(clientfd, commandbuf.substr(idx + 1, commandbuf.size() - idx)); // 调用命令处理方法
}
}

// "help" command handler
void help(int, string)
{
cout << "show command list >>> " << endl;
for (auto &p : commandMap)
{
cout << p.first << " : " << p.second << endl;
}
cout << endl;
}
// "addfriend" command handler
void addfriend(int clientfd, string str)
{
int friendid = atoi(str.c_str());
json js;
js["msgid"] = ADD_FRIEND_MSG;
js["id"] = g_currentUser.getId();
js["friendid"] = friendid;
string buffer = js.dump();

int len = send(clientfd, buffer.c_str(), strlen(buffer.c_str()) + 1, 0);
if (-1 == len)
{
cerr << "send addfriend msg error -> " << buffer << endl;
}
}

// "chat" command handler
void chat(int clientfd, string str)
{
int idx = str.find(":"); // friendid:message
if (-1 == idx)
{
cerr << "chat command invalid!" << endl;
return;
}

int friendid = atoi(str.substr(0, idx).c_str());
string message = str.substr(idx + 1, str.size() - idx);

json js;
js["msgid"] = ONE_CHAT_MSG;
js["id"] = g_currentUser.getId();
js["name"] = g_currentUser.getName();
js["toid"] = friendid;
js["msg"] = message;
js["time"] = getCurrentTime();
string buffer = js.dump();

int len = send(clientfd, buffer.c_str(), strlen(buffer.c_str()) + 1, 0);
if (-1 == len)
{
cerr << "send chat msg error -> " << buffer << endl;
}
}

// "creategroup" command handler groupname:groupdesc
void creategroup(int clientfd, string str)
{
int idx = str.find(":");
if (-1 == idx)
{
cerr << "creategroup command invalid!" << endl;
return;
}

string groupname = str.substr(0, idx);
string groupdesc = str.substr(idx + 1, str.size() - idx);

json js;
js["msgid"] = CREATE_GROUP_MSG;
js["id"] = g_currentUser.getId();
js["groupname"] = groupname;
js["groupdesc"] = groupdesc;
string buffer = js.dump();

int len = send(clientfd, buffer.c_str(), strlen(buffer.c_str()) + 1, 0);
if (-1 == len)
{
cerr << "send creategroup msg error -> " << buffer << endl;
}
}
// "addgroup" command handler
void addgroup(int clientfd, string str)
{
int groupid = atoi(str.c_str());
json js;
js["msgid"] = ADD_GROUP_MSG;
js["id"] = g_currentUser.getId();
js["groupid"] = groupid;
string buffer = js.dump();

int len = send(clientfd, buffer.c_str(), strlen(buffer.c_str()) + 1, 0);
if (-1 == len)
{
cerr << "send addgroup msg error -> " << buffer << endl;
}
}

// "groupchat" command handler groupid:message
void groupchat(int clientfd, string str)
{
int idx = str.find(":");
if (-1 == idx)
{
cerr << "groupchat command invalid!" << endl;
return;
}

int groupid = atoi(str.substr(0, idx).c_str());
string message = str.substr(idx + 1, str.size() - idx);

json js;
js["msgid"] = GROUP_CHAT_MSG;
js["id"] = g_currentUser.getId();
js["name"] = g_currentUser.getName();
js["groupid"] = groupid;
js["msg"] = message;
js["time"] = getCurrentTime();
string buffer = js.dump();

int len = send(clientfd, buffer.c_str(), strlen(buffer.c_str()) + 1, 0);
if (-1 == len)
{
cerr << "send groupchat msg error -> " << buffer << endl;
}
}

// "loginout" command handler
void loginout(int clientfd, string)
{
json js;
js["msgid"] = LOGINOUT_MSG;
js["id"] = g_currentUser.getId();
string buffer = js.dump();

int len = send(clientfd, buffer.c_str(), strlen(buffer.c_str()) + 1, 0);
if (-1 == len)
{
cerr << "send loginout msg error -> " << buffer << endl;
}
else
{
//代表用户退出登录了
isMainMenuRunning=false;
}
}

//接收线程
void readTaskHandler(int clientfd)
{
for ( ;; )
{
char buffer[1024] = {0};
int len = recv(clientfd, buffer,1024,0);
if (-1 == len||0 == len)
{
close(clientfd);
exit(-1);
}
//接收Chaterver转发的数据,反序列化生成json数据对象
json js = json::parse(buffer);
int msgtype=js["msgid"].get<int>();
if (ONE_CHAT_MSG == msgtype)
{
cout << js["time"].get<string>() << " [" << js["id"] << "]" << js["name"].get<string>()
<< " said: " << js["msg"].get<string>() << endl;
continue;
}
if (GROUP_CHAT_MSG == msgtype)
{
cout << "群消息[" << js["groupid"] << "]:" << js["time"].get<string>() << " [" << js["id"] << "]" << js["name"].get<string>()
<< " said: " << js["msg"].get<string>() << endl;
continue;
}

}
}

6.新增

1.加入了全局变量isMainMenuRunning表示用户是否在线,退出登录后就变为false,在线就是true

2.登陆成功后好友列表和群组列表加入了初始化部分,删掉了原来存储的数据,不删的话连续同一个用户登录会返回多次列表内容

3.加入readthreadNumber控制接收线程只启动一次,因为用户退出后,该线程会在recv处阻塞,而新用户登录会重新开启一个readThread,那么就相当于有一个线程一直阻塞,浪费资源