⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clientsocket.cpp

📁 一个小型的网络聊天服务器和客户端
💻 CPP
字号:
// ClientSocket.cpp: implementation of the CClientSocket class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "ChatServer.h"
#include "ServerDlg.h"
#include "ClientSocket.h"
#include "msg.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

//构造函数
CClientSocket::CClientSocket(CServerDlg* pDlg)
{
	m_pDlg=pDlg;
	m_nMsgCount=0;
	m_pFile=NULL;
	m_pArchiveIn=NULL;
	m_pArchiveOut=NULL;
}

//初始化
void CClientSocket::Initialize()
{
	//构造相应的CSocketFile对象
	m_pFile=new CSocketFile(this);
	//构造相应的CArchive对象
	m_pArchiveIn=new CArchive(m_pFile,CArchive::load);
	m_pArchiveOut=new CArchive(m_pFile,CArchive::store);
}

//放弃传送
void CClientSocket::Abort()
{
	if (m_pArchiveOut!=NULL)
	{
		m_pArchiveOut->Abort();
		delete m_pArchiveOut;
		m_pArchiveOut=NULL;
	}
}

//发送消息
void CClientSocket::SendMessage(CMsg* pMsg)
{
	if (m_pArchiveOut!=NULL)
	{
		//对消息进行序列化
		pMsg->Serialize(*m_pArchiveOut);
		//将CArchive对象中的数据强制性写入文件中
		m_pArchiveOut->Flush();
	}
}

//接收消息
void CClientSocket::ReceiveMessage(CMsg* pMsg)
{
	//对消息进行序列化
	pMsg->Serialize(*m_pArchiveIn);
}

//OnReceive事件处理函数
void CClientSocket::OnReceive(int nErrorCode)
{
	CSocket::OnReceive(nErrorCode);
	//调用主对话框类中的相应函数处理
	m_pDlg->OnReceive(this);
}

//析构函数
CClientSocket::~CClientSocket()
{
	if (m_pArchiveOut!=NULL)
		delete m_pArchiveOut;
	if (m_pArchiveIn!=NULL)
		delete m_pArchiveIn;
	if (m_pFile!=NULL)
		delete m_pFile;
}

#ifdef _DEBUG

void CClientSocket::AssertValid() const
{
	CSocket::AssertValid();
}

void CClientSocket::Dump(CDumpContext& dc) const
{
	CSocket::Dump(dc);
}

#endif

IMPLEMENT_DYNAMIC(CClientSocket,CSocket)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -