简体   繁体   中英

Can´t execute file after writing it to the disk

I have a few files I transfer over TCP. However when I then try to execute them I get an windows screen pop up, saying I should contact the developer to get an appropriate version for my pc. The files should be transferred correctly so I am wondering where the problem lays.

Server OS: Windows Server 2016

My PC OS: Windows 10

Server code

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

    }

Client Code

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

EDIT: Forgot to check for file size. All the files have a size of zero bytes so there must be an error somewhere while sending.

Aside from things like checking the correct runtime versions are installed on the target system and the like...

Could it be that when you are not receiving the entire file content in a single Read operation?

You can (and should) ensure that you read all the data using something like:

(Untested...)

var offset = 0;
while (offset < length)
{
    var read = stream.Read(buffer, offset, length - offset);
    if (read == 0) {
        //Socket disconnected
        break;
    }
    offset += read;
}

You can (and should) also do a similar thing with the Write call to ensure that all data has been written... it doesn't happen often, but it does happen - for various reasons that are not for this thread :) (Murphy's Law and all that!)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM