2017-05-18 7 views
0

バイナリファイル(Test.bin)を自分のサーバーモジュールからClient Executeableに送信して、メモリに保存します。サーバー側(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; 
    } 
} 

それから私はこのようにクライアントに送信:私は、受信クライアント側(C++)で

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(); 

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"); 
} 

これはすべてうまくいきます。今問題:

私は、クライアントサイドの配列にサーバーサイドのファイルに書き込んだ行をどのように読むことができますか?私はClient-Deviceにファイルを保存してStreamreaderを使用したくないので、メモリからそれを読みたいと思っています!

すべてのヘルプは大歓迎です! (いくつかのコードを提供するのがおそらく最もよいでしょう)

+0

を、あなたは、バイナリデータが改行文字を含めることはできませんか? –

+0

新しい行の文字「\ n」... – xTyrion

答えて

0

文字列をバイナリストリームにシリアル化しているので、私は次のようにします。あなたの配列内の各文字列の場合:

  • 文字列
  • のサイズをシリアライズ文字列 を(あなたが任意の区切り文字を必要としない)シリアライズ。 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(); 
+0

が含まれている可能性がありますか?私はあなたが何を意味するのか本当に理解していないので、/ – xTyrion

+0

私はC#で簡単なサンプルを追加しました。 –

+0

https://pastebin.com/X1u3bBFxは私のためには機能しません:( – xTyrion

関連する問題