Original address: http://www.cnblogs.com/yukaizhao/archive/2011/08/04/system-io-pipes.html
Named pipes:
Named pipes are more powerful than anonymous pipes and can be used to make duplex communication between processes (that is, the two processes that communicate are both readable and writable); Named pipes can also be used to communicate across networks between different machines. Multiple Namedpipeserverstream instances can be created in multiple threads, serving multiple client-side services. In addition, named Pipes Support message transmission so that the client can read messages of any length without knowing the length of the message.
Service-Side code:
//server sends messages//To create a named pipe instanceusing(Namedpipeserverstream Serverstream =NewNamedpipeserverstream ("Testpipe")){ //Waiting for connectionserverstream.waitforconnection (); using(StreamWriter writer =NewStreamWriter (Serverstream)) {writer. AutoFlush=true; stringtemp; while(temp = Console.ReadLine ())! ="Stop") {writer. WriteLine (temp); } }}
Client code:
//Client reads datausing(Namedpipeclientstream Clientstream =NewNamedpipeclientstream ("Testpipe")){ //Open ConnectionClientstream.connect (); using(StreamReader reader =NewStreamReader (Clientstream)) { stringtemp; while(temp = reader.) ReadLine ())! ="Stop") {Console.WriteLine ("{0}:{1}", DateTime.Now, temp); } }}
The result of the above code execution is the server input text, the client displays the time and text content of the received text. It says that the named pipes are full-duplex, so you can also have the client write the content, and the server accepts the content, and the code is the same.
Named pipes based on message transfer:
A message-based named pipe can pass an indefinite amount of content without passing a content length or terminator, which is not a message-based transmission. We're all entering a piece of text into the pipeline, using the WriteLine method to transfer the information with a carriage return as a terminator. The other end of the pipeline uses the ReadLine method to read the carriage return newline as a sign of the end of a message delivery, which is not necessary when using message-based transmission. The following example:
Service-Side code:
//server sends messagesUTF8Encoding encoding =Newutf8encoding ();stringMessage1 ="Named Pipe Message Example";stringMessage2 ="another Named Pipe Message Example.";byte[] bytes;using(Namedpipeserverstream Serverstream =NewNamedpipeserverstream ("messagepipe2", Pipedirection.inout,1, Pipetransmissionmode.message, Pipeoptions.none)) {serverstream.waitforconnection (); //Send Messagebytes =encoding. GetBytes (MESSAGE1); Serverstream.write (Bytes,0, Bytes. Length); Bytes=encoding. GetBytes (Message2); Serverstream.write (Bytes,0, Bytes. Length);}
Client code:
//Client Read MessageDecoder Decoder =Encoding.UTF8.GetDecoder ();byte[] bytes =New byte[Ten];Char[] chars =New Char[Ten];using(Namedpipeclientstream Clientstream =NewNamedpipeclientstream ("messagepipe2") ) {clientstream.connect (); Clientstream.readmode=Pipetransmissionmode.message; //convert binary to string intnumbytes; Do { stringMessage =""; Do{numbytes= Clientstream.read (Bytes,0, Bytes. Length); intNumChars = decoder. GetChars (Bytes,0, NumBytes, chars,0); Message+=New string(Chars,0, NumChars); } while(!clientstream.ismessagecomplete); Decoder. Reset (); Console.WriteLine (message); } while(NumBytes! =0);}
Communicating between processes using Named pipes (GO)