Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 )
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

An introduction to the wonderful sport of Scam Baiting.  Learn how to help fight scammers by beating them at their own game. This great pass time helps the world, while providing an endless source of entertainment. Enjoy!
In this article, I’ll show how research, determination, and use of modern technology helped me solve a DNA mystery.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

721 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