B
Bigmac Turdsplash
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?
server.rb...
How can i do this with out using ftp?
Bigmac said: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?
Albert said: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.
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?
Robert said:You can use DRb or simply stream it through a socket.
Kind regards
robert
Bigmac said: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
Robert said:A few remarks:
The file handle is not closed properly. These two lines above can be
replaced by
fileContent = File.read('file.jpg')
I'd rather use sock.write here as this is the more low level output
operation.
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
Bigmac said: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?
file = File.new('file.jpg')
destFile = File.new('file-received.jpg', 'w')
2009/5/11 Albert Schlef said:You're probbaly using Windows. Instead of:
Do:
file = File.new('file.jpg', 'b')
destFile = File.new('file-received.jpg', 'wb')
destFile = File.new('file-received.jpg', 'wb')
Bigmac said:#client.rb
require 'socket'
sock = TCPSocket.open('localhost', 2000)
destFile = File.new('c:\\hacked\\text.txt', 'w')
destFile.print
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.