clientsocket.cpp

来自「掌握Visual C++编程,挺基础的」· C++ 代码 · 共 115 行

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

#include "stdafx.h"
#include "MyChatSvr.h"
#include "ClientSocket.h"
#include "MyChatSvrDoc.h"

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

/////////////////////////////////////////////////////////////////////////////
// CClientSocket
CClientSocket::CClientSocket()
{
	m_pDoc = NULL;
}

CClientSocket::CClientSocket(CMyChatSvrDoc* pDoc)
{
	m_pDoc = pDoc;
}


CClientSocket::~CClientSocket()
{
	if(m_pArIn != NULL)
	{
		m_pArIn->Close();
		delete m_pArIn;
		m_pArIn = NULL;
	}
	if(m_pArOut != NULL)
	{
		m_pArOut->Close();
		delete m_pArOut;
		m_pArOut = NULL;
	}
	if(m_pFile != NULL)
	{
		m_pFile->Close();
		delete m_pFile;
		m_pFile = NULL;
	}
}


// 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) 
{
	CSocket::OnReceive(nErrorCode);

//	AfxMessageBox("svr:enter ClientSocket::OnReceive()");
	ASSERT(m_pDoc != NULL);
	m_pDoc->ProcessReceive(this);	
}

void CClientSocket::Init()
{
	m_pFile = new CSocketFile(this);
	m_pArIn = new CArchive(m_pFile, CArchive::load);
	m_pArOut = new CArchive(m_pFile, CArchive::store);
//	Listen();
}

CString CClientSocket::ReadData()
{
//	AfxMessageBox("svr:enter ClientSocket::readdata()");
	CString str;
	*m_pArIn >> str;
	return str;
}

void CClientSocket::SendData(CString strMsg)
{
	*m_pArOut << "Server: " + strMsg;
	m_pArOut->Flush();
}

void CClientSocket::Finish_Comm()
{
	if(m_pArIn != NULL)
	{
		m_pArIn->Close();
		delete m_pArIn;
		m_pArIn = NULL;
	}
	if(m_pArOut != NULL)
	{
		m_pArOut->Close();
		delete m_pArOut;
		m_pArOut = NULL;
	}
	if(m_pFile != NULL)
	{
		m_pFile->Close();
		delete m_pFile;
		m_pFile = NULL;
	}
	Close();
}

⌨️ 快捷键说明

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