-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientSocket.cpp
More file actions
150 lines (127 loc) · 3.45 KB
/
ClientSocket.cpp
File metadata and controls
150 lines (127 loc) · 3.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
147
148
149
150
// ClientSocket.cpp : 实现文件
//
#include "stdafx.h"
#include "SelfRoom.h"
#include "ClientSocket.h"
//ADD
#include "tagHeader.h"
//ADD
// CClientSocket
CClientSocket::CClientSocket()
{
}
//ADD
CClientSocket::CClientSocket(CPtrList *list) : m_dlgServer(NULL)
{
clist = list;
}
//ADD
CClientSocket::~CClientSocket()
{
}
// CClientSocket 成员函数
void CClientSocket::OnClose(int nErrorCode)
{
// TODO: 在此添加专用代码和/或调用基类
//ADD
POSITION pos = clist->Find(this);
if(pos != NULL)
{
clist->RemoveAt(pos);
CTime time = CTime::GetCurrentTime();
CString t = time.Format("%H:%M:%S");
CEdit *p_Edit = (CEdit *)m_dlgServer->GetDlgItem(IDC_EDIT_INFO);
CString strTemp = t + " " + this->m_strName + " 离开聊天室\r\n";
p_Edit->ReplaceSel(strTemp);
Header head;
head.type = SEND_MESSAGE;
head.len = strTemp.GetLength();
CClientSocket *curr = NULL;
POSITION pos = clist->GetHeadPosition();
while (pos != NULL)
{
curr = (CClientSocket *)clist->GetNext(pos);
curr->Send((char *)&head,sizeof(Header));
curr->Send(strTemp,strTemp.GetLength());
}
m_dlgServer->UpdateUser(this);
this->Close();
delete this;
}
//ADD
CSocket::OnClose(nErrorCode);
}
void CClientSocket::OnReceive(int nErrorCode)
{
// TODO: 在此添加专用代码和/或调用基类
//ADD
char buff1[sizeof(Header)];
memset(buff1, 0, sizeof(buff1));
Receive(buff1,sizeof(buff1));
this->AsyncSelect(FD_CLOSE|FD_READ|FD_WRITE);
Header *header = (Header*)buff1;
int length = header->len;
char type = header->type;
if(type == LOGIN_IO)
{
char buff[1000];
memset(buff,0,sizeof(buff));
Receive(buff,length);
this->AsyncSelect(FD_CLOSE|FD_READ|FD_WRITE);
m_dlgServer->UpdateData();
CTime time = CTime::GetCurrentTime();
CString t = time.Format("%H:%M:%S");
CEdit *p_Edit = (CEdit *)::AfxGetMainWnd()->GetDlgItem(IDC_EDIT_INFO);
CString strTemp = t + " " + CString(buff) + " 进入聊天室\r\n";
p_Edit->ReplaceSel(strTemp);
m_strName = buff;
Header head;
head.type = SEND_MESSAGE;
head.len = strTemp.GetLength();
Header head_history;
head_history.type = SEND_MESSAGE;
m_dlgServer->m_history += m_strName + ", 欢迎您加入!";
head_history.len = m_dlgServer->m_history.GetLength();
CClientSocket *curr = NULL;
POSITION pos = clist->GetHeadPosition();
while (pos != NULL)
{
curr = (CClientSocket *)clist->GetNext(pos);
if(curr->m_strName == m_strName)
{
curr->Send((char *)&head_history,sizeof(Header));
curr->Send(m_dlgServer->m_history,m_dlgServer->m_history.GetLength());
}
else
{
curr->Send((char *)&head,sizeof(Header));
curr->Send(strTemp,strTemp.GetLength());
}
}
m_dlgServer->UpdateUser(this);
}
if(type == SEND_MESSAGE)
{
char buff[1000];
memset(buff,0,sizeof(buff));
Receive(buff,sizeof(buff));
this->AsyncSelect(FD_CLOSE|FD_READ|FD_WRITE);
CTime time = CTime::GetCurrentTime();
CString t = time.Format("%H:%M:%S");
CString nikeName = this->m_strName;
CString strTemp = t + " " + nikeName + " 说: " + CString(buff) + "\r\n";
CString str = nikeName + " " + t + "\r\n" + " " + CString(buff);
CEdit *p_Edit = (CEdit *)::AfxGetMainWnd()->GetDlgItem(IDC_EDIT_INFO);
p_Edit->ReplaceSel(strTemp);
CClientSocket *curr = NULL;
POSITION pos = clist->GetHeadPosition();
while (pos != NULL)
{
curr = (CClientSocket *)clist->GetNext(pos);
curr->Send((char *)header,sizeof(Header));
curr->Send(str,str.GetLength());
}
}
//ADD
CSocket::OnReceive(nErrorCode);
}