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

客户端:#include<cstdlib>#include<iostream>#include<windows.h>usingnamespacestd;intmain(i... 客户端:
#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");
return 0;
}
system("PAUSE");
return EXIT_SUCCESS;
}
服务端:
#include <cstdlib>
#include <iostream>
#include <windows.h>
#include <string>

using namespace std;
DWORD WINAPI Wait(
LPVOID lpParameter // thread data
);
HANDLE hPipe;
int main(int argc, char *argv[])
{

SECURITY_ATTRIBUTES sa;
sa.bInheritHandle=true;

CreateNamedPipe("\\\\.\\pipe\\ethan",PIPE_ACCESS_DUPLEX|FILE_FLAG_OVERLAPPED,
0,1,1024,1024,0,
NULL);
if(hPipe==INVALID_HANDLE_VALUE){
cout<<"创建命名管道失败"<<endl;
hPipe=NULL;
system("PAUSE");
return 0;
}
HANDLE hThread;
hThread=CreateThread(NULL,0,Wait,NULL,0,NULL);
while(true){}

system("PAUSE");
return EXIT_SUCCESS;
}
DWORD WINAPI Wait(
LPVOID lpParameter // thread data
){

HANDLE hEvent;
hEvent=CreateEvent(NULL,true,false,NULL);
if(!hEvent){
cout<<"创建事件对象失败"<<endl;
CloseHandle(hEvent);
hEvent=NULL;
system("PAUSE");
return 0;
}
OVERLAPPED ol;
ol.hEvent=hEvent;
if(!ConnectNamedPipe(hPipe,&ol)){
if(ERROR_PIPE_CONNECTED!=GetLastError()){
cout<<"等待客户端失败"<<endl;
CloseHandle(hPipe);
CloseHandle(hEvent);
hPipe=NULL;
}
}
if(!WAIT_FAILED==WaitForSingleObject(hEvent,INFINITE)){
cout<<"等待对象失败"<<endl;
CloseHandle(hPipe);
CloseHandle(hEvent);
hPipe=NULL;
}
char buf[100];
DWORD dwRead;
while(true){
if(!ReadFile(hPipe,buf,100,&dwRead,NULL)){
cout<<"读取数据失败"<<endl;
continue;
}
cout<<buf;
}
}
如果这个问题解答不了,能不能给出一个命名管道通信的例子?
展开
 我来答
ssepotato
2008-04-04 · TA获得超过517个赞
知道小有建树答主
回答量:285
采纳率:100%
帮助的人:221万
展开全部
("\\\\.\\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_to_send;
char client_fifo_name[256];
pid_t mypid;

/*打开服务管道*/

server_fifo_fd = open(SERVER_FIFO_NAME,O_WRONLY);
if(server_fifo_fd == -1)
{
perror("Sorry,no server");
exit(1);
}
/*创建以进程ID命名的客户接收管道*/
mypid = getpid();
sprintf(client_fifo_name,CLIENT_FIFO_NAME,mypid);
if(mkfifo(client_fifo_name,0777) == -1)
{
perror(client_fifo_name);
exit(2);
}

/*向服务进程连续发送和接收五次数据*/
for(times_to_send=0;times_to_send<5;times_to_send++)
{
sprintf(my_request.text_data,"Hello from %d,%d",mypid,times_to_send);
my_request.client_pid = mypid;

/*向服务进程发出请求*/
printf("%d send: %s,",mypid,my_request.text_data);
write(server_fifo_fd,&my_request,sizeof(my_request));

/*从服务进程接收回答,打开客户有名管道*/
client_fifo_fd = open(client_fifo_name,O_RDONLY);

if(client_fifo_fd!=-1)
{
if(read(client_fifo_fd,&my_request,sizeof(my_request))>0)
{
printf("%d received: %s\n",mypid,my_request.text_data);
}
close(client_fifo_fd);
}

}
close(server_fifo_fd);
unlink(client_fifo_fd);
exit(0);
}
服务端:
/*
*服务程序
*/
#include"cliserv.h"
int main()
{
int server_fifo_fd,client_fifo_fd;
struct data_to_pass my_data;
int nbytes;
char client_fifo_name[256];
char * tmp_char_ptr;

/*创建并打开服务FIFO*/
mkfifo(SERVER_FIFO_NAME,0777);
server_fifo_fd = open(SERVER_FIFO_NAME,O_RDONLY);
if(server_fifo_fd == -1)
{
perror("Server info failed!");
exit(1);
}
/*客户请求排队*/
sleep(10);
do
{
/*接收来自客户的请求*/
nbytes = read(server_fifo_fd,&my_data,sizeof(my_data));
if(nbytes>0)
{
/*处理客户请求*/
tmp_char_ptr = my_data.text_data;
while(*tmp_char_ptr)
{
*tmp_char_ptr = toupper(*tmp_char_ptr);
tmp_char_ptr++;
}
/*返回处理过的数据*/
sprintf(client_fifo_name,CLIENT_FIFO_NAME,my_data.client_pid);
client_fifo_fd = open(client_fifo_name,O_WRONLY);
if(client_fifo_fd != -1)
{
write(client_fifo_fd,&my_data,sizeof(my_data));
close(client_fifo_fd);

}
}
}while(nbytes>0);
close(server_fifo_fd);
unlink(SERVER_FIFO_NAME);
return 0;
}
运行时:
先运行服务端程序,在运行客户端程序。
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式