gpt4 book ai didi

ruby - 用 uint34 打包字符串

转载 作者:行者123 更新时间:2023-12-03 12:05:35 25 4
gpt4 key购买 nike

我试图通过套接字发送一些东西,所以我做了一个简单的协议(protocol),它是:

[消息长度(uint32)][包id(uint32)][消息]

那么,如何使用此协议(protocol)发送消息?我试过这个:

message = 'hi'
parent_socket.send([message.length, 2].pack('LL') + message, 0)

它似乎不起作用。我得到的只是长度和数据包ID。
我怎么能弄清楚这个?

(更新)
我用来接收消息的代码是:
if child_socket.ready?
header = child_socket.read(8).unpack('LL')
length = header[0]
packet = header[1]
case packet
when 1
stdin.write(child_socket.read(length))
when 2
puts child_socket.read(length)
#send console
else
Console.show "Unknown packet : #{packet}"
end

输出为 10。看起来很正常 (4 + 4 + 2)

在这种情况下,长度为2,数据包为2。所以它切换到'when 2'但它什么也不输出。

最佳答案

问题是您只阅读前 8 个字节。

header = child_socket.read(8).unpack('LL')

根据 IO#read 的文档,如果您不传递长度,它将读取到 EOF,它应该得到所有内容。所以只需删除该长度参数:
header = child_socket.read.unpack('LL')

关于ruby - 用 uint34 打包字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18331232/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com