# Sending files over a network

**URL:** <https://www.ruby-forum.com/t/sending-files-over-a-network/56587>\
**Category:** Ruby\
**Created:** [April 1, 2006, 12:20pm UTC](https://www.ruby-forum.com/t/sending-files-over-a-network/56587 "2006-04-01T12:20:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pieter\_Breed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@Pieter\_Breed](https://www.ruby-forum.com/u/Pieter_Breed)\
**Post date:** [April 1, 2006, 12:20pm UTC](https://www.ruby-forum.com/t/sending-files-over-a-network/56587/1 "2006-04-01T12:20:34Z")

</div>

Hi All,

I am relatively new to Ruby and I am considering using it for a little  
itch of my own I want to scratch. It deals with sending files over a  
network, but in a one-computer-to-one-computer way, not swarming like  
with torrents.

My question is two-fold: What is, in general, the better way of sending  
files across a network like this. By better I mean appropriate for  
files that may be both very long and very short? Some error checking  
also needs to be done so the files don’t corrupt. I realise this is not  
really ruby specific, but bear with me please.

How would you do/implement this in Ruby? If I was more familiar with  
either Ruby or network programming I could probably bootstrap my  
knowledge, but it seems I just don’t know enough.

Regards,  
Pieter Breed

---

<div class="post-metadata">

**Author:** ![Pieter\_Breed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@Pieter\_Breed](https://www.ruby-forum.com/u/Pieter_Breed)\
**Post date:** [April 1, 2006, 7:35pm UTC](https://www.ruby-forum.com/t/sending-files-over-a-network/56587/2 "2006-04-01T19:35:32Z")

</div>

Pieter Breed wrote:

> Hi All,
> 
> I am relatively new to Ruby and I am considering using it for a little  
> itch of my own I want to scratch. It deals with sending files over a  
> network, but in a one-computer-to-one-computer way, not swarming like  
> with torrents.

why not useing ftp?

> My question is two-fold: What is, in general, the better way of sending  
> files across a network like this. By better I mean appropriate for  
> files that may be both very long and very short? Some error checking  
> also needs to be done so the files don’t corrupt. I realise this is not  
> really ruby specific, but bear with me please.
> 
> How would you do/implement this in Ruby?

u dont need to, its been done for you before, lots fo times, just find  
one implementation that suits your needs, and stick to it.

---

<div class="post-metadata">

**Author:** ![Pieter\_Breed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@Pieter\_Breed](https://www.ruby-forum.com/u/Pieter_Breed)\
**Post date:** [April 2, 2006, 12:17am UTC](https://www.ruby-forum.com/t/sending-files-over-a-network/56587/3 "2006-04-02T00:17:35Z")

</div>

Uhm, thanks… I didn’t think of that…

actually I really meant to ask what I asked in the first place… Sorry  
for being bitchy, but I your answer was kind of silly.

Pieter

---

<div class="post-metadata">

**Author:** ![Pieter\_Breed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@Pieter\_Breed](https://www.ruby-forum.com/u/Pieter_Breed)\
**Post date:** [April 2, 2006, 12:32am UTC](https://www.ruby-forum.com/t/sending-files-over-a-network/56587/4 "2006-04-02T00:32:48Z")

</div>

On Sun, 2 Apr 2006, Pieter Breed wrote:

> Uhm, thanks… I didn’t think of that…
> 
> actually I really meant to ask what I asked in the first place… Sorry  
> for being bitchy, but I your answer was kind of silly.
> 
> Pieter

check out ruby-sendfile - you’ll have a tough time getting faster.

regards.

-a

---

<div class="post-metadata">

**Author:** ![Pieter\_Breed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@Pieter\_Breed](https://www.ruby-forum.com/u/Pieter_Breed)\
**Post date:** [April 2, 2006, 10:14pm UTC](https://www.ruby-forum.com/t/sending-files-over-a-network/56587/5 "2006-04-02T22:14:36Z")

</div>

Thanks Konstantin, that is really helpful and I will give it a bash

Regards,  
Pieter

---

<div class="post-metadata">

**Author:** ![Pieter\_Breed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@Pieter\_Breed](https://www.ruby-forum.com/u/Pieter_Breed)\
**Post date:** [April 2, 2006, 5:59pm UTC](https://www.ruby-forum.com/t/sending-files-over-a-network/56587/6 "2006-04-02T17:59:59Z")

</div>

If you know how to work with files, and how to set up connections  
(TCPServer/TCPSocket) you know enough to send a file over network with  
Ruby. Performance will be network-limited, so don’t worry too much  
about it.

You can use MD5/SHA1 for additional error checking, for error checking,  
tough TCP has this feature on its own.

pseudocode ( as in - not tested but with a couple of fixes should  
probably work )

- sender:  
name=“file.txt”; len = File.new(name).stat.size.to\_s  
socket = TCPSocket.new(‘[remote.host.org](http://remote.host.org)’, 6666)  
socket.puts name  
socket.pits.len  
socket.write File.read(name)  
socket.close

- reciever:  
socket = TCPServer.new(‘0.0.0.0’, 6666).accept  
name = socket.gets  
len = socket.gets  
File.open(name, “wb”) { | fl | fl.write (socket.read len.to\_i ) }  
socket.close

* * *

HTH

---

<div class="post-metadata">

**Author:** ![Pieter\_Breed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@Pieter\_Breed](https://www.ruby-forum.com/u/Pieter_Breed)\
**Post date:** [April 3, 2006, 11:16am UTC](https://www.ruby-forum.com/t/sending-files-over-a-network/56587/7 "2006-04-03T11:16:23Z")

</div>

btw, depending on your requirements you might want to have a look at  
[http://rio.rubyforge.org/](http://rio.rubyforge.org/) -  
it gives IO, File, Dir and sevral others a simple consistent interface

- in case you want to work with directories and files and a lot
