From: Joel VanderWerf Date: 2008-09-06T15:48:52+09:00 Subject: Re: Question About TCPServer & TCPSocket classes Steve Lewis wrote: > Hello Rubyists, > > I've been messing around with socket programming in Ruby lately, and I've > hit a snag with some of my experiments. My first, (successful), > experiment was to create a basic client/server script wherein the client > sends a string to the server, and the server responds by simply writing > the string back to the client...that code is as follows: > > Server: > ##################################### > require 'socket' > > class PingServer < TCPServer > > def start_server > loop do > Thread.start(self.accept) do |s| > p "Connection accepted from server #{s.inspect}" > request = s.readline.gsub(/\n$/, '') ^^^^^^^^ This method reads until newline, and binary data (such as Marshal.dump output) is not generally delimited that way. Use IO#read or Socket#recv instead. If you want to read a binary message, you can do one of several things: 1. pick a delimiter char and escape that wherever it occurs within the binary data (yuck) 2. send a length field before sending the data, so the receiver knows how much to wait for 3. send one message per socket connection 4. encode the binary data in some non-binary form: e.g. base64, using pack("m"), and delimit using blank lines. -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407