Forum: Ruby File over tcp? with out using net/ftp

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D974b374bd09edcef9285ff7c4f59ea5?d=identicon&s=25 Bigmac Turdsplash (i8igmac)
on 2009-05-08 01:51
Im trying to send a file back and forth between a client.rb and
server.rb...

How can i do this with out using ftp?
699c00ad35f2755810b4aa5f423d73e2?d=identicon&s=25 Albert Schlef (alby)
on 2009-05-08 09:39
Bigmac Turdsplash wrote:
> Im trying to send a file back and forth between a client.rb and
> server.rb...
>
> How can i do this with out using ftp?

There are gazillion other protocols for transferring files expect FTP.

For example, you can use HTTP. Write a tiny server with Sinatra, and the
client with Mechanize.

You can also write your own protocol.
699c00ad35f2755810b4aa5f423d73e2?d=identicon&s=25 Albert Schlef (alby)
on 2009-05-08 09:44
Albert Schlef wrote:
> Bigmac Turdsplash wrote:
>> Im trying to send a file back and forth between a client.rb and
>> server.rb...
>>
>> How can i do this with out using ftp?
>
> There are gazillion other protocols for transferring files expect FTP.
>
> For example, you can use HTTP. Write a tiny server with Sinatra, and the
> client with Mechanize.
>
> You can also write your own protocol.

Sorry, I didn't notice the "back and forth" part. So HTTP isn't very
suitable for this. Right now I can't think of some other protocol.

What's the problem with FTP?
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-05-08 17:30
(Received via mailing list)
On 08.05.2009 01:51, Bigmac Turdsplash wrote:
> Im trying to send a file back and forth between a client.rb and
> server.rb...
>
> How can i do this with out using ftp?

You can use DRb or simply stream it through a socket.

Kind regards

  robert
D974b374bd09edcef9285ff7c4f59ea5?d=identicon&s=25 Bigmac Turdsplash (i8igmac)
on 2009-05-09 01:53
Robert Klemme wrote:
> On 08.05.2009 01:51, Bigmac Turdsplash wrote:
>> Im trying to send a file back and forth between a client.rb and
>> server.rb...
>>
>> How can i do this with out using ftp?
>
> You can use DRb or simply stream it through a socket.
>
> Kind regards
>
>   robert

I have been doing a lot of google'n trying to find example code for
sending files threw the socket... I would really like to see some
exmaple code for the client and server... im clueless...

server.rb

require 'socket'
data = file.socket("c:\\file.txt") #????????????
server = TCPServer.new(1234)
loop do
  Thread.start(server.accept) do |connection|
  print connection
    connection.write(data)
  end
end
76630f0c8623c01ff8d70a666c575171?d=identicon&s=25 Philippe Chotard (chobolt)
on 2009-05-09 10:03
Bigmac Turdsplash wrote:
> I have been doing a lot of google'n trying to find example code for
> sending files threw the socket... I would really like to see some
> exmaple code for the client and server... im clueless...

Hi Bigmac, here is a simple exemple you can begin with.

* Client (sends the file) :
require 'socket'
file = File.new('file.jpg')
fileContent = file.read
sock = TCPSocket.open('localhost', 2000)
sock.print fileContent
sock.close

* Server (receives the file) :
require 'socket'
sock = TCPServer.open(2000)
con = sock.accept
msg = con.read
destFile = File.new('file-received.jpg', 'w')
destFile.print msg
destFile.close

Have fun
ChoBolT
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-05-09 10:41
(Received via mailing list)
On 09.05.2009 10:03, Philippe Chotard wrote:
> Bigmac Turdsplash wrote:
>> I have been doing a lot of google'n trying to find example code for
>> sending files threw the socket... I would really like to see some
>> exmaple code for the client and server... im clueless...
>
> Hi Bigmac, here is a simple exemple you can begin with.

A few remarks:

> * Client (sends the file) :
> require 'socket'
> file = File.new('file.jpg')
> fileContent = file.read

The file handle is not closed properly.  These two lines above can be
replaced by

fileContent = File.read('file.jpg')

> sock = TCPSocket.open('localhost', 2000)
> sock.print fileContent

I'd rather use sock.write here as this is the more low level output
operation.

> sock.close
>
> * Server (receives the file) :
> require 'socket'
> sock = TCPServer.open(2000)
> con = sock.accept
> msg = con.read
> destFile = File.new('file-received.jpg', 'w')
> destFile.print msg
> destFile.close

Again, I'd rather use #write instead of #print here.  And use the block
form of File.open.

Some more general remarks: depending on operating systems involved files
should probably opened rather in binary mode than text mode.

Then, as files can grow quite large, your approach (i.e. reading the
whole file into memory before writing it) is likely to fail for large
files.  Usually a solution with small chunks is better.

Kind regards

  robert
D974b374bd09edcef9285ff7c4f59ea5?d=identicon&s=25 Bigmac Turdsplash (i8igmac)
on 2009-05-10 07:15
Robert Klemme wrote:
> On 09.05.2009 10:03, Philippe Chotard wrote:
>> Bigmac Turdsplash wrote:
>>> I have been doing a lot of google'n trying to find example code for
>>> sending files threw the socket... I would really like to see some
>>> exmaple code for the client and server... im clueless...
>>
>> Hi Bigmac, here is a simple exemple you can begin with.
>
> A few remarks:
>
>> * Client (sends the file) :
>> require 'socket'
>> file = File.new('file.jpg')
>> fileContent = file.read
>
> The file handle is not closed properly.  These two lines above can be
> replaced by
>
> fileContent = File.read('file.jpg')
>
>> sock = TCPSocket.open('localhost', 2000)
>> sock.print fileContent
>
> I'd rather use sock.write here as this is the more low level output
> operation.
>
>> sock.close
>>
>> * Server (receives the file) :
>> require 'socket'
>> sock = TCPServer.open(2000)
>> con = sock.accept
>> msg = con.read
>> destFile = File.new('file-received.jpg', 'w')
>> destFile.print msg
>> destFile.close
>
> Again, I'd rather use #write instead of #print here.  And use the block
> form of File.open.
>
> Some more general remarks: depending on operating systems involved files
> should probably opened rather in binary mode than text mode.
>
> Then, as files can grow quite large, your approach (i.e. reading the
> whole file into memory before writing it) is likely to fail for large
> files.  Usually a solution with small chunks is better.
>
> Kind regards
>
>   robert

This is vary helpful... will be easy for me to build functions off this
example...

i have tested with this and it works with text files but thats it...
image files and executable's dont function after being sent threw the
socket...you said i should open files in binary mode? how can i do this
and then after words will the files function properly?

i have been trying to get the client to receive files from the server...
could i see one more example if its not to much to ask?
699c00ad35f2755810b4aa5f423d73e2?d=identicon&s=25 Albert Schlef (alby)
on 2009-05-11 10:06
Bigmac Turdsplash wrote:
> i have tested with this and it works with text files but thats it...
> image files and executable's dont function after being sent threw the
> socket...you said i should open files in binary mode? how can i do this
> and then after words will the files function properly?

You're probbaly using Windows. Instead of:

> file = File.new('file.jpg')
> destFile = File.new('file-received.jpg', 'w')

Do:

file = File.new('file.jpg', 'b')
destFile = File.new('file-received.jpg', 'wb')
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-05-11 10:55
(Received via mailing list)
2009/5/11 Albert Schlef <albertschlef@gmail.com>:
>
> Do:
>
> file = File.new('file.jpg', 'b')
> destFile = File.new('file-received.jpg', 'wb')

I would do that regardless of platform for these reasons

- documentation (immediately clear that this file is treated as binary),
- portability of programs.

Kind regards

robert
D974b374bd09edcef9285ff7c4f59ea5?d=identicon&s=25 Bigmac Turdsplash (i8igmac)
on 2009-05-12 02:18
> file = File.new('file.jpg', 'b')
> destFile = File.new('file-received.jpg', 'wb')

I figured this out the other day... im still having trouble getting the
client to download a file from the server... the process is in my head
but i cant implement this...

This is my attempt... i have been playing around with this code but i
cant figure it out...

#server.rb
require 'socket'


     fileContent = File.read('text.txt')
   sock = TCPServer.open(2000)
   con = sock.accept
   sock.print fileContent
   sock.close

#client.rb
require 'socket'

      sock = TCPSocket.open('localhost', 2000)
    destFile = File.new('c:\\hacked\\text.txt', 'w')
    destFile.print
    destFile.close
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2009-05-12 09:31
Bigmac Turdsplash wrote:
> #client.rb
> require 'socket'
>
>       sock = TCPSocket.open('localhost', 2000)
>     destFile = File.new('c:\\hacked\\text.txt', 'w')
>     destFile.print

That prints nothing to destFile. You probably need something like:

  data = sock.read
  destFile.print data

Your server also needs to loop accepting connections, and ideally
threads to service multiple clients concurrently. Have a look at
gserver.rb in the standard library.
This topic is locked and can not be replied to.