开发者

Ruby TCPSocket read_all

开发者 https://www.devze.com 2022-12-29 15:54 出处:网络
Is there a method that can act like read_a开发者_JAVA技巧ll, 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 t

Is there a method that can act like read_a开发者_JAVA技巧ll, 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!


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.


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 :)

0

精彩评论

暂无评论...
验证码 换一张
取 消