gpt4 book ai didi

qt - 通过 QTcpSocket 发送结构

转载 作者:行者123 更新时间:2023-12-04 06:27:22 25 4
gpt4 key购买 nike

我们可以通过 QTcpSocket 套接字将包含指向另一个结构的指针的结构发送到运行在其他物理位置的程序的 QTcpServer 套接字然后这个程序。我的代码会是这样的......

<i>
QTcpSocket tcpSocket = new QTcpSocket(this);
Struct a{ int a1; int a2;} __attribute__((packed));

Struct b { int b1; int b2}__attribute__((packed));
Struct c{ a *c1; a*c2; }QByteArray block;

QDataStream out(&block, QIODevice::WriteOnly);
out << quint16(0)<<c;
out << quint16(block.size() - sizeof(quint16));
tcpSocket->write(block);
</i>

但这有如下错误:
/TcpClient-build-desktop/../TcpClient/tcpclient.cpp:137: 错误:“out.QDataStream::operator<<(0) << c”中的“operator<<”不匹配

最佳答案

当然可以,但意义不大。 :-) 如果结构有指针,那么这些在远程主机上将是无效的。

当然,如果需要,您还可以发送指针指向的内容。

一个提示:如果您为可移植性编程,请记住在传输之前将任何整数转换为网络字节顺序,然后再转换回主机字节顺序。

Wikipedia article about endianness .

关于qt - 通过 QTcpSocket 发送结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5880287/

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