프로세스 간 통신: 우체통

6681 단어 통신
우체통은 방송 통신 체계를 바탕으로 설계된 것으로 연결이 없는 신뢰할 수 없는 데이터로 전송된다.우체통은 일종의 단방향 통신 메커니즘으로 우체통을 만드는 서버 프로세스가 데이터를 읽고 우체통을 여는 클라이언트 프로세스가 데이터를 쓴다.
// MailslotSvrView.cpp : implementation of the CMailslotSvrView class

//



#include "stdafx.h"

#include "MailslotSvr.h"



#include "MailslotSvrDoc.h"

#include "MailslotSvrView.h"



#ifdef _DEBUG

#define new DEBUG_NEW

#undef THIS_FILE

static char THIS_FILE[] = __FILE__;

#endif



/////////////////////////////////////////////////////////////////////////////

// CMailslotSvrView



IMPLEMENT_DYNCREATE(CMailslotSvrView, CView)



BEGIN_MESSAGE_MAP(CMailslotSvrView, CView)

	//{{AFX_MSG_MAP(CMailslotSvrView)

	ON_COMMAND(IDM_MAILSLOT_RECV, OnMailslotRecv)

	//}}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()



/////////////////////////////////////////////////////////////////////////////

// CMailslotSvrView construction/destruction



CMailslotSvrView::CMailslotSvrView()

{

	// TODO: add construction code here



}



CMailslotSvrView::~CMailslotSvrView()

{

}



BOOL CMailslotSvrView::PreCreateWindow(CREATESTRUCT& cs)

{

	// TODO: Modify the Window class or styles here by modifying

	//  the CREATESTRUCT cs



	return CView::PreCreateWindow(cs);

}



/////////////////////////////////////////////////////////////////////////////

// CMailslotSvrView drawing



void CMailslotSvrView::OnDraw(CDC* pDC)

{

	CMailslotSvrDoc* pDoc = GetDocument();

	ASSERT_VALID(pDoc);

	// TODO: add draw code for native data here

}



/////////////////////////////////////////////////////////////////////////////

// CMailslotSvrView printing



BOOL CMailslotSvrView::OnPreparePrinting(CPrintInfo* pInfo)

{

	// default preparation

	return DoPreparePrinting(pInfo);

}



void CMailslotSvrView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)

{

	// TODO: add extra initialization before printing

}



void CMailslotSvrView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)

{

	// TODO: add cleanup after printing

}



/////////////////////////////////////////////////////////////////////////////

// CMailslotSvrView diagnostics



#ifdef _DEBUG

void CMailslotSvrView::AssertValid() const

{

	CView::AssertValid();

}



void CMailslotSvrView::Dump(CDumpContext& dc) const

{

	CView::Dump(dc);

}



CMailslotSvrDoc* CMailslotSvrView::GetDocument() // non-debug version is inline

{

	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMailslotSvrDoc)));

	return (CMailslotSvrDoc*)m_pDocument;

}

#endif //_DEBUG



/////////////////////////////////////////////////////////////////////////////

// CMailslotSvrView message handlers



void CMailslotSvrView::OnMailslotRecv() 

{

	// TODO: Add your command handler code here

	HANDLE hMailslot;

	hMailslot = CreateMailslot("\\\\.\\mailslot\\MyMailslot",0,

		MAILSLOT_WAIT_FOREVER,NULL);

	if(INVALID_HANDLE_VALUE == hMailslot)

	{

		MessageBox(" ");

		return;

	}

	char buf[100];

	DWORD dwRead;

	if(!ReadFile(hMailslot,buf,100,&dwRead,NULL))

	{

		MessageBox(" ");

		CloseHandle(hMailslot);

		return;

	}

	MessageBox(buf);

	CloseHandle(hMailslot);

}


  
// 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(IDM_MAILSLOT_SEND, OnMailslotSend)

	//}}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

}



/////////////////////////////////////////////////////////////////////////////

// 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::OnMailslotSend() 

{

	HANDLE hMailslot;

	hMailslot = CreateFile("\\\\.\\mailslot\\MyMailslot",GENERIC_WRITE,

		FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);

	if(INVALID_HANDLE_VALUE == hMailslot)

	{

		MessageBox(" ");

		return;

	}

	char buf[]="http://www.cn";

	DWORD dwWrite;

	if(!WriteFile(hMailslot,buf,strlen(buf)+1,&dwWrite,NULL))

	{

		MessageBox(" ");

		CloseHandle(hMailslot);

		return;

	}

	CloseHandle(hMailslot);

}


좋은 웹페이지 즐겨찾기