2017-05-18 1 views
0

내 서버 모듈의 이진 파일 (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; 
    } 
} 

그런 다음 내가 같은 클라이언트로 전송 : 클라이언트 측 (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