简体   繁体   English

消息模式下的C#命名管道有时合并消息?

[英]C# Named Pipes in message mode sometimes merge messages?

I'm using Named Pipes in a project I'm working on, both sides are written in C# (client and server) and both are on the same computer (The named pipes are used for RPC). 我在我正在研究的项目中使用命名管道,双方都是用C#(客户端和服务器)编写的,两者都在同一台计算机上(命名管道用于RPC)。 The named pipes on both sides are set to use 'PipeTransmissionMode.Message' mode which according to my understanding should "pack" each chunk of data passed to pipe.Write() in a single message and pipe.Read() on the other end should receive the entire message or none of it. 两边的命名管道都设置为使用'PipeTransmissionMode.Message'模式,根据我的理解应该“打包”传递给pipe.Write()的每个数据块在一个消息中,pipe.Read()在另一端应该接收整个消息或者不接收任何消息。

While trying to deserialize the data on the receiving end, I saw that I get big chucks of data, bigger than the packets I sent. 在尝试反序列化接收端的数据时,我发现我获得了大量的数据,大于我发送的数据包。 So I made a small test, I created two Applications, server and client, which looks like this: 所以我做了一个小测试,我创建了两个应用程序,服务器和客户端,看起来像这样:

Server: 服务器:

class Program
{
    static void Main(string[] args)
    {
        NamedPipeServerStream pipe = new NamedPipeServerStream("TestPipe", PipeDirection.InOut, 1, PipeTransmissionMode.Message, PipeOptions.Asynchronous, 1024, 1024);
        pipe.WaitForConnection();

        Random random = new Random();

        while(true)
        {
            byte[] buffer = new byte[32];
            random.NextBytes(buffer);
            pipe.Write(buffer, 0, buffer.Length);
            Console.WriteLine(buffer.Length);
        }
    }
}

Client: 客户:

class Program
{
    static void Main(string[] args)
    {
        NamedPipeClientStream pipe = new NamedPipeClientStream(".", "TestPipe", PipeDirection.InOut, PipeOptions.Asynchronous);
        pipe.Connect();

        while(true)
        {
            byte[] buffer = new byte[2048];
            int read = pipe.Read(buffer, 0, buffer.Length);
            Console.WriteLine(read);

            if(read > 32)
               Console.WriteLine("Big");
        }
    }
}

Running this, the server outputs only 32 (which is what I expected) but the client, sometimes output 64 followed by "Big". 运行它,服务器只输出32(这是我所期望的)但客户端,有时输出64后跟“大”。 What's going on? 这是怎么回事?

PS: If I put Thread.Sleep(100) on the server loop, it works as expected (writes only 32 on both sides) PS:如果我把Thread.Sleep(100)放在服务器循环上,它按预期工作(两边只写32)

EDIT: If I put a Thread.Sleep(100) on the server and Thread.Sleep(200) on the client, I receive bigger packets more often, which leads me to believe that if more than one message is arrived before the call to read then read returns all of them. 编辑:如果我在服务器上放置一个Thread.Sleep(100)并在客户端上放置Thread.Sleep(200) ,我会更频繁地收到更大的数据包,这使我相信如果在调用之前有多个消息到达read然后read返回所有这些。 How can I ensure each read will return only one message? 如何确保每次读取只返回一条消息?

You forgot to set the ReadMode to Message as well: 您忘了将ReadMode设置为Message

pipe.ReadMode = PipeTransmissionMode.Message;

When you create or connect to a pipe, the default mode is always Byte , regardless of the TransmissionMode setting. 创建或连接到管道时,无论TransmissionMode设置如何,默认模式始终为Byte

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM