【发布时间】: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