📄 clientsocket.cpp
字号:
// ClientSocket.cpp : implementation file
//
#include "stdafx.h"
#include "TCPChatServer.h"
#include "ClientSocket.h"
#include "tagHeader.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CClientSocket
CClientSocket::CClientSocket(CPtrList *pPrtList) : m_dlgServer(NULL)
{
m_pPtrlist = pPrtList;
}
CClientSocket::~CClientSocket()
{
}
// Do not edit the following lines, which are needed by ClassWizard.
#if 0
BEGIN_MESSAGE_MAP(CClientSocket, CSocket)
//{{AFX_MSG_MAP(CClientSocket)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#endif // 0
/////////////////////////////////////////////////////////////////////////////
// CClientSocket member functions
void CClientSocket::OnReceive(int nErrorCode)
{
// TODO: Add your specialized code here and/or call the base class
char buff1[sizeof(Header)];
memset(buff1, 0, sizeof(buff1));
Receive(buff1,sizeof(buff1));
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);
CTime time = CTime::GetCurrentTime();
CString t = time.Format(_T("%Y-%m-%d %H:%M:%S"));
CEdit *p_Edit = (CEdit *)::AfxGetMainWnd()->GetDlgItem(IDC_EDIT_INFO);
CString csTemp = t + _T(": ") + CString(buff) + _T(" 进入聊天室\r\n");
p_Edit->ReplaceSel(csTemp);
m_csNickName = buff;
m_dlgServer->UpdateUser(this);
}
if (type == SEND_MESSAGE)
{
char buff[1000];
memset(buff,0,sizeof(buff));
Receive(buff,sizeof(buff));
CClientSocket *curr = NULL;
POSITION pos = m_pPtrlist->GetHeadPosition();
while (pos != NULL)
{
curr = (CClientSocket *)m_pPtrlist->GetNext(pos);
curr->Send((char *)header,sizeof(Header));
curr->Send(buff, sizeof(buff));
}
}
CSocket::OnReceive(nErrorCode);
}
void CClientSocket::OnClose(int nErrorCode)
{
// TODO: Add your specialized code here and/or call the base class
POSITION pos = m_pPtrlist->Find(this);
if (pos != NULL)
{
m_pPtrlist->RemoveAt(pos);
CTime time = CTime::GetCurrentTime();
CString t = time.Format(_T("%Y-%m-%d %H:%M:%S"));
CEdit *p_Edit = (CEdit *)m_dlgServer->GetDlgItem(IDC_EDIT_INFO);
CString csTemp = t + _T(": ") + this->m_csNickName + _T(" 离开聊天室\r\n");
p_Edit->ReplaceSel(csTemp);
m_dlgServer->UpdateUser(this);
this->Close();
delete this;
}
CSocket::OnClose(nErrorCode);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -