ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> ruby-talk
ruby-talk
Sending Binary Data?
by Yacin Nadji other posts by this author
Feb 28 2006 12:52AM messages near this date
Re: Subclassing Struct.new | Re: Sending Binary Data?
Hello Ruby Gurus!

My friend and I are writing a chat/IM client in Ruby. It's nothing 
special, but it's helping us learn the language, especially the lower 
level networking parts really well and I'm thoroughly enjoying the 
project so far. I thought implementing file transfers would be pretty 
nifty, but I'm running into some problems. I did a simple copy by 
reading the file (a picture) and adding the lines to a string, and 
writing that string out to a new file:

a = String.new

IO.foreach("lighter.jpg") do |line|
  a += line
end

file = File.open("new.jpg","w")
file.write(a)
file.close

And it worked swimmingly, however, when I tried to do this over a 
TCPClient/Server:

server.rb

require 'socket'
port = 9191
file = File.open("omgnew.jpg","w+")
server = TCPServer.new('localhost', port)
while session = server.accept
  file.write(session.gets)
  server.close
  file.close
end

client.rb

require 'socket'

server = TCPSocket.new('localhost',9191)
file = String.new
IO.foreach("lighter.jpg") do |line|
  file += line.chomp
end

server.send(file,0)


It works for a bit, then I get this error:

server.rb:7:in `accept': closed stream (IOError)
        from server.rb:7

The file sizes are nearly identical, I'm off by 479 bytes:

ynadji@onizuka:file_transfer$ du -b *  
122753  lighter.jpg
122274  omgnew.jpg

Any ideas? I could be doing this entirely wrong, searching didn't yield 
any helpful results, so I figured I'd turn here. I was reading around 
with Array#pack and String#unpack which seems to be heading in the right 
direction, but I'm completely lost :P.

Thanks!

Yacin Nadji
Thread:
Yacin Nadji
Logan Capaldo
Robert Klemme

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved