简体   繁体   中英

Ruby TCPSocket read_all

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!

使用Ruby无法为您提供帮助,但是对象序列化和网络连接的通常做法是先发送长度,以便您知道要读取的长度,或者使用预定义的分隔符来分隔消息。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM