1.client:
1#include"iostream"2#include"windows.h"3 4 using namespacestd;5 voidMainintargcChar*argv[])6 { 7LPCTSTR message="The pipe ' s message from a client to server.";8 if(argc==2)9message=argv[1];Ten DWORD Writenum; One A if(Waitnamedpipe ("\\\\.\\pipe\\test", nmpwait_wait_forever) = =FALSE) { -cout<<"wait for the link to fail! "<<Endl; - return; the } - -HANDLE Hpipe=createfile ("\\\\.\\pipe\\test", generic_read| -Generic_write,0, null,open_existing,file_attribute_normal,null); + if(hpipe==Invalid_handle_value) { -cout<<"Pipe Open failed! "<<Endl; + return; A } at -cout<<"Successful Piping Connection"<<Endl; - if(WriteFile (Hpipe,message,strlen (Message), &writenum,null) = =FALSE) { -cout<<"data Write pipeline failed! "<<Endl; - } - CloseHandle (hpipe); in}
2.server:
1#include"iostream"2#include"windows.h"3 using namespacestd;4 5 voidMain () {6 Charbuffer[1024x768];7 DWORD Readnum;8 9HANDLE M_hpipe=createnamedpipe ("\\\\.\\pipe\\test", pipe_access_duplex,pipe_type_byte| Pipe_readmode_byte,1,0,0, +, NULL);Ten One A if(m_hpipe==Invalid_handle_value) -cout<<"failed to create named pipe! "<<Endl; - the while(1){ - if(Connectnamedpipe (m_hpipe,null) = =FALSE) { - CloseHandle (m_hpipe); -cout<<"failed to establish link to client"<<Endl; + } - + if(ReadFile (M_hpipe,buffer,1024x768, &readnum,null) = =FALSE) Acout<<"Read Pipe failer!\n"<<Endl; at - Else{ -buffer[readnum]=0; -cout<<"Read pipe is:"<<buffer<<". \ n"<<Endl; - } - in if(Disconnectnamedpipe (m_hpipe) = =FALSE) -cout<<"Terminate link failed"<<Endl; to Else +cout<<"successfully terminated link"<<Endl; - if(strcmp (Buffer,"End")==0) the Break; * } $ Panax Notoginseng CloseHandle (m_hpipe); -}