exe to bytes?

BlitzPlus Forums/BlitzPlus Beginners Area/exe to bytes?

Bergmann(Posted 2013) [#1]
I was wondering if it would be possible to convert an exe file into bytes to be broken apart and sent via TCP and then put back together again on a client. Can anyone help me?


Captain Wicker (crazy hillbilly)(Posted 2013) [#2]
no. it is not possible to convert pre-compiled Windows *exe files from machine code (Blitz) to byte code. You would have to recompile your program using a language that compiles to byte code.


Yasha(Posted 2013) [#3]
Indeed you can, you can send any kind of file over TCP as the content doesn't matter, it's just data when it's being sent. Note that the mere fact you can send it makes no guarantee at all about whether it can be run at the destination, however (it may be that the other computer needs permissions you can't set... nothing Blitz can do about that easily).

You can load an entire file into a Blitz bank with ReadBytes (if you create the bank to be the size of the whole file and use that size to read the data). Once you have that, you can decide on a block size, move along the bank copying each of those blocks to a TCP stream, and reconstruct an equivalent bank at the other end from each block as it comes in (to copy a "block" from a chunk of the bank into a TCP stream, you can also use WriteBytes with the stream as the target and the start and length of the block within the bank as the parameters; you'll probably want to write an integer header first to indicate whether the block being sent is complete, or if it's the last one and slightly shorter). Once the whole receiving bank is filled, the receiving program can dump it out to disk with WriteBytes.

This is the same very simple procedure you'd use to send any file, executable or not, between two computers (there are better ways but this is pretty straightforward). The fact it's an exe you want to send isn't really very important - whether it runs once it gets there (or whether it's safe) is the responsibility of the host system, not the file transfer.

An example (I make no guarantee this is "good enough" 'cause I'm tired, but it works):





Copy any file you like and either rename it to "myFile.o", or change that name in both programs. Run the server program in the first box, and while it's running, also run the client program in the second box: it will receive in chunks a copy of myFile.o and save it to myFile2.o.

As I said above, whether the file is executable isn't really the business of Blitz (machine code is just binary data, to this code), but up to the OS at the other end to decide how to handle if you try to execute it there.


On post #2: the question does not appear to have anything to do with bytecode. Bytecode is something else. (As it happens, there's also no particular reason why you couldn't compile x86 to any other bytecode, except that there is usually no good reason for doing so.)


Kryzon(Posted 2013) [#4]
Are there any criteria for determining an appropriate data-chunk size?

@Aimless:

BlitzPlus Documentation:
- ReadFile (to open the local .EXE file)
- ReadBytes (to collect data from the local .EXE file)

Old Blitzcoder Articles:
- Using Blitz Banks
- Using Blitz TCP Commands
- Basic Blitz TCP server communication


_PJ_(Posted 2013) [#5]


Kryzon (Posted 1 day ago) #4

Are there any criteria for determining an appropriate data-chunk size?



Size of file, speed/reliability and bandwidth of connection, urgency and acceptance of risk -

I would even suggest that tens of kBytes per chunk is fine