Wednesday 7 December 2011

Transferring files over internet

I was reading this script of Nick Antonaccio here:
http://www.rebol.org/view-script.r?script=binary-file-transfer.r

And I was mazed how is simple to send files with Rebol, here how it works:
First of all the receiver PC start to hear on TCP port 8 (but you can choose any port you wish:

; server/receiver - run first:
if error? try [port: first wait open/binary/no-wait tcp://:8] [quit]
mark: find file: copy wait port #""
length: to-integer to-string copy/part file mark
while [length > length? remove/part file next mark] [append file port]
view layout [image load file]


Then the sender crate and send the image:
; client/sender - run after server (change IP address if using on 2 pcs):
save/png %image.png to-image layout [box blue "I traveled through ports!"]
port: open/binary/no-wait tcp://127.0.0.1:8   ; adjust this IP address
insert file: read/binary %image.png join l: length? file #""
insert port file


and here the result:



There are just few lines of code, and this way you can transfer any file (music, videos, images, text, whatever) trough internet. Amazing!
You can learn more here: http://www.rebol.net/cookbook/recipes/0058.html

No comments:

Post a Comment