简体   繁体   English

有没有办法格式化 EndRead(ar)?

[英]Is there a way to format EndRead(ar)?

I would like to process data via a NamedPipeServer.我想通过 NamedPipeServer 处理数据。 To the server 1000 data are passed as string with content "int, double, bool, string, ...".向服务器 1000 数据作为字符串传递,内容为“int, double, bool, string, ...”。 The int Value increases from 1 -1000; int 值从 1 -1000 增加; double, bool and string also change. double、bool 和 string 也发生变化。 In total, 15 values are passed in one line.一行中总共传递了 15 个值。

The received data should be read out in the server and converted from string to int,double, bool.接收到的数据应该在服务器中读出,并从字符串转换为 int、double、bool。 My program is functional in this respect.我的程序在这方面很有用。

My problem is that the conversion of the data becomes erroneous if the method "EndRead(ar);"我的问题是,如果方法“EndRead(ar);”,数据转换就会出错。 is not synchronous.不是同步的。 The sent string is then read multiple times.然后多次读取发送的字符串。 EndRead then does not recognize the end of the line. EndRead 则无法识别行尾。 Is there a way to force EndRead?有没有办法强制 EndRead?

private void BackgroundInit()
{
    _currentServer.WaitForConnection();   //Wait for a client connection
    ClientMessage cm = new ClientMessage  //Create a new message object
    {
        buffer = new byte[1024]       //Set the receive buffer
    };
    _currentServer.BeginRead(cm.buffer, 0, cm.buffer.Length, new AsyncCallback(ReadStream), cm);         }

private async void ReadStream(IAsyncResult ar)
{
    ClientMessage cmo = (ClientMessage)ar.AsyncState;  //Get the message object
    if (_currentServer == null) return;
    int recv = _currentServer.EndRead(ar);    
     //Receive data ==> Fail at 81 - 85


     if (recv > 0) //If data received
    {
    //Convert msg to string
    byte[] read = new byte[recv];
        Buffer.BlockCopy(cmo.buffer, 0, read, 0, recv);
        string message = Encoding.Unicode.GetString(read);

    Array.Clear(cmo.buffer, 0, cmo.buffer.Length);//Free the byte array

        if (_currentServer != null)
    {
         _currentServer.BeginRead(cmo.buffer, 0, cmo.buffer.Length, new AsyncCallback(ReadStream), cmo);
         }

}

示例 ==> 消息 81 到 85 有错误

Passing \r\n is not successful.传递 \r\n 不成功。 Adjusting the Buffer_Size did not bring any improvement either.调整 Buffer_Size 也没有带来任何改善。 The subsequent formatting of the received strings is not possible because the non-synchronous part of the data is skipped.接收到的字符串的后续格式化是不可能的,因为数据的非同步部分被跳过了。

Just use StreamReader to read data line by line.只需使用StreamReader逐行读取数据即可。

async void ReadPipe()
{
    using(var reader = new StreamReader(_currentServer))
    {
        while (true) 
        {
            var line = await reader.ReadLineAsync();
            if (line == null)
                break;

            //...
        }
    }
}

And on the server side, use StreamWriter.WriteLine to send data.在服务器端,使用StreamWriter.WriteLine发送数据。

using(var writer = new StreamWriter(_currentServer))
{
    writer.WriteLine(...);
}

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

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