Use named MPs queues for inter-process communication and named MPs queues for Process Communication.
Create a named pipe
Named pipelines are often used for communication between applications. Because serialization and deserialization are not required, the efficiency is very high. It is more efficient than TCP communication, but lower than shared memory.
The named pipe can implement inter-process communication between machines on the local machine or in the LAN, so it is the best communication method.
Create a NamedPipeServerStream:
NamedPipeServerStream pipeServer = new NamedPipeServerStream(_pipName, PipeDirection.InOut, 10);
This indicates that the pipe of the named pipe server can communicate in two directions, similar to TCP duplex. Then, use the following code to wait for the connection:
pipeServer.WaitForConnection();
If there is a connection, you can use stream reader for reading:
StreamReader sr = new StreamReader(pipeServer);
Similarly, you can use a stream write operator to write data to the stream. The other end of the pipeline can read the stream:
using (StreamWriter sw = new StreamWriter(pipeServer)) { sw.AutoFlush = true; sw.WriteLine("hello world " + str); }
Note: using is used here, which means that the stream will be closed after writing, but the pipeline will also be closed, so pay attention to it. If the client needs to read all the data, it needs to wait until the stream is closed.
Custom Application layer communication protocol
To read all the data in the MPs queue, see the following code:
StreamReader sr = new StreamReader(pipeServer); string text =sr.ReadToEnd();
This method can read all the data. However, if the write operator does not call the Close method in another section of the pipeline, the program will be blocked here. Therefore, an "Application Protocol" must be defined, and the client tells the server to stop reading data.
We follow the HTTP method and use two or more consecutive carriage return lines to end the HTTP header information. We also define this and add other labels to indicate that the stream data has been sent, reference Sender:
Public string Query (string request) {if (! _ PipeClient. isConnected) {_ pipeClient. connect (10000);} StreamWriter sw = new StreamWriter (_ pipeClient); sw. writeLine (request); sw. writeLine (); // two lines in a row plus "# END" indicates that sw is ended. writeLine (); sw. writeLine ("# END"); sw. flush (); StreamReader sr = new StreamReader (_ pipeClient); string returnVal = sr. readToEnd (); return returnVal ;}
On the server side, the following method is used to read stream data:
String str = null; string strAll = null; System. text. stringBuilder sb = new System. text. stringBuilder (); StreamReader sr = new StreamReader (pipeServer); while (pipeServer. canRead & (null! = (Str = sr. readLine () {// when there are two consecutive line breaks plus # END, it indicates that the input ends if (str = "# END") {strAll = sb. toString (); if (strAll. endsWith ("\ r \ n") break;} else {if (str = "") sb. appendLine (); else sb. appendLine (str) ;}} strAll = strAll. substring (0, strAll. length-"\ r \ n ". length );
Test and download
Finally, write a console program for the client and server:
namespace NamePipedSample_Server{ class Program { static void Main(string[] args) { NamedPipeListenServer svr = new NamedPipeListenServer("test"); svr.Run(); Console.Read(); } }}
namespace NamePipedSample_Client{ class Program { static void Main(string[] args) { string sendStr = null; using (NamedPipeClient client = new NamedPipeClient(".", "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 used to name the client-server communication of the MPs queue successfully. The program in this article is improved on the basis of the original article, expressed thanks, the original address: http://blog.csdn.net/educast/article/details/7219774
Download the program Demo in this article