Qt Network Communication
-
Hi All,
I'm new at Qt and I would like to learn how to transfer objects via TCP. I'm thinking about some kind of ".Net Remoting". Are there any cross-platform ways to do it? I tried to serialize the object into a QDataStream and transfer it, but it would be better to use a general solution.
Could you please tell me some options for interprocess communication via TCP?
Thank you in advance
-
welcome to devnet
Probably a good start are the "network examples.":http://qt-project.org/doc/qt-5.0/qtnetwork/examples-network.html Typically the "fortune client":http://qt-project.org/doc/qt-5.0/qtnetwork/fortuneclient.html and "fortune server":http://qt-project.org/doc/qt-5.0/qtnetwork/fortuneserver.html are demonstrating the implementation on both sides. In order to use them you can run both on the same machine.
Using TCP/IP has the advantage to run on one or on several machines the different processes.
Concerning the interoperability with different OS that is no problem, because TCP/IP is handled by all OS. Qt may be used on major OS as well. In general you need only to compile for your designated OS platforms.
-
A TCP socket is not at the level of abstraction you might want though: It sends a number of bytes only. Transferring objects over a TCP socket is not trivial. You usually want to transfer data only, not implementation details like which object holds the data in which way.
If you want to implement objects talking to each in an distributed environment then you might want to check out non-Qt mechanism like CORBA (is there any newer cross-platform solution for distributing objects nowadays? CORBA was the hype in the 90s...), which is pretty similar in functionality to .Net remoting. It does require you to define interfaces to objects in a special language (IDL) and then generates bindings for your programming language of choice from those IDL files. The good(?) thing is that you can implement all your objects in a different programming language thanks to that;) With .net remoting this step seems not to be necessary, but of course you are bound to .net for your implementation.
In my experience such an approach will not work too well since objects will have a radically different reaction times based on whether they are local, in a different process or maybe even on some other machine. This is impossible to hide from the user. The only way around this is to define which objects go together in one binary: You end up with a set of objects in a "server" and another set in a "client" and have these two communicate.
The whole network-transparent-object approach quickly becomes overkill then IMHO. A custom (maybe even HTTP-based) protocol usually is able to provide the same functionality with less overhead. You usually end up with a simpler protocol that is easier to understand and debug, too.
-
Hi storedenen!
For transfring objects via TCP you can use something as following:
@struct structName
{
int varint;
char varchar [10];
};
QDataStream &operator <<(QDataStream &out,const structName &dataStruct);
QDataStream &operator >>(QDataStream &in, structName &dataStruct);
in *.cpp file
QDataStream &operator <<(QDataStream &out,const structName &dataStruct)
{
out << dataStruct.varint;
out.writeRawData ( dataStruct.varchar, 10 );
return out;
}QDataStream &operator >>(QDataStream &in, structName &dataStruct)
{
dataStruct = structName();
in >> dataStruct.varint;
in.readRawData ( dataStruct.varchar, 10 )
return in;
}@for easy interprocess communication via TCP i can recomendate "Qxt’s":http://libqxt.bitbucket.org/doc/0.6/modules.html QxtNetwork with RPC stuff
-
How about Restful WebServices with JSON for serialization ?
-
Thank you very much for everybody.
I think I need to learn more about the Qt's networking ability based on your comments.