To create a named pipe
Named pipes are often used for communication between applications, and are highly efficient because they do not require serialization and deserialization operations. More efficient than TCP communication, but lower than shared memory.
Named pipes are the best means of communication by enabling interprocess communication between machines on a local machine or in a LAN.
Create a Namedpipeserverstream:
New ( ten);
This indicates that the pipe for the named pipe server is safe for bidirectional communication, similar to TCP duplex. Next, use the following code to wait for the connection:
Pipeserver.waitforconnection ();
If you have a connection, you can read it using a stream reader:
New StreamReader (Pipeserver);
Similarly, you can also use a stream write operator to write data to the stream, and the other end of the pipeline can read the stream:
using New StreamWriter (Pipeserver)) { true; Sw. WriteLine ("" + str);}
Note: Using is used here, which means that the stream is closed when it is finished, but it also closes the pipe, so be aware of it. If the client wants to read all the data, it needs to wait here to close the stream.
Custom Application Layer Communication protocol
How to read all the data in the pipeline, see the following code:
Newstring text =sr. ReadToEnd ();
This way you can read all the data, but in another section of the pipeline, if the write operator does not call the Close method, it cannot be read and the program will block here. Therefore, an "application protocol" must be defined, and the client tells the server to end reading data appropriately.
We follow the HTTP protocol method, using more than 2 consecutive carriage return to the end of the HTTP header information, we also define, and attach other tokens to indicate that the flow data sent, refer to the sending side:
Public stringQuery (stringrequest) { if(!_pipeclient.isconnected) {_pipeclient.connect (10000); } StreamWriter SW=NewStreamWriter (_pipeclient); Sw. WriteLine (Request); Sw. WriteLine ();//2 consecutive line breaks Plus "#END" means endSW. WriteLine (); Sw. WriteLine ("#END"); Sw. Flush (); StreamReader SR=NewStreamReader (_pipeclient); stringReturnVal =Sr. ReadToEnd (); returnReturnVal; }
On the server side, the following methods are used to read the stream data:
stringstr =NULL; stringStrall =NULL; System.Text.StringBuilder SB=NewSystem.Text.StringBuilder (); StreamReader SR=NewStreamReader (pipeserver); while(Pipeserver.canread && (NULL! = (str =Sr. ReadLine () ))) {//when encountering 2 consecutive line breaks plus #end, the input ends if(str = ="#END") {Strall=sb. ToString (); if(Strall.endswith ("\r\n\r\n")) Break; } Else { if(str = ="") sb. Appendline (); Elsesb. Appendline (str); }} Strall= Strall.substring (0, Strall.length-"\r\n\r\n\r\n". Length);
Test and download
Finally, write a client and server console program:
namespace namepipedsample_server{ class program { staticvoid Main (string[] args) { new namedpipelistenserver ("test"); Svr. Run (); Console.read (); }}}
namespacenamepipedsample_client{classProgram {Static voidMain (string[] args) { stringSendstr =NULL; using(Namedpipeclient client =NewNamedpipeclient (".","Test") ) {Sendstr="fff\r\ndddd\r\n"; Console.WriteLine ("send:{0}", SENDSTR); Console.WriteLine ("reply:{0}", client. Query (SENDSTR)); Sendstr="54353"; Console.WriteLine ("send:{0}", SENDSTR); Console.WriteLine ("reply:{0}", client. Query (SENDSTR)); Sendstr="aaaaaaa"; Console.WriteLine ("send:{0}", SENDSTR); Console.WriteLine ("reply:{0}", client. Query (SENDSTR)); } Console.WriteLine ("send all OK."); Console.read (); } }}
After testing, this method is named pipe client-server communication succeeded. This procedure is in the Netizen original article based on the improvement, in this expressed thanks, the original address: http://blog.csdn.net/educast/article/details/7219774
This program demo download
Using Named pipes for interprocess communication