zoukankan      html  css  js  c++  java
  • 进程间通信:邮槽

    邮槽是基于广播通信体系设计出来的,它彩用无连接的不可靠的数据传输。
    邮槽是一种单向通信机制,创建邮槽的服务器进程读取数据,打开邮槽的客户机进程入数据。

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

      

  • 相关阅读:
    关于Delphi中RS Float字段只有4位及OADateTime不能显示到秒的解决办法
    xml特殊字符处理(js)
    总结一下最近将163邮箱拖动效果改成兼容Firefox遇到的问题
    在XIB里面关于@property,@synthesize,release,dealloc的怪现象
    关于Core Data里面删除了一个被retain的NSManagedObject
    Hello cnblogs' world!
    html5离线应用程序 Amy
    css3布局相关的样式 Amy
    html5拖放API Amy
    css3MediaQueries的相关样式 Amy
  • 原文地址:https://www.cnblogs.com/wucg/p/2421390.html
Copyright © 2011-2022 走看看