上面只贴了client端的,server端的如下: ssnpPipe=CreateNamedPipe("\\\\.\\PIPE\\ssnp", PIPE_ACCESS_INBOUND, PIPE_TYPE_MESSAGE|PIPE_WAIT, 1,100,100,0, (LPSECURITY_ATTRIBUTES) NULL); //Check and see if the named pipe was created if(ssnpPipe==INVALID_HANDLE_VALUE) { cerr<<"ERROR:Unableto create a named pipe."<<endl; return(1); } //Allow a client to connect to the name pipe,terminate if unsuccessful cout<<"Waiting for connection..."<<endl; if(!ConnectNamedPipe(ssnpPipe,(LPOVERLAPPED)NULL)) { cerr<<"ERROR:Unable to connect a named pipe"<<GetLastError()<<endl; CloseHandle(ssnpPipe); return(1); } //Repeatedly check for messages until the programis terminated while(1) { //Read the message and check to see if read was successful if(!ReadFile(ssnpPipe,toDisptxt,sizeof(toDisptxt), &NumBytesRead,(LPOVERLAPPED) NULL)) { cerr<<"ERROR:Unable to read from named pipe" <<GetLastError()<<endl; CloseHandle(ssnpPipe); return(1); } //display the Message cout<<toDisptxt<<endl; }//while
ssnpPipe=CreateNamedPipe("\\\\.\\PIPE\\ssnp",
PIPE_ACCESS_INBOUND,
PIPE_TYPE_MESSAGE|PIPE_WAIT,
1,100,100,0,
(LPSECURITY_ATTRIBUTES) NULL);
//Check and see if the named pipe was created
if(ssnpPipe==INVALID_HANDLE_VALUE)
{
cerr<<"ERROR:Unableto create a named pipe."<<endl;
return(1);
}
//Allow a client to connect to the name pipe,terminate if unsuccessful
cout<<"Waiting for connection..."<<endl;
if(!ConnectNamedPipe(ssnpPipe,(LPOVERLAPPED)NULL))
{
cerr<<"ERROR:Unable to connect a named pipe"<<GetLastError()<<endl;
CloseHandle(ssnpPipe);
return(1);
}
//Repeatedly check for messages until the programis terminated
while(1)
{
//Read the message and check to see if read was successful
if(!ReadFile(ssnpPipe,toDisptxt,sizeof(toDisptxt),
&NumBytesRead,(LPOVERLAPPED) NULL))
{
cerr<<"ERROR:Unable to read from named pipe"
<<GetLastError()<<endl;
CloseHandle(ssnpPipe);
return(1); }
//display the Message
cout<<toDisptxt<<endl;
}//while
PIPE_ACCESS_DUPLEX看你的需要了。