Send Binary (Image) Using a TCP SocketAuthor: Carl Sassenrath Ok, so someone recently asked me, "what's a simple way to send an image between computers, in binary, using TCP sockets?" Since there was no simple example given, I quickly wrote this one up... Sending binary data over a network is different than sending text. In text mode, you can wait for each line to be received, then process the line. But in binary, how do you know when you've received all the data? There is no "line terminator" nor can you use any other character to indicate the end (because they may appear in the binary data you want to send). So, what's the trick to sending binary? How do you know when all the data has been received? One easy solution to this problem is to send the length of the data at the beginning of the transmission. That length can be used by the receiver to know when all the data has been received. With that in mind you are ready to write the sender (also called the "client"). This code must read the binary file and send its data. But, it first inserts in front of the data a small REBOL text block that provides the file name and data length. (Often called a "header".) The text is null (zero byte) terminated to mark its end.
Notice the WAIT SERVER line. If you close the socket before all the data has been sent, the receiver could miss the end! Now you can write the receiver code (also called the "server").
Above, a portion of the data is received and converted to a REBOL block to discover the file name and data length. The rest of the data is then received and saved into the file. Note again that the client is the one that finally terminates the connection. To try this example, change the file name in the sender to that of any image you have. Next, run the receiver code (you are starting a server). Leave that running. Now, run the sender and poof! The file is transferred. Of course, if you want to do much else, there is more to learn about this whole client/server process. See the manual or search the web for REBOL server examples. Hopefully, this example will get you started in the right direction. |