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

📄 socket.cpp

📁 聊天的
💻 CPP
字号:
// Socket.cpp : implementation file
//

#include "stdafx.h"
#include "Meeting.h"
#include "Socket.h"
#include "MeetingDoc.h"
#include "Shape.h"

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

/////////////////////////////////////////////////////////////////////////////
// CListenSocket

CListenSocket::CListenSocket()
{

}

CListenSocket::CListenSocket(CMeetingDoc* pDoc)
{
	m_pDoc=pDoc;
}

CListenSocket::~CListenSocket()
{
}


// Do not edit the following lines, which are needed by ClassWizard.
#if 0
BEGIN_MESSAGE_MAP(CListenSocket, CSocket)
	//{{AFX_MSG_MAP(CListenSocket)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#endif	// 0

/////////////////////////////////////////////////////////////////////////////
// CListenSocket member functions
/////////////////////////////////////////////////////////////////////////////
// CClientSocket

CClientSocket::CClientSocket()
{
}
CClientSocket::CClientSocket(CMeetingDoc* pDoc)
:m_pFile(NULL)
, m_pArchiveIn(NULL)
, m_pArchiveOut(NULL)
{
	m_pDoc=pDoc;
}

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

BOOL CClientSocket::Initialize()
{

	m_pFile=new CSocketFile(this);
	m_pArchiveIn=new CArchive(m_pFile,CArchive::load);
	m_pArchiveOut=new CArchive(m_pFile,CArchive::store);
	return TRUE;
}

void CListenSocket::OnAccept(int nErrorCode) 
{
	m_pDoc->AcceptClient();

	CSocket::OnAccept(nErrorCode);
}

void CListenSocket::OnReceive(int nErrorCode) 
{
	// TODO: Add your specialized code here and/or call the base class
	
	CSocket::OnReceive(nErrorCode);
}

void CClientSocket::OnReceive(int nErrorCode) 
{
	m_pDoc->ReceiveData(this);
	
	CSocket::OnReceive(nErrorCode);
}

void CListenSocket::OnClose(int nErrorCode) 
{
	CSocket::OnClose(nErrorCode);
}
BOOL CClientSocket::SendShape(CShape* &pShape)
{
	if(!m_pArchiveOut==NULL)
	{
       BYTE bValue = (BYTE)(rand()%32000);
       WORD nCopies = (WORD)(rand()%8);
       *m_pArchiveOut << bValue << nCopies;
		for(int p = 0; p < nCopies; p++)
        {
                // Send data
                *m_pArchiveOut << bValue;
		}
		*m_pArchiveOut<<pShape;
		m_pArchiveOut->Flush();
		return true;
	}
	return false;

}

BOOL CClientSocket::ReceiveShape(CShape *&pShape)
{
	
	if(!m_pArchiveIn==NULL)
	{
		BYTE bCheck,bValue;
        WORD nCopies;
            // Receive header information
            *m_pArchiveIn >> bCheck >> nCopies;

            for(int c = 0; c < nCopies; c++)
            {
                // Receive data
                *m_pArchiveIn >> bValue;
                if (bCheck != bValue)
					return false;
                    //AfxMessageBox("通迅不稳定,数据丢失!");
            }



//		pMsg->Serialize(*m_pArchiveIn);
//		CArchive& ar=(CArchive)*m_pArchiveIn;

		*m_pArchiveIn>>pShape;
		return true;
	}
	return false;
}

void CClientSocket::OnClose(int nErrorCode) 
{
	m_pDoc->CloseSocket(this);

	CSocket::OnClose(nErrorCode);
}

⌨️ 快捷键说明

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