views:

137

answers:

2

Is there a method that can act like read_all, as in instead of using TCPSocket.read(no_of_bytes), one can simply TCPSocket.read_all. I am sending objects first by YAML::dump'ing them then sending them but I don't know a way to get their size in bytes. Thanks in advance, ell. Oh and I am very, very new to any form of network programming so go easy on me!

+1  A: 

Can't help you with Ruby, but the usual practice with object serialization and networking is to either send the length first, so you know how much to read, or use a pre-defined delimiter to separate messages.

Nikolai N Fetissov
+1  A: 

I doubt there is such a function. HOWEVER! Writing it really is the easiest part. I'm going to have to make this language agnostic, because it's a long time since I've written any ruby code, but in pseudocode it is basically like this

def read_all(s)
   buffer = ""

   while (tmp = s.recv(128))
      if tmp == end_of_file
         break
      end

      buffer = buffer + tmp
   end

   return buffer
 end

Done. Looping and receiving until there is no more data available. That's one of the easiest tasks :)

LukeN