Using Named pipes for interprocess communication under C + +

Source: Internet
Author: User
Tags function prototype readfile biztalk

Previously, the mail slots have been used for interprocess communication: http://www.cnblogs.com/jzincnblogs/p/5192654.html, which uses named pipes for interprocess communication.

Unlike postal slots, named pipes transfer data between processes in a connected and reliable way, so the named pipes can transmit data only one-to-one. The steps for using named Pipes are as follows:

① creates a named pipe, the named pipe is created by calling the function CreateNamedPipe (), and the function prototype is as follows:

1 HANDLE WINAPI createnamedpipe (2 _in_ lpctstr lpname,3 _in_ DWORD Dwopenmode,4 _in_ DWORD Dwpipemode,5 _in_ DWORD nmaxinstances,6 _in_ DWORD noutbuffersize,7 _in_ DWORD ninbuffersize,8 _in_ DWORD ndefaulttimeout,9 _in_opt_ lpsecurity_attributes lpsecurityattributesTen);

The setting method of each parameter can refer to msdn:https://msdn.microsoft.com/zh-cn/biztalk/aa365150 (v=vs.80)

② Connect Named pipes. When a user successfully creates a named pipe, it can call the related function to connect the named pipe, and for the server, the function connectnamedpipe () waits for the client's connection request, and the function prototype is as follows:

1 BOOL WINAPI connectnamedpipe (2  _in_        HANDLE       hnamedpipe,3   _inout_opt_ lpoverlapped lpoverlapped4 );

Parameter setting Method: Https://msdn.microsoft.com/zh-cn/biztalk/aa365150 (v=vs.80)

For the client, before connecting to a named pipe created by the server, you need to determine whether the named pipe is available, call the function waitnamedpipe () implementation, and use the function to refer to the msdn:https://msdn.microsoft.com/zh-cn/ subscriptions/aa365800

When the Waitnamedpipe () call succeeds, you can use CreateFile () to open the named pipe to the handle of the obtained pipe.

③ read-write Named pipes, read and write operations on named Pipes are done using functions ReadFile () and WriteFile (), similar to the previous postal slots.

The implementation code for the server and the client is as follows:

Server-side:

1 //Server2 //Named pipes with reliable connection-based transmission, only one-to-one transmission3#include <windows.h>4#include <iostream>5 6 #defineBuf_size 10247 8 usingStd::cerr;9 usingstd::cout;Ten usingStd::endl; One  A intMain () - { - HANDLE h_pipe; the     CharBuf_msg[buf_size]; -DWORD NUM_RCV;//the number of bytes actually received -     //Create a named pipe named Mypipe, the message can only flow from the client to the server, read and write data in blocking mode, byte stream format, the timeout value of 0 for the default of 50 milliseconds -H_pipe =:: CreateNamedPipe ("\\\\.\\pipe\\mypipe", Pipe_access_inbound, Pipe_readmode_byte | Pipe_wait, Pipe_unlimited_instances, Buf_size, Buf_size,0, nullptr); +     if(H_pipe = =Invalid_handle_value) -     { +Cerr <<"Failed to create named pipe! Error Code:"<<:: GetLastError () <<"\ n"; ASystem"Pause"); at         return 1; -     } -     Else -     { -cout <<"Named Pipe created successfully...\n"; -     } in     //waiting for Named pipe client connections -     if(:: Connectnamedpipe (H_pipe, nullptr)) to     { +cout <<"A Client connected...\n"; -memset (Buf_msg,0, buf_size); the         //reading Data *         if(:: ReadFile (H_pipe, Buf_msg, Buf_size, &NUM_RCV, nullptr)) $         {Panax Notoginsengcout <<"Message Received:"<< buf_msg <<"\ n"; -         } the         Else +         { ACerr <<"Failed to receive message! Error Code:"<<:: GetLastError () <<"\ n"; the :: CloseHandle (h_pipe); +:: System ("Pause"); -             return 1; $         } $     } - :: CloseHandle (h_pipe); -:: System ("Pause"); the     return 0; -}

Client:

1 //Client2#include <windows.h>3#include <iostream>4 5 #defineBuf_size 10246 7 usingStd::cerr;8 usingstd::cout;9 usingStd::endl;Ten  One intMain () A { - HANDLE h_pipe; -     CharBuf_msg[] ="Test for named pipe ..."; theDWORD NUM_RCV;//the number of bytes actually received -cout <<"Try to connect named pipe...\n"; -     //Connecting Named Pipes -     if(:: Waitnamedpipe ("\\\\.\\pipe\\mypipe", Nmpwait_wait_forever)) +     { -         //Open the specified named pipe +H_pipe =:: CreateFile ("\\\\.\\pipe\\mypipe", Generic_write,0, nullptr, open_existing, File_attribute_normal, nullptr); A         if(H_pipe = =Invalid_handle_value) at         { -Cerr <<"Failed to open the appointed named pipe! Error Code:"<<:: GetLastError () <<"\ n"; -:: System ("Pause"); -             return 1; -         } -         Else in         { -             if(:: WriteFile (H_pipe, Buf_msg, Buf_size, &NUM_RCV, nullptr)) to             { +cout <<"Message sent successfully...\n"; -             } the             Else *             { $Cerr <<"Failed to send message! Error Code:"<<:: GetLastError () <<"\ n";Panax Notoginseng :: CloseHandle (h_pipe); -:: System ("Pause"); the                 return 1; +             } A         } the :: CloseHandle (h_pipe); +     } -:: System ("Pause"); $     return 0; $}

Using Named pipes for interprocess communication under C + +

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.