zoukankan      html  css  js  c++  java
  • 命名管道跨进程通讯

    客户端代码:

    #include "stdafx.h"  
    #include <stdio.h>  
    #include <windows.h>  
    #include <ctime>  
      
    int main(int argc, _TCHAR* argv[])  
    {  
        srand(time(NULL));  
      
        DWORD wlen = 0;  
        Sleep(1000);//等待pipe的创建成功!  
      
        BOOL bRet = WaitNamedPipe(TEXT("\\.\Pipe\mypipe"), NMPWAIT_WAIT_FOREVER);  
      
        if (!bRet)  
        {  
            printf("connect the namedPipe failed!
    ");  
            return 0;  
        }  
      
        HANDLE hPipe = CreateFile(          //管道属于一种特殊的文件  
            TEXT("\\.\Pipe\mypipe"),    //创建的文件名  
            GENERIC_READ | GENERIC_WRITE,   //文件模式  
            0,                              //是否共享  
            NULL,                           //指向一个SECURITY_ATTRIBUTES结构的指针  
            OPEN_EXISTING,                  //创建参数  
            FILE_ATTRIBUTE_NORMAL,          //文件属性(隐藏,只读)NORMAL为默认属性  
            NULL);                          //模板创建文件的句柄  
      
        if (INVALID_HANDLE_VALUE == hPipe)  
        {  
            printf("open the exit pipe failed!
    ");  
        }  
        else  
        {  
            while(true)  
            {  
                char buf[256] = "";  
                sprintf(buf,"%s%d",buf,rand()%1000);  
                if(WriteFile(hPipe,buf,sizeof(buf),&wlen,0)==FALSE) //向服务器发送内容  
                {  
                    printf("write to pipe failed!
    ");  
                    break;  
                }  
                else  
                {  
                    printf("To Server: data = %s, size = %d
    ", buf, wlen);  
                    char rbuf[256] = "";  
                    DWORD rlen = 0;  
                    ReadFile(hPipe, rbuf, sizeof(rbuf), &rlen, 0);  //接受服务发送过来的内容  
                    printf("From Server: data = %s, size = %d
    ", rbuf, rlen);  
                }  
                Sleep(1000);  
            }  
            CloseHandle(hPipe);//关闭管道  
        }  
      
        system("pause");  
        return 0;  
    }  
    

    服务端代码:

    #include "stdafx.h"  
    #include <stdio.h>  
    #include <windows.h>  
    #include <ctime>  
      
    int main(int argc, _TCHAR* argv[])  
    {  
        srand(time(NULL));  
      
        char buf[256] = "";  
        DWORD rlen = 0;  
        HANDLE hPipe = CreateNamedPipe(  
            TEXT("\\.\Pipe\mypipe"),                        //管道名  
            PIPE_ACCESS_DUPLEX,                                 //管道类型   
            PIPE_TYPE_MESSAGE|PIPE_READMODE_MESSAGE|PIPE_WAIT,  //管道参数  
            PIPE_UNLIMITED_INSTANCES,                           //管道能创建的最大实例数量  
            0,                                                  //输出缓冲区长度 0表示默认  
            0,                                                  //输入缓冲区长度 0表示默认  
            NMPWAIT_WAIT_FOREVER,                               //超时时间  
            NULL);                                              //指定一个SECURITY_ATTRIBUTES结构,或者传递零值  
      
        if (INVALID_HANDLE_VALUE == hPipe)  
        {  
            printf("Create Pipe Error(%d)
    ",GetLastError());  
        }  
        else  
        {  
            printf("Waiting For Client Connection...
    ");  
      
            if(!ConnectNamedPipe(hPipe, NULL))  //阻塞等待客户端连接。  
            {  
                printf("Connection failed!
    ");  
            }  
            else  
            {  
                printf("Connection Success!
    ");  
            }  
      
            while (true)  
            {  
                if(!ReadFile(hPipe,buf,256,&rlen,NULL)) //接受客户端发送过来的内容  
                {             
                    printf("Read Data From Pipe Failed!
    ");  
                    break;  
                }  
                else  
                {  
                    printf("From Client: data = %s, size = %d
    ", buf, rlen);  
                      
                    char wbuf[256] = "";  
                    sprintf(wbuf, "%s%d", wbuf, rand()%1000);  
                    DWORD wlen = 0;  
                    WriteFile(hPipe, wbuf, sizeof(wbuf), &wlen, 0); //向客户端发送内容  
                    printf("To Client: data = %s, size = %d
    ", wbuf, wlen);  
                    Sleep(1000);  
                }  
            }  
            FlushFileBuffers(hPipe);   
            DisconnectNamedPipe(hPipe);   
            CloseHandle(hPipe);//关闭管道  
        }  
      
        system("pause");  
        return 0;  
    }  
    

     下篇,写下C# Name Pipe 跨进程通信

  • 相关阅读:
    (五)消费Dubbo服务
    (四)Dubbo Admin管理控制台
    (三)发布Dubbo服务
    (二)zookeeper安装
    (一)Dubbo简介
    解决tomcat 启动 一闪而过
    Redis的数据结构之哈希
    Redis的数据结构之字符串
    Jedis 连接池实例
    linux/centos定时任务cron
  • 原文地址:https://www.cnblogs.com/jackbase/p/7269873.html
Copyright © 2011-2022 走看看