结构解包奇怪的结果

2024-04-19 19:30:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在和一个讨厌的虫子斗争。我有两台电脑。我从一个发送数据,从另一个接收数据。你知道吗

起初我尝试了struct fmt,我是这样发送的:

    # Two chars and three shorts
    fmt = struct.Struct('2B 3h')
    # Two chars and three floats
    fmt2 = struct.Struct('2B 3f')
    message=[0]*5
    message[0] = cmd
    message[1] = 6
    message[2] = d1
    message[3] = d2
    message[4] = d3
    conn.send(fmt.pack(*message))

客户机上的值已正确解包并正确无误:

raw = self.s.recv(BUFFER_SIZE)
idx = 0
while idx < len(raw):
    length, data = parse(raw, idx)
    idx += length

    cmd, length, x, y, z = fmt.unpack(data)

但是如果在服务器和客户机上使用第二种格式(fmt2),解包就会失败。(我注意到d1、d2和d3是浮动的)。我试着一个接一个地解压这些值,但是得到了x,y,z的错误数据

我不知道如何解决这个问题,我不太熟悉struct。你知道吗

谢谢


Tags: andcmdmessagerawlengthstructd2d1