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

📄 e05doc.cpp

📁 这是书上的关于聊天的程序
💻 CPP
字号:
// E05Doc.cpp : implementation of the CE05Doc class
//

#include "stdafx.h"
#include "E05.h"
#include "SetupDlg.h"
#include "E05Doc.h"
#include "E05View.h"

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

/////////////////////////////////////////////////////////////////////////////
// CE05Doc

IMPLEMENT_DYNCREATE(CE05Doc, CDocument)

BEGIN_MESSAGE_MAP(CE05Doc, CDocument)
	//{{AFX_MSG_MAP(CE05Doc)
	ON_COMMAND(ID_FILE_CONNECT, OnFileConnect)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CE05Doc construction/destruction

CE05Doc::CE05Doc()
{
	// TODO: add one-time construction code here
	m_socket=NULL;
}

CE05Doc::~CE05Doc()
{
}

BOOL CE05Doc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	((CEditView*)m_viewList.GetHead())->SetWindowText(NULL);

	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)

	return TRUE;
}



/////////////////////////////////////////////////////////////////////////////
// CE05Doc serialization

void CE05Doc::Serialize(CArchive& ar)
{
	// CEditView contains an edit control which handles all serialization
	((CEditView*)m_viewList.GetHead())->SerializeRaw(ar);
}

/////////////////////////////////////////////////////////////////////////////
// CE05Doc diagnostics

#ifdef _DEBUG
void CE05Doc::AssertValid() const
{
	CDocument::AssertValid();
}

void CE05Doc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CE05Doc commands

void CE05Doc::OnFileConnect() 
{
	// TODO: Add your command handler code here
	CSetupDlg dlg;
	if (dlg.DoModal()!=IDOK) return;

	if (m_socket!=NULL)
	{
		m_socket->Close();
		delete m_socket;
	}
	
	m_socket=new CChatSocket(this);
	if (!m_socket->Create())
	{
		AfxMessageBox("创建Socket失败!");
		delete m_socket;
		m_socket=NULL;
		return;
	}
	if (!m_socket->Connect(dlg.m_address,dlg.m_port+BASE_CHANNEL))
	{
		AfxMessageBox("连接服务器失败!");
		delete m_socket;
		m_socket=NULL;
		return;
	}
	AfxMessageBox("成功连接到聊天服务器!");
}

void CE05Doc::ReceiveMessage()
{
	ASSERT(m_socket);
	char buffer[MAX_BUFFER_SIZE];
	int len=m_socket->Receive(buffer,MAX_BUFFER_SIZE-1);
	if (len<1)
	{
		AfxMessageBox("接收信息异常!");
		return;
	}
	buffer[len]=0;
	ShowMessage(buffer);
}

void CE05Doc::ShowMessage(LPSTR msg)
{
	POSITION pos=GetFirstViewPosition();
	while (pos!=NULL)
	{
		CEditView* view=(CEditView*)GetNextView(pos);
		if (view->IsKindOf(RUNTIME_CLASS(CE05View))) continue;
		CEdit& edit=view->GetEditCtrl();

		char buffer[MAX_BUFFER_SIZE];
		wsprintf(buffer,"%s\r\n",msg);

		int len=edit.GetWindowTextLength();
		edit.SetSel(len,len);
		edit.ReplaceSel(buffer);
	}
}

void CE05Doc::SendMessage(LPSTR msg)
{
	if (m_socket==NULL) return;
	if (!m_socket->Send(msg,strlen(msg)))
	{
		AfxMessageBox("发送消息失败!");
		return;
	}
}

⌨️ 快捷键说明

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