进程间通信

1、邮槽

 

#include <Windows.h>
#include <stdio.h>
#include <iostream>
using namespace std;

/*
  邮槽  服务端
  注意: 先启动服务端


  邮槽是一种windows系统提供的一种单项通信的机制,单工,邮槽传输的数据非常小,一般在400kb
  如果数据过大,可能导致邮槽不能正常工作
*/


int main()
{

    HANDLE mail2;
    char text[]= "你好,this is a mailslot message test";
    DWORD writeText;
    mail2 = CreateFile("\\\\.\\mailslot\\myslot",GENERIC_WRITE,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
    if(INVALID_HANDLE_VALUE == mail2)
    {
        cout<<"打开邮槽失败!"<<endl;
    }else
    {
        if(WriteFile(mail2,text,sizeof(text),&writeText,NULL))
        {
            ::Sleep(1000);
            cout<<"数据写入成功!"<<endl;
        }else
        {
            cout<<"数据写入失败!"<<endl;
        }
        CloseHandle(mail2);
    }

    ::Sleep(10000);
    return 0;
}
#include <Windows.h>
#include <stdio.h>
#include <iostream>
using namespace std;
/*
  邮槽  客户端
*/

int main()
{

    HANDLE mail;
    mail = ::CreateMailslot("\\\\.\\mailslot\\myslot",0,MAILSLOT_WAIT_FOREVER,NULL); //定义邮槽句柄
    if(mail == INVALID_HANDLE_VALUE)
    {
        cout<<"创建邮槽失败!"<<endl;
        CloseHandle(mail);
    }else
    {
        cout<<"创建邮槽成功!,正在读取数据...."<<endl;
        char szText[200];
        DWORD readText;
        if(ReadFile(mail,szText,200,&readText,NULL)) //读取数据
        {
            cout<<szText<<endl;
        }else
        {
            cout<<"读取数据失败"<<endl;
        }

    }
    
    CloseHandle(mail); //关闭邮槽
    Sleep(10000);

    return 0;
}

 

2、命名管道

#include <Windows.h>
#include <stdio.h>
#include <iostream>
using namespace std;

int main()
{
    HANDLE hPip;          //命名管道句柄
    OVERLAPPED ovi = {0}; //定义结构体变量
    char buf[200];
    DWORD readbuf;
    hPip = CreateNamedPipe("\\\\.\\pipe\pipename",PIPE_ACCESS_DUPLEX,
        PIPE_TYPE_BYTE,PIPE_UNLIMITED_INSTANCES,1024,1024,0,NULL);
    cout<<"创建管道成功,正在等待客户端连接!"<<endl;
    if(::ConnectNamedPipe(hPip,&ovi)) //阻塞式?
    {
        cout<<"客户端连接成功"<<endl;
        cout<<"正在读取数据..."<<endl;
        if(ReadFile(hPip,buf,200,&readbuf,NULL))
        {
            cout<<"数据读取成功"<<endl;
            cout<<"读取的数据是: "<<buf<<endl;
        }else
        {
            cout<<"数据读取失败!"<<endl;
        }
    }


    cin.get();    

    return 0;
}
#include <Windows.h>
#include <stdio.h>
#include <iostream>
using namespace std;

int main()
{
    HANDLE hPip;
    OVERLAPPED ovi = {0}; //初始化结构体
    ZeroMemory(&ovi,sizeof(OVERLAPPED));
    char buf[] = "命名管道测试程序";
    DWORD readbuf;
    cout<<"正在连接命名管道!"<<endl;

    if(WaitNamedPipe("\\\\.\\pipe\\pipename",NMPWAIT_WAIT_FOREVER)) //连接命名管道
    {
        hPip = CreateFile("\\\\.\\pipe\\pipename",GENERIC_READ|GENERIC_WRITE,0,
            NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);  //打开指定命名管道
        if(hPip == INVALID_HANDLE_VALUE)
        {
            cout<<"打开命名管道失败!"<<endl;
        }else
        {
            //写入数据到命名管道
            if(WriteFile(hPip,buf,sizeof(buf),&readbuf,NULL))
            {
                cout<<"数据写入成功!"<<endl;
            }else
            {
                cout<<"数据写入失败!"<<endl;
            }
        }
    }else
    {
        cout<<"连接命名管道失败!"<<endl;
    }


    cin.get();    
    return 0;
}
posted @ 2015-12-31 18:20  hongjack  阅读(201)  评论(0编辑  收藏  举报