我用命名管道写的程序来实现两个进程间的通信,想知道为什么老是不能得到自己想要的结果!!

来源:百度知道 编辑:UC知道 时间:2024/07/08 09:53:09
客户端:
#include <cstdlib>
#include <iostream>
#include <windows.h>
using namespace std;

int main(int argc, char *argv[])
{
HANDLE hPipe;
if(!WaitNamedPipe("\\\\.\\pipe\\ethan",NMPWAIT_WAIT_FOREVER))
{
cout<<"当前没有可用的命名管道实例"<<endl;
system("PAUSE");
return 0;
}
hPipe=CreateFile("\\\\.\\pipe\\ethan",GENERIC_READ|GENERIC_WRITE
,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
if(INVALID_HANDLE_VALUE==hPipe){
cout<<"打开命名管道失败"<<endl;
hPipe=NULL;
system("PAUSE");
return 0;
}

char buf[]="我帅不帅?";
DWORD dwWrite;
if(!WriteFile(hPipe,buf,strlen(buf)+1,&dwWrite,NULL)){
cout<<"写入数据失败"<<endl;
system("pause")

("\\\\.\\pipe\\ethan",PIPE_ACCESS_DUPLEX|FILE_FLAG_OVERLAPPED,
0,1,1024,1024,0,
NULL);
由于FIFO是作为一个有名文件存在于文件系统中的,需要确认你是否有创建文件的权限。看你上面写的文件的路径好像有问题哦。
下面是UNIX环境命名管道的例子。
头文件:
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<fcntl.h>
#include<limits.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<ctype.h>

#define SERVER_FIFO_NAME "/opt/chengxs/temp/fifo/serv_fifo" /*服务专用的fifo*/
#define CLIENT_FIFO_NAME "/opt/chengxs/temp/fifo/client_%d_fifo" /*客户专用的fifo*/

struct data_to_pass
{
pid_t client_pid;
char text_data[256-1];
};
客户端:
/*
*客户程序
*/

#include"cliserv.h"
int main()
{
int server_fifo_fd,client_fifo_fd;
struct data_to_pass my_request;
int times