Reputation: 375
Can we send structure which contains pointers to another structures, via QTcpSocket socket to QTcpServer socket of a program running at other physical location then this program. My code will be like this....
<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>
but this have error like: /TcpClient-build-desktop/../TcpClient/tcpclient.cpp:137: error: no match for ‘operator<<’ in ‘out.QDataStream::operator<<(0) << c’
Upvotes: 2
Views: 2461
Reputation: 32665
You can use Qxt which has a RPC mechanism in its Network module. You can connect signals and slots through network and send objects of arbitrary types as arguments. You can get it here.
Upvotes: 0
Reputation: 1567
Sure you can, but it wouldn't make much sense. :-) If the structure has pointers, then those will be invalid on the remote host.
Of course, you could additionally send what the pointers point to, if you need it.
One tip: if you program for portability, remember to convert any integers to network byte order before transferring, and back to host byte order afterwards.
Wikipedia article about endianness.
Upvotes: 1
Reputation: 1220
Adding to what has been said you could also use an approach similar to that of the RPC system which also sends the object those pointers point to but keeps them on the heap and changes the pointers on the recieving machine accordingly, that way you actually get pointers that work.
To accomplish this without error is of course not easy and you should use this approach only if it is impossible to change your concept.
Best regards
D
Upvotes: 0
Reputation: 41519
You can, as long as the receiver knows that it cannot only have the pointers dereferenced by the sending process. That would mean communicating back to the source and awaiting the result. This process is called 'object broking' and has been implemented in e.g. CORBA, COM+, jre and others.
A pointer is only to be interpreted in a known piece of virtual memory.
Upvotes: 2