Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How is a program sended through TCP ?

Posted on 2013-05-30
6
Medium Priority
?
501 Views
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 ?!
0
Comment
Question by:AlexMert
  • 3
  • 3
6 Comments
 
LVL 25

Expert Comment

by:clockwatcher
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.
0
 

Author Comment

by:AlexMert
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 !
0
 
LVL 25

Expert Comment

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

http://www.autoitscript.com/autoit3/docs/functions/FileOpen.htm

FileOpen ( "myoutputfile.exe", 16 )
0
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 

Author Comment

by:AlexMert
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 !
0
 
LVL 25

Accepted Solution

by:
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.
0
 

Author Closing Comment

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

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

Sometimes clients can lose connectivity with the Lotus Notes Domino Server, but there's not always an obvious answer as to why it happens.   Read this article to follow one of the first experiences I had with Lotus Notes on a client's machine, my…
Compliance and data security require steps be taken to prevent unauthorized users from copying data.  Here's one method to prevent data theft via USB drives (and writable optical media).
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…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Suggested Courses

876 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