Ruby TCPSocket read_all

发布于 2024-09-01 04:13:19 字数 182 浏览 6 评论 0原文

是否有一种方法可以像 read_all 一样工作,而不是使用 TCPSocket.read(no_of_bytes),只需 TCPSocket.read_all 即可。我首先通过 YAML::dump'ing 发送对象,然后发送它们,但我不知道如何获取它们的大小(以字节为单位)。预先感谢,嗯。哦,我对任何形式的网络编程都非常非常陌生,所以慢慢来吧!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

放肆 2024-09-08 04:13:40

我怀疑有这样的功能。然而!写它确实是最简单的部分。我必须让这种语言变得不可知,因为我已经很长时间没有编写任何 ruby​​ 代码了,但在伪代码中它基本上就像这样

def read_all(s)
   buffer = ""

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

      buffer = buffer + tmp
   end

   return buffer
 end

完成了。循环并接收,直到没有更多可用数据为止。这是最简单的任务之一:)

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

东北女汉子 2024-09-08 04:13:37

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文