[Webinar] Streamline your web hosting managementRegister Today


How is a program sended through TCP ?

Posted on 2013-05-30
Medium Priority
Last Modified: 2013-11-13
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 ?!
Question by:AlexMert
  • 3
  • 3
LVL 25

Expert Comment

ID: 39208723
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.

Author Comment

ID: 39208884
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 !
LVL 25

Expert Comment

ID: 39208950
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 )
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions


Author Comment

ID: 39209067
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 !
LVL 25

Accepted Solution

clockwatcher earned 1100 total points
ID: 39209130
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.

Author Closing Comment

ID: 39209140
Excellent man ! Now you clarified me out ! Awesome resonse :) Thanks alot!

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Getting a solution on Experts Exchange is pretty easy--if you are asking the right questions in the right way.  While you won't get answers like RTFM or STFW from EE, experts will shy away from questions that are not focused, are badly explained, o…
Understanding the various editions available is vital when you decide to purchase Windows Server 2012. You need to have a basic understanding of the features and limitations in each edition in order to make a well-informed decision that best suits …
Saved searches can save you time by quickly referencing commonly searched terms on any topic. Whether you are looking for questions you can answer or hoping to learn about a specific issue, a saved search can help you get the most out of your time o…
Where to go on the main page to find the job listings. How to apply to a job that you are interested in from the list that is featured on our Careers page.

608 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question