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

📄 mailslotclientview.cpp

📁 visual c++网络通信程序开发指南附带的程序所有的源码。
💻 CPP
字号:
// MailslotClientView.cpp : implementation of the CMailslotClientView class
//

#include "stdafx.h"
#include "MailslotClient.h"

#include "MailslotClientDoc.h"
#include "MailslotClientView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMailslotClientView

IMPLEMENT_DYNCREATE(CMailslotClientView, CView)

BEGIN_MESSAGE_MAP(CMailslotClientView, CView)
	//{{AFX_MSG_MAP(CMailslotClientView)
	ON_COMMAND(ID_LINK, OnLink)
	ON_COMMAND(ID_SEND, OnSend)
	ON_COMMAND(ID_CLOSE, OnClose)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMailslotClientView construction/destruction

CMailslotClientView::CMailslotClientView()
{
	// TODO: add construction code here

}

CMailslotClientView::~CMailslotClientView()
{
}

BOOL CMailslotClientView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CMailslotClientView drawing

void CMailslotClientView::OnDraw(CDC* pDC)
{
	CMailslotClientDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	pDC->TextOut(10, 10, m_sMessage);
}

/////////////////////////////////////////////////////////////////////////////
// CMailslotClientView printing

BOOL CMailslotClientView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CMailslotClientView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CMailslotClientView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CMailslotClientView diagnostics

#ifdef _DEBUG
void CMailslotClientView::AssertValid() const
{
	CView::AssertValid();
}

void CMailslotClientView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CMailslotClientDoc* CMailslotClientView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMailslotClientDoc)));
	return (CMailslotClientDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMailslotClientView message handlers

void CMailslotClientView::OnLink() 
{
	// 打开邮槽
	m_hMailSlot = CreateFile("\\\\*\\Mailslot\\Test",
							GENERIC_WRITE, FILE_SHARE_READ, 
							NULL, OPEN_EXISTING, 
							FILE_ATTRIBUTE_NORMAL, NULL);					
	if (m_hMailSlot == INVALID_HANDLE_VALUE)
	{
		m_sMessage.Format("打开邮槽失败,失败代码:%d", GetLastError());
		Invalidate();
		return;
	}
	else
	{
		m_sMessage = "成功打开邮槽!";
		Invalidate();
	}
}

void CMailslotClientView::OnSend() 
{
	// 要发送的数据
	CString sMessage = "Hello World!";

	// 实际发送出去的字节数
	DWORD dwBytesWritten;

	// 向邮槽写入数据
	int ret = WriteFile(m_hMailSlot, sMessage, sMessage.GetLength(), &dwBytesWritten, NULL);
	if (ret == 0)
	{
		m_sMessage.Format("发送数据失败,失败代码:%d", GetLastError());
		Invalidate();
	}
	else
	{
		m_sMessage.Format("成功发送数据,数据长度:%d字节", dwBytesWritten);
		Invalidate();
	}
}

void CMailslotClientView::OnClose() 
{
	// 关闭邮槽句柄
	CloseHandle(m_hMailSlot);	

	m_sMessage = "关闭与服务器的连接";
	Invalidate();
}

⌨️ 快捷键说明

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