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

📄 serverview.cpp

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

#include "stdafx.h"
#include "Server.h"

#include "ServerDoc.h"
#include "ServerView.h"

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

UINT ReadProc(LPVOID lpVoid)
{
	// 数据缓存
	char buffer[1024];
	DWORD ReadNum;

	// 获取视句柄
	CServerView* pView = (CServerView*)lpVoid;

	// 等待客户机的连接
	if (ConnectNamedPipe(pView->m_hPipe, NULL) == FALSE)
	{
		// 关闭管道句柄
		CloseHandle(pView->m_hPipe);

		// 显示信息
		pView->m_sMessage = "与客户机建立连接失败!";
		pView->Invalidate();
		return 0;
	}
	else
	{
		// 显示信息
		pView->m_sMessage = "与客户机建立连接!";
		pView->Invalidate();
	}

	// 从管道读取数据
	if (ReadFile(pView->m_hPipe, buffer, sizeof(buffer), &ReadNum, NULL) == FALSE)
	{
		// 关闭管道句柄
		CloseHandle(pView->m_hPipe);

		// 显示信息
		pView->m_sMessage = "从管道读取数据失败!";
		pView->Invalidate();
	}
	else
	{
		// 显示接收到的信息
		buffer[ReadNum] = '\0';
		pView->m_sMessage = CString(buffer);
		pView->Invalidate();
	}

	return 1;
}

/////////////////////////////////////////////////////////////////////////////
// CServerView

IMPLEMENT_DYNCREATE(CServerView, CView)

BEGIN_MESSAGE_MAP(CServerView, CView)
	//{{AFX_MSG_MAP(CServerView)
	ON_COMMAND(ID_START, OnStart)
	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()

/////////////////////////////////////////////////////////////////////////////
// CServerView construction/destruction

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

}

CServerView::~CServerView()
{
}

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

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CServerView drawing

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

/////////////////////////////////////////////////////////////////////////////
// CServerView printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CServerView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CServerView message handlers

void CServerView::OnStart() 
{
	// 创建命名管道
	m_hPipe = CreateNamedPipe("\\\\.\\Pipe\\Test", PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, 1, 0, 0, 1000, NULL);
	if (m_hPipe == INVALID_HANDLE_VALUE)
		m_sMessage = "创建命名管道失败!";
	else
	{
		m_sMessage = "成功创建命名管道!";
		
		// 开启线程
		AfxBeginThread(ReadProc, this);
	}

	// 更新信息显示
	Invalidate();
}

void CServerView::OnClose() 
{
	// 终止连接
	if (DisconnectNamedPipe(m_hPipe) == FALSE)
		m_sMessage = "终止连接失败!";
	else
	{
		// 关闭管道句柄
		CloseHandle(m_hPipe);

		m_sMessage = "成功终止连接!";
	}

	// 更新信息显示
	Invalidate();
}

⌨️ 快捷键说明

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