使用命名管道进程之间通信(转)

原文地址:http://www.cnblogs.com/yukaizhao/archive/2011/08/04/system-io-pipes.html

命名管道:

命名管道的功能比匿名管道更强大,可以在进程之间做双工通信(即通信的两个进程都是既可以读也可写的);命名管道也可以实现跨网络在不同机器之间进行通信。可以在多线程中创建多个NamedPipeServerStream实例,为多个client端服务。另外命名管道还支持消息传输,这样client端可以读取任意长度的消息,而无须知道消息的长度。

服务端代码:

//服务端 发送消息
//创建命名管道实例
using (NamedPipeServerStream serverStream = new NamedPipeServerStream("testpipe"))
{
    //等待连接
    serverStream.WaitForConnection();
    using (StreamWriter writer = new StreamWriter(serverStream))
    {
        writer.AutoFlush = true;
        string temp;
        while ((temp = Console.ReadLine()) != "stop")
        {
            writer.WriteLine(temp);
        }
    }
}

客户端代码:

//客户端读取数据
using (NamedPipeClientStream clientStream = new NamedPipeClientStream("testpipe"))
{
    //开启连接
    clientStream.Connect();
    using (StreamReader reader = new StreamReader(clientStream))
    {
        string temp;
        while ((temp = reader.ReadLine()) != "stop")
        {
            Console.WriteLine("{0}:{1}", DateTime.Now, temp);
        }
    }
}

上面代码执行结果是服务端输入文本,客户端显示接受到文本的时间和文本内容。上面说了命名管道是全双工通信的,所以你也可以让客户端写内容,而服务端接受内容,代码大同小异。

基于消息传输的命名管道:

基于消息的命名管道可以传递不定长的内容,而无需传递内容长度或者结束符,上面非基于消息的传输我们都是在向管道中输入一段文本,使用WriteLine方法以回车换行作为结束符传输信息,而管道的另一端再使用ReadLine方法以读取到回车换行符作为一个消息传递结束的标志;而在使用基于消息传输时就不必这么做了。如下示例:

服务端代码:

//服务端发送消息
UTF8Encoding encoding = new UTF8Encoding();
string message1 = "Named Pipe Message Example";
string message2 = "Another Named Pipe Message Example.";

byte[] bytes;
using (NamedPipeServerStream serverStream =
    new NamedPipeServerStream("messagepipe2", PipeDirection.InOut, 1,
    PipeTransmissionMode.Message, PipeOptions.None))
{
    serverStream.WaitForConnection();

    //发送消息
    bytes = encoding.GetBytes(message1);
    serverStream.Write(bytes, 0, bytes.Length);
    bytes = encoding.GetBytes(message2);
    serverStream.Write(bytes, 0, bytes.Length);
}

客户端代码:

//客户端读取消息
Decoder decoder = Encoding.UTF8.GetDecoder();
byte[] bytes = new byte[10];
char[] chars = new char[10];
using (NamedPipeClientStream clientStream = new NamedPipeClientStream("messagepipe2"))
{
    clientStream.Connect();
    clientStream.ReadMode = PipeTransmissionMode.Message;
    //将二进制转换成字符串
    int numBytes;
    do
    {
        string message = "";
        do
        {
            numBytes = clientStream.Read(bytes, 0, bytes.Length);
            int numChars = decoder.GetChars(bytes, 0, numBytes, chars, 0);
            message += new string(chars, 0, numChars);
        } while (!clientStream.IsMessageComplete);
        decoder.Reset();
        Console.WriteLine(message);
    } while (numBytes != 0);
}
原文地址:https://www.cnblogs.com/tianma3798/p/5069372.html