How is a program sended through TCP ?

I know the question is a typical one but it is not ! In fact this is more exactly :

I found that when I transfer a picture thru TCP/IP, I first read it byte with byte and I send then those bytes. I outputed in a MsgBox all the bytes of that small picture and the result looked like this :
0x7EGG45UIGU84HG8UEGEUG5GH479GU8EG  ......... 

Open in new window

Starting with the '0' folowed by an 'x' then a bunch of numers and letters mixed together.
I'm pretty new coder so thats why Im asking and thats why I don't understand some things :
=> After this, I manually extracted this text and copy pasted it in the "FileWrite" function -> to write that string in a new created file to build the picture and clone it . And it worked ! The picture I wanted to clone( to transfer ) was exactly the same as the one built ,just copy pasting the 'byte string' of that picture !

Pretty easy and simple and logic ! I read the picture, outputed the bytes that are composing that picture, took that bunch of letters and numbers with that 0x in front and I wrote exactly that 'text', in a new created file, via ofcourse the programming functions.

BUT when I do exactly the same with a program ( executable ), the file is same size, same icon, BUT is not working when I start it ! It says that is not a valid Win32 program.

? So my question is : how can I do the same with an executable file ? Why when I fileread it and then fileWrite it works, BUT if I show what it had been read, copy that in the 'what to be wrote" parameter of the FileWrite function, don't work just like with the pictures ?!
Who is Participating?

Improve company productivity with a Business Account.Sign Up

clockwatcherConnect With a Mentor Commented:
Character/text data carries with it an encoding.  Translating a binary data stream into a text string involves encoding it.  It's actually why you're having problems with this in the first place.  You can't just send over a binary data stream to something that is expecting text.  You'll get garbage because behind the scenes the thing that's receiving it is decoding it into something that it understands.  In the case of your image, you're not getting enough garbage for it to make a difference.  It's intact enough to still be an image.  If you ran an MD5 on it though, you'd probably find that it didn't match-- that a few bytes had been messed with.  A graphic file is loose enough that in many cases it won't matter.  An executable isn't.

Typically, you would base64 encode a binary data stream, if you had to count on the thing that's receiving it to handle it as text.  Base64 will encode your binary data into an ascii encoding, so an input stream that expects text will be able to safely handle it and not do any unsafe character translation on it when it comes in.   You'd then base64 decode your ascii string back into a raw binary data stream.
You haven't given us enough to go on to really answer the question.  We don't know what language and tools you're using to read your input and write your output.   But based on the fact that you mentioned FileWrite.  I'm guessing that might mean that you're using java's FileWriter.  FileWriter is intended to write character streams not binary streams.  It does character encoding.  If you really are using java, you'll want to look at using a FileOutputStream.  The same can be said for the input side.  You'll want to use an InputStream that doesn't do any character decoding and is compatible with a binary data stream.

If I guessed wrong on your language choice, the problem is still likely the same.  Whatever you're using to do either your reading or writing is probably built to work with character streams and is doing encoding in the background.  You should be using functions/objects/methods that are designed to work with binary data.
AlexMertAuthor Commented:
Oh ok sorry my fault . Right now Im not coding in any major language like java or C , I code in AutoIt 3 ( a script-based language but very very powerful ). But nevermind, I will post this on their forum since they know exactly about this language problems. Thanks anyway !
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Glanced at the AutoIt docs and you need to make sure you're opening your file for writing in binary mode.

FileOpen ( "myoutputfile.exe", 16 )
AlexMertAuthor Commented:
Yes I know, thats what Im using, as I said it works perfect with pictures, but let me clarify you perfectly, and Im pretty sure is an interesting subject too :

The FileWrite function in au3, is doing exactly what is saying : writing BYTES in an empty file ! Those BYTES I provide them in it's second parameter so looks like this :
FileWrite( "C:\calc.exe" , $bytes )

Open in new window

where the $bytes is the variable from the second parameter that contains the bytes that I need to copy in the new created file, to clone the previous one. Everything clear till now ? Ok then how do I obtain those bytes ? I read the file I want to clone byte by byte and save all of them in a variable that I will send it to that second parameter of the FileWrite function. So I copy the file from one location to another, byte by byte :). Clear till now ? ok, but what I REALLY WANT IS :

- to transform those bytes into a string or text that I could copy and send it via Messenger to a friend that will take this text and transform it back into exactly the same bytes tyhat I had and apply them too to the FileWrite function, exactly like me ! As I said , this works with picture type files, but not with executable files !
AlexMertAuthor Commented:
Excellent man ! Now you clarified me out ! Awesome resonse :) Thanks alot!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.