Pipe in Windows

Source: Internet
Author: User
This pipeline is not the one that UNIX uses for command line.

Windows MPs queues can access named MPs queues on machines with names on the current machine or known machines. You can also create named MPs queues by yourself. A named pipe is the same as a server socket. It can perform listen and generate many instances to communicate with many clients. The main functions are:

Callnamedpipe: connection, read, write, close, timeout
Connectnamedpipe: Waiting for client connection
Createnamedpipe: Create Named Pipe
Disconnectnamedpipe: disconnect
Peeknamedpipe: test the pipe input buffer.
Waitnamedpipe: wait until the named pipe can start to connect. The client uses
The client uses createfile, writefile, and readfile to perform necessary operations.

Recently, I used WCF as a project. I was surprised that it was very comfortable to encapsulate and use the network. Therefore, I also wanted to implement a habit in C ++. Of course, I do not intend to support soap, and I also want to add the remote object access function, that is, the object generated by a is uploaded to another machine B, and B is sent to C, c can still call the object function, as long as the interface is correct. WCF can host many services belonging to different applications on the same port. pipe should be able to solve the problem of message distribution, because one port can only host one server socket, at least I think so.

Therefore, it is encapsulated. The specific usage of the functions listed above is as follows.

1 # include "... \ VL ++ \ library \ platform \ vl_console.h"
2 # include "... \ VL ++ \ library \ data \ vl_system.h"
3 # include "... \ VL ++ \ library \ data \ vl_comm.h"
4
5 using namespace VL;
6 using namespace Vl: Platform;
7 using namespace Vl: system;
8 using namespace Vl: System: synchronization;
9 Using namespace Vl: communication;
10
11 void vlmain (vl_console & con)
12 {
13 con. settitle (L "vczh pipe ");
14 con. settestmemoryleaks (true );
15 con. setpauseonexit (true );
16
17 vbool serverprocess = false;
18 vl_synevent event;
19 switch (event. Create (false, true, l "vczh_event "))
20 {
21 case vl_synobject: arsucceed:
22 serverprocess = true;
23 con. Write (L "Server \ r \ n ");
24 con. Write (L "Press [enter] to start. \ r \ n ");
25 con. waitforenter ();
26 break;
27 case vl_synobject: aralreadyexists:
28 con. Write (L "client \ r \ n ");
29 con. Write (L "waiting for signal ");
30 event. waitfor ();
31 con. Write (L "signaled \ r \ n ");
32 break;
33 case vl_synobject: arfail:
34 con. Write (L "fail \ r \ n ");
35 return;
36}
37 If (serverprocess)
38 {
39 vl_pipeserver server (L "\\\\\\ PIPE \ vczhpipe", true, true );
40 event. Signal ();
41 vl_autoptr <vl_serverpipe> pipe = server. waitforconnection ();
42 if (PIPE)
43 {
44 con. Write (L "message link from:" + Pipe-> getclientcomputername () + L "\ r \ n ");
45 vwchar buffer [1024];
46 While (true)
47 {
48 Vint READ = pipe-> readdata (vbuffer) buffer, 1024 );
49 If (read =-1)
50 {
51 con. Write (L "read fail. \ r \ n ");
52 break;
53}
54 else if (read = 0)
55 {
56 break;
57}
58 else
59 {
60 con. Write (L "*");
61 buffer [read] = 0;
62 con. Write (buffer );
63 con. Write (L "\ r \ n ");
64}
65}
66}
67 else
68 {
69 con. Write (L "fail. \ r \ n ");
70}
71 event. Signal ();
72}
73 else
74 {
75 vl_autoptr <vl_clientpipe> pipe = new vl_clientpipe (L "\\\\\\ pipe \\ vczhpipe ");
76 if (pipe-> connect ())
77 {
78 vwchar message1 [] = l "This is the first message .";
79 vwchar message2 [] = l "this is the second message .";
80 Pipe-> writedata (vbuffer) message1, sizeof (message1)-2 );
81 pipe-> writedata (vbuffer) message2, sizeof (message2)-2 );
82}
83 else
84 {
85 con. Write (L "fail. \ r \ n ");
86}
87 event. waitfor (1000 );
88}
89 con. Write (L "stop ");
90}

This program is started twice without being closed. The first program is started as the server, and the second program is started as the client. Press enter on the server to start the process. The client sends two messages to the server. The server accepts and prints the messages on the screen. After the process is completed, two programs will end in one second. The reason for waiting for one second is that, if the pipeline ends directly, messages that are not read will disappear.

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.