我想從我的服務器模塊發送一個二進制文件(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」... – xTyrion