clientsocket.cpp

来自「利用CSocket类编写的一个网络聊天程序,加深对CSocket类的基本应用的认」· C++ 代码 · 共 87 行

CPP
87
字号
// ClientSocket.cpp : implementation file
//

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

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

/////////////////////////////////////////////////////////////////////////////
// CClientSocket
IMPLEMENT_DYNAMIC(CClientSocket, CSocket)
CClientSocket::CClientSocket()
{
}

CClientSocket::~CClientSocket()
{
	if (m_pArchiveOut != NULL)
		delete m_pArchiveOut;

	if (m_pArchiveIn != NULL)
		delete m_pArchiveIn;

	if (m_pFile != NULL)
		delete m_pFile;
}


// 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
CClientSocket::CClientSocket(CChatServerDoc* pDoc)
{
	m_pDoc = pDoc;
	m_nMsgCount = 0;
	m_pFile = NULL;
	m_pArchiveIn = NULL;
	m_pArchiveOut = NULL;

}

void CClientSocket::Init()
{
	m_pFile = new CSocketFile(this);
	m_pArchiveIn = new CArchive(m_pFile,CArchive::load);
	m_pArchiveOut = new CArchive(m_pFile,CArchive::store);
}

void CClientSocket::SendMsg(CMsg* pMsg)
{
	if (m_pArchiveOut != NULL)
	{
		//数据由pMsg输出到m_pArchiveOut
		pMsg->Serialize(*m_pArchiveOut);
		//将数据写入文件
		m_pArchiveOut->Flush();
	}
}

void CClientSocket::ReceiveMsg(CMsg* pMsg)
{
	//数据由m_pArchiveIn读入到pMsg
	pMsg->Serialize(*m_pArchiveIn);
}

void CClientSocket::OnReceive(int nErrorCode)
{
	//调用CSocekt的OnReceive函数
	CSocket::OnReceive(nErrorCode);
	//其余工作交给文档类处理
	m_pDoc->ProcessReceive(this);
}

⌨️ 快捷键说明

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