码迷,mamicode.com
首页 > Windows程序 > 详细

windows pipe

时间:2015-06-29 17:51:13      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:管道   windows   命名管道   

管道分为 匿名管道命名管道
1.匿名管道只能在父子进程间进行通信,不能在网络间通信,而且数据传输是单向的,只能一端写,另一端读。
2.命令管道可以在任意进程间通信,通信是双向的,任意一端都可读可写,但是在同一时间只能有一端读、一端写。

每一个 命名管道 都有一个唯一的名字以区分于存在于系统的命名对象列表中的其他命名管道。管道服务器在调用CreateNamedPipe()函数创建命名管道的一个或多个实例时为其指定了名称。对于管道客户机,则是在调用CreateFile()或CallNamedPipe()函数以连接一个命名管道实例时对管道名进行指定。命名管道的命名规范与邮槽有些类似,对其标识也是采用的UNC格式:

\\Server\Pipe\[Path]Name 

其中,第一部分\Server指定了服务器的名字,命名管道服务即在此服务器创建,其字串部分可表示为一个小数点(表示本机)、星号(当前网络字段)、域名或是一个真正的服务;第二部分\Pipe与邮槽的\Mailslot一样是一个不可变化的硬编码字串,以指出该文件是从属于NPFS;第三部分[Path]Name则使应用程序可以唯一定义及标识一个命名管道的名字,而且可以设置多级目录。

服务端使用函数:

CreateNamedPipe(); // 创建管道  
ConnectNamedPipe(); // 阻塞,等待客户端连接

客户端使用函数:

CreateFile(); // 打开(连接)管道  

双方共用函数

WriteFile();  
ReadFile(); // 阻塞,使用方便  
CloseHandle(); // 关闭管道,断开连接

服务器端代码示例:

#include <stdio.h>  
#include <windows.h>  

#define PIPE_NAME L"\\\\.\\Pipe\\test"  

HANDLE g_hPipe = INVALID_HANDLE_VALUE;  

int main()  
{  
    char buffer[1024];  
    DWORD WriteNum;  

    printf("test server.\n");  
    g_hPipe = CreateNamedPipe(PIPE_NAME, PIPE_ACCESS_DUPLEX, \  
            PIPE_TYPE_BYTE|PIPE_READMODE_BYTE , 1, 0, 0, 1000, NULL);  
    if(g_hPipe == INVALID_HANDLE_VALUE)  
    {  
        printf("Create name pipe failed!\n");  
        goto out;  
    }  

    printf("Wait for connect...\n");  
    if(ConnectNamedPipe(g_hPipe, NULL) == FALSE)  
    {  
        printf("Connect failed!\n");  
        goto out;  
    }  
    printf("Connected.\n");  

    while(1)  
    {  
        scanf("%s", &buffer);  
        if(WriteFile(g_hPipe, buffer, (DWORD)strlen(buffer), &WriteNum, NULL) == FALSE)  
        {  
            printf("Write failed!\n");  
            break;  
        }  
    }  

out:  
    printf("Close pipe.\n");  
    CloseHandle(g_hPipe);  
    system("pause");  
    return 0;  
}  

客户端代码示例:

#include <stdio.h>  
#include <windows.h>  

#define PIPE_NAME L"\\\\.\\Pipe\\test"  

HANDLE g_hPipe = INVALID_HANDLE_VALUE;  

int main()  
{  
    char buffer[1024];  
    DWORD ReadNum;  

    printf("test client.\n");  

    g_hPipe = CreateFile(PIPE_NAME, GENERIC_READ | GENERIC_WRITE,   
            0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);  
    if (g_hPipe == INVALID_HANDLE_VALUE)  
    {  
        printf("Connect pipe failed!\n");  
        goto out;  
    }  
    printf("Connected.\n");  

    while(1)  
    {  
        if(ReadFile(g_hPipe, buffer, sizeof(buffer), &ReadNum, NULL) == FALSE)  
        {  
            break;  
        }  
        buffer[ReadNum] = 0;  
        printf("%s\n", buffer);  
    }  
out:  
    printf("Close pipe.\n");  
    CloseHandle(g_hPipe);  
    system("pause");  
    return 0;  
}  

windows pipe

标签:管道   windows   命名管道   

原文地址:http://blog.csdn.net/liukang325/article/details/46681219

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!