How can you send complex objects with sockets?
Solved
General and Desktop
-
hello to the whole community Qt Forum,
I have a question, I'm developing a client / server application and I would like to know if there are socket (qt) functions capable of sending complex objects of variable size, but note when sending.
Obviously the receiver will also know the size;
Thanks in advance -
-
@danga96 said in How can you send complex objects with sockets?:
I need to send an object through a socket, not through a file
#include <QCoreApplication> #include <QDebug> #include <QDataStream> #include <QByteArray> struct complex_object{ QString m_string; float m_float; int m_int; }; int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); complex_object cp; cp.m_string = "Some 💩"; cp.m_float = 0.666; cp.m_int = 42; qInfo() << "before send:" << cp.m_string << cp.m_float << cp.m_int; QByteArray data; // used by socket stuff to hold data QDataStream sdata(&data, QIODevice::WriteOnly); sdata << cp.m_string << cp.m_float << cp.m_int; qInfo() << "... magically floating through cyberspace via sockets"; complex_object cp2; QDataStream rdata(&data, QIODevice::ReadOnly); rdata >> cp2.m_string >> cp2.m_float >> cp2.m_int; qInfo() << "after receive:" << cp2.m_string << cp2.m_float << cp2.m_int; return a.exec(); }
I like puppies and kittens... (reference to show that describes "knowing your place", and yes, totally random)