-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSeverSocket.cpp
More file actions
317 lines (290 loc) · 8.71 KB
/
Copy pathSeverSocket.cpp
File metadata and controls
317 lines (290 loc) · 8.71 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
// ServerSocket.cpp : implementation file
//
#include "stdafx.h"
#include "ChatRoomServer.h"
#include "ServerSocket.h"
#include "ChatRoomServer.h"
#include "ChatRoomServerDlg.h"
#include "Message.h"
#include <fstream>
using namespace std;
// ServerSocket
ServerSocket::ServerSocket()
:m_UserName(_T(""))
{
}
ServerSocket::~ServerSocket()
{
}
void ServerSocket::OnAccept(int nErrorCode)
{
//收到到一个连接请求
ServerSocket* toClientSocket;
toClientSocket = new ServerSocket();
toClientSocket->m_ptoList = &this->m_pList;
Accept(*toClientSocket);
m_pList.AddTail(toClientSocket);
CSocket::OnAccept(nErrorCode);
}
//接收客户端发来的消息
void ServerSocket::OnReceive(int nErrorCode)
{
char *pMsg = new char[sizeof(Message)]; //申请和Message同等大小的字符串空间并用pMsg指向它
if (!pMsg)
{
TRACE0("ServerSocket::OnReceive 内存不足!");
return;
}
memset(pMsg, 0, sizeof(Message));
Receive(pMsg, sizeof(Message)); //接收消息到pMsg中
Message* rcvMsg = (Message*)pMsg;
switch (rcvMsg->type) //对不同类型的消息做不同的处理
{
case MSG_REGISTER: //注册消息
{
this->OnRegister(rcvMsg);
break;
}
case MSG_FINDPSW: //找回密码消息
{
this->OnFindPassword(rcvMsg);
break;
}
case MSG_LOGIN:
{
this->OnLogin(rcvMsg); //处理登录消息
((CChatRoomServerDlg*)theApp.m_pMainWnd)->UpdateServerList(); //更新用户列表
break;
}
case MSG_OFFLINE:
{
Message* offlinemsg = new Message;
*offlinemsg = *rcvMsg;
theApp.offlineMessage.AddTail(offlinemsg);
break;
}
default: break;
}
delete pMsg;
CSocket::OnReceive(nErrorCode);
}
void ServerSocket::SaveUserinfoToFile() //保存用户信息到文件中
{
fstream fp("./UserInfo.txt", ios::out | ios::binary);
if (!fp)
{
AfxMessageBox(_T("打开用户信息文件失败!"));
return;
}
POSITION ps = UserInfoList.GetHeadPosition();
while (ps != NULL)
{
UserInfo* pTemp = (UserInfo*)UserInfoList.GetNext(ps);
fp.write((char*)pTemp, sizeof(UserInfo));
}
fp.close();
}
void ServerSocket::OnRegister(Message* rcvMsg)
{
Message* sendMsg = new Message;
memset(sendMsg, 0, sizeof(Message));
POSITION ps = UserInfoList.GetHeadPosition();
while (ps != NULL)
{
UserInfo* pTemp = (UserInfo*)UserInfoList.GetNext(ps);
if ((CString)(pTemp->username) == rcvMsg->data.regMsg.userName) //判断用户名是否已存在
{
//若用户名已存在
sendMsg->type = MSG_REGFAIL;
Send(sendMsg, sizeof(Message)); //发送注册失败的信息
delete sendMsg;
return;
}
}
//若用户名不存在
UserInfo* newUser = new UserInfo;
memset(newUser, 0, sizeof(UserInfo));
strcpy_s(newUser->username, 20, rcvMsg->data.regMsg.userName);
strcpy_s(newUser->password, 40, rcvMsg->data.regMsg.password);
strcpy_s(newUser->answer, 40, rcvMsg->data.regMsg.answer);
newUser->userIndex = UserInfoList.GetCount();
UserInfoList.AddTail(newUser); //注册成功,加入新用户信息到用户信息
this->SaveUserinfoToFile();
sendMsg->type = MSG_REGSUCCESS;
Send(sendMsg, sizeof(Message)); //发送注册成功的信息
delete sendMsg;
CTime time;
time = CTime::GetCurrentTime();
CString strTime = time.Format("%Y-%m-%d %H:%M:%S ");
strTime = strTime + _T(" 用户名为 ") + newUser->username + _T(" 的新用户注册了...\r\n");
((CChatRoomServerDlg*)theApp.GetMainWnd())->DisplayLog(strTime);
return;
}
void ServerSocket::OnFindPassword(Message* rcvMsg) //处理找回密码消息
{
Message* sendMsg = new Message;
memset(sendMsg, 0, sizeof(Message));
POSITION ps = UserInfoList.GetHeadPosition();
while (ps != NULL)
{
UserInfo* pTemp = (UserInfo*)UserInfoList.GetNext(ps);
if ((CString)(pTemp->username) == rcvMsg->data.regMsg.userName) //找到对应用户信息
{
if (rcvMsg->data.findpswMsg.if_success == 2) //返回修改成功的消息
{
strcpy_s(pTemp->password, 40, rcvMsg->data.findpswMsg.password);
sendMsg->type = MSG_FINDPSW;
sendMsg->data.findpswMsg.if_success = 3;
Send(sendMsg, sizeof(Message));
delete sendMsg;
CTime time;
time = CTime::GetCurrentTime();
CString strTime = time.Format("%Y-%m-%d %H:%M:%S ");
strTime = strTime + rcvMsg->data.findpswMsg.userName + _T(" 用户修改了密码...\r\n");
((CChatRoomServerDlg*)theApp.GetMainWnd())->DisplayLog(strTime);
this->SaveUserinfoToFile();
return;
}
else if ((CString)(pTemp->answer) == rcvMsg->data.regMsg.answer)
{
if (rcvMsg->data.findpswMsg.if_success == 0) //返回提示修改密码的消息
{
sendMsg->type = MSG_FINDPSW;
strcpy_s(sendMsg->data.findpswMsg.userName, 20, rcvMsg->data.findpswMsg.userName);
sendMsg->data.findpswMsg.if_success = 1;
Send(sendMsg, sizeof(Message));
delete sendMsg;
return;
}
}
else
{
sendMsg->type = MSG_FINDPSW; //密保答案不正确
sendMsg->data.findpswMsg.if_success = 0;
Send(sendMsg, sizeof(Message));
delete sendMsg;
return;
}
}
}
//用户信息列表中不存在该用户名
sendMsg->type = MSG_FINDPSW;
sendMsg->data.findpswMsg.if_success = 0;
Send(sendMsg, sizeof(Message));
delete sendMsg;
return;
}
void ServerSocket::UpdateClientList() //更新客户端用户列表
{
Message* sendMsg = new Message;
memset(sendMsg, 0, sizeof(Message));
int userNum = UserInfoList.GetCount();
int n = 0;
OtheruserInfo* userInfo = new OtheruserInfo[userNum]; //创建记录所有用户信息的数组,包括用户名,是否在线,对方的监听IP和端口号
POSITION ps = UserInfoList.GetHeadPosition();
while (ps != NULL)
{
UserInfo* pTemp = (UserInfo*)UserInfoList.GetNext(ps);
strcpy_s(userInfo[n].username, 20, pTemp->username);
userInfo[n].online = pTemp->online;
userInfo[n].addr = pTemp->addr;
userInfo[n].fileaddr = pTemp->fileaddr;
n++;
}
sendMsg->type = MSG_UPDATE;
sendMsg->data.userinfoMsg.count = n;
memcpy_s(sendMsg->data.userinfoMsg.userinfo, sizeof(OtheruserInfo)*n, userInfo, sizeof(OtheruserInfo)*n);
ServerSocket* pSocket;
ps = m_ptoList->GetHeadPosition();
while (ps != NULL)
{
pSocket = (ServerSocket*)UserInfoList.GetNext(ps);
pSocket->Send(sendMsg, sizeof(Message));
}
delete userInfo;
userInfo = 0;
delete sendMsg;
sendMsg = 0;
}
//用户登录
void ServerSocket::OnLogin(Message* rcvMsg)
{
Message* sendMsg = new Message;
memset(sendMsg, 0, sizeof(Message));
sendMsg->type = MSG_LOGIN;
POSITION ps = UserInfoList.GetHeadPosition();
while (ps != NULL)
{
UserInfo* pTemp = (UserInfo*)UserInfoList.GetNext(ps);
if ((CString)(pTemp->username) == rcvMsg->data.regMsg.userName) //找到对应用户信息
{
if ((CString)(pTemp->password) == rcvMsg->data.regMsg.password) //密码正确,登录成功
{
pTemp->online = 1; //更新用户信息的登录状态
pTemp->addr = rcvMsg->data.loginMsg.addr; //更新用户信息的监听地址
pTemp->fileaddr = rcvMsg->data.loginMsg.fileaddr;
sendMsg->data.loginMsg.if_success = 1;
strcpy_s(sendMsg->data.loginMsg.userName, 20, rcvMsg->data.regMsg.userName);
this->Send(sendMsg, sizeof(Message)); //向客户端发送登录成功信息
UpdateClientList(); //向客户端发送用户在线情况信息
this->m_UserName = rcvMsg->data.regMsg.userName;
CTime time; //在服务器上显示用户登录信息
time = CTime::GetCurrentTime();
CString strTime = time.Format("%Y-%m-%d %H:%M:%S ");
strTime = strTime + this->m_UserName + _T(" 用户登录...\r\n");
((CChatRoomServerDlg*)theApp.GetMainWnd())->DisplayLog(strTime);
((CChatRoomServerDlg*)theApp.m_pMainWnd)->UpdateServerList(); //更新服务器在线用户列表
POSITION ps = theApp.offlineMessage.GetHeadPosition(); //检查是否有送往该用户的离线消息
POSITION pstemp;
Message* pTemp;
while (ps != NULL)
{
pstemp = ps;
pTemp = (Message*)theApp.offlineMessage.GetNext(ps);
if ((CString)rcvMsg->data.loginMsg.userName == pTemp->to_user)
{
Send(pTemp, sizeof(Message));
theApp.offlineMessage.RemoveAt(pstemp);
}
}
return;
}
else
{
sendMsg->data.loginMsg.if_success = 0;
this->Send(sendMsg, sizeof(Message)); //密码错误,登录失败
return;
}
}
}
sendMsg->data.loginMsg.if_success = 0;
this->Send(sendMsg, sizeof(Message)); //用户名不存在,登录失败
return;
}
//用户下线
void ServerSocket::OnClose(int nErrorCode)
{
CTime time;
time = CTime::GetCurrentTime();
CString strTime = time.Format("%Y-%m-%d %H:%M:%S ");
strTime = strTime + this->m_UserName + _T(" 用户离开...\r\n");
((CChatRoomServerDlg*)theApp.GetMainWnd())->DisplayLog(strTime);
POSITION pss = m_ptoList->Find(this);
m_ptoList->RemoveAt(pss);
POSITION ps = UserInfoList.GetHeadPosition();
while (ps != NULL)
{
UserInfo* pTemp = (UserInfo*)UserInfoList.GetNext(ps);
if ((CString)(pTemp->username) == this->m_UserName)
{
pTemp->online = 0; //用户信息列表中该用户更新为不在线
}
}
//通知客户端刷新在线名单
this->UpdateClientList();
((CChatRoomServerDlg*)theApp.m_pMainWnd)->UpdateServerList();
this->Close();
//销毁该套接字
delete this;
CSocket::OnClose(nErrorCode);
}