Windows Namedpipe Named pipes clent and server

Source: Internet
Author: User

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); -}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.