我有一些通过TCP传输的文件。但是,当我随后尝试执行它们时,我弹出一个Windows屏幕,说我应该联系开发人员以获取适合我的PC的版本。文件应该正确传输,所以我想知道问题出在哪里。
服务器操作系统:Windows Server 2016
我的PC操作系统:Windows 10
服务器代码
private void SendLauncherFiles(NetworkStream stream)
{
TCPManager manager = new TCPManager();
Console.WriteLine("Sending Launcher files");
string path = System.AppDomain.CurrentDomain.BaseDirectory;
path += "LauncherFiles\\";
string[] extensions = { "LegitAimLauncher.exe", "Bleak.dll", "Jupiter.dll", "PeNet.Asn1.dll", "PeNet.dll" };
for(int i = 0; i < extensions.Length; i++)
{
path += extensions[i];
byte[] bFileData = File.ReadAllBytes(path);
string newPath = path.Replace(extensions[i], "");
path = newPath;
manager.SendInt(bFileData.Length, stream);
stream.Write(bFileData, 0, bFileData.Length);
}
}
客户代码
private void ReceiveFiles(NetworkStream stream)
{
TCPManager manager = new TCPManager();
string path = System.AppDomain.CurrentDomain.BaseDirectory;
path += "LauncherFiles\\";
string[] extensions = { "LegitAimLauncher.exe", "Bleak.dll", "Jupiter.dll", "PeNet.Asn1.dll", "PeNet.dll" };
for(int i = 0; i < extensions.Length; i++)
{
path += extensions[i];
int length = manager.GetInt(stream);
byte[] bFileData = new byte[length];
stream.Read(bFileData, 0, bFileData.Length);
File.WriteAllBytes(path, bFileData);
string newPath = path.Replace(extensions[i], "");
path = newPath;
}
}
TCPManager
public void SendInt(int data, NetworkStream stream)
{
byte[] bData = BitConverter.GetBytes(data);
stream.Write(bData, 0, bData.Length);
}
public int GetInt(NetworkStream stream)
{
byte[] bData = new byte[sizeof(int)];
stream.Read(bData, 0, bData.Length);
int Data = BitConverter.ToInt32(bData, 0);
return Data;
}
编辑:忘记检查文件大小。所有文件的大小均为零字节,因此发送时一定在某个地方出错。
答案 0 :(得分:0)
除了诸如检查正确的运行时版本之类的安装在目标系统上之外,诸如此类...
是否可能是因为您在一次Read
操作中没有收到全部文件内容?
您可以(并且应该)确保使用以下方式读取所有数据:
(未经测试...)
var offset = 0;
while (offset < length)
{
var read = stream.Read(buffer, offset, length - offset);
if (read == 0) {
//Socket disconnected
break;
}
offset += read;
}
您也可以(并且应该)对Write
调用执行类似的操作,以确保所有数据都已被写入...虽然它不会经常发生,但它确实会被写入 发生-出于各种原因,而不是此线程:)(墨菲定律及其他所有原因!)