【问题标题】:Read Binary FIle Lines From Memory从内存中读取二进制文件行
【发布时间】:2017-10-18 02:25:25
【问题描述】:

我想将二进制文件 (Test.bin) 从我的服务器模块发送到我的客户端可执行文件,它存储在内存中。在服务器端 (C#) 我这样创建 .bin:

public static bool Write(string fileName, string[] write)
{
    try
    {
        using (BinaryWriter binWriter = new BinaryWriter(File.Open(fileName, FileMode.Create)))
        {
            // each write-Array-Segment contains a 256 char string
            for (int i = 0; i < write.Length; i++)
                binWriter.Write(write[i] + "\n");
        }

        return true;
    }
    catch (Exception e)
    {
        return false;
    }
}

然后我像这样将它发送给客户:

byte[] buffer = File.ReadAllBytes(Program.testFile /*Test.bin*/ );
byte[] bytes = BitConverter.GetBytes(buffer.Length);

if (BitConverter.IsLittleEndian)
    Array.Reverse((Array)bytes);

this.tcpClient.GetStream().Write(bytes, 0, 4);
this.tcpClient.GetStream().Write(buffer, 0, buffer.Length);

this.tcpClient.Close();

在客户端 (C++) 我接收它并像这样存储它:

DWORD UpdateSize = 0;
NetDll_recv(XNCALLER_SYSAPP, Sock, (char*)&UpdateSize, 4, 0); // what's being first received

unsigned char* Update = new unsigned char[UpdateSize];
if (UpdateSize == 0 || !Network_Receive(Sock, Update, UpdateSize) /*Downloading file into "Update"*/ )
{
    Sleep(2000);
    Network_Disconnect(Sock);
    printf("Failed to download file.\n");
}

这一切都很好。现在的问题:

如何将我写入服务器端文件的行读取到客户端的数组中?我不想将文件存储在客户端设备上并使用 Streamreader,我想从内存中读取它!

非常感谢任何帮助! (提供一些代码可能是最好的)

【问题讨论】:

  • 你确定二进制数据不能包含换行符吗?
  • 它确实包含一个换行符“\n”...

标签: c# memory tcp binary client


【解决方案1】:

由于您要将字符串序列化到二进制流中,因此我将执行以下操作。对于数组中的每个字符串:

  • 序列化字符串的大小
  • 序列化字符串 (您不需要任何分隔符)。

在 C++ 客户端中,当您收到流时:

  • 只读取大小(要读取的字节数取决于整数大小)
  • 当你有大小时,你读取指定的字节数以便重构字符串

然后,你继续读取大小,然后是相应的字符串,直到字节流结束。

按要求举个简单的例子:

string[] parts = new string[]
{
    "abcdefg",
    "Lorem ipsum dolor sit amet, consectetur adipiscing elit.Maecenas viverra turpis mauris, nec aliquet ex sodales in.",
    "Vivamus et quam felis. Vestibulum sit amet enim augue.",
    "Sed tincidunt felis nec elit facilisis sagittis.Morbi eleifend feugiat leo, non bibendum dolor faucibus sed."
};
MemoryStream stream = new MemoryStream();
// serialize each string as a couple of size/bytes array.
BinaryWriter binWriter = new BinaryWriter(stream);
foreach (var part in parts)
{
    var bytes = UTF8Encoding.UTF8.GetBytes(part);
    binWriter.Write(bytes.Length);
    binWriter.Write(bytes);
}

// read the bytes stream: first get the size of the bytes array, then read the bytes array and convert it back to a stream.
stream.Seek(0, SeekOrigin.Begin);
BinaryReader reader = new BinaryReader(stream);
while (stream.Position < stream.Length)
{
    int size = reader.ReadInt32();
    var bytes = reader.ReadBytes(size);
    var part = UTF8Encoding.UTF8.GetString(bytes);
    Console.WriteLine(part);
}
stream.Close();
Console.ReadLine();

【讨论】:

  • 您可以复制和修改我的代码吗?因为我不太明白你的意思:/
  • 我在 C# 中添加了一个简单的示例。
  • 好吧,它现在工作得很好,你能不使用 windows.h 和其他 3rd 方库将阅读部分转换为 C++,因为 c++ 可执行文件是为仅支持本机 c++ 的设备编写的。感谢您的时间和精力! :)
  • 不抱歉,我让您进行转换,因为我没有时间自己做。你现在有了通用的二进制序列化机制,实现起来应该很简单:^)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-08-24
  • 2015-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-19
相关资源
最近更新 更多