PDA

View Full Version : Speaking Applications in Qt



lorik
14th September 2011, 14:00
I need to write 2 applications that can speak with each other on the same computer?

What is the best way of doing it in QT?

pkj
14th September 2011, 14:05
Refer QProcess class. You can also use network classes to communicate by setting your own protocol

lorik
14th September 2011, 14:24
Can you refer to some example/s of setting my own protocol using network classes?

pkj
14th September 2011, 17:19
Suppose App A is to send to App B. In app A make a class deriving from QTCPServer. There you maintain the state of connections to App A. Provide some handshake string to confirm. Then let App B ask for objects by stating, say the class names. The derived class in App A is supposed to send object class name, serialized object, etc. In app B, deserialize it and knowing the object type beforehand, you can deserialize and you have your object in B. Protocol will be your way how you wish your apps to talk. Since tcp guarantees delivery, it can be implemented.
As far as examples are concerned, merely knowing network classes and qtcpserver class should suffice. However, this does assume you are making both the applications. I had used this to communicate a qt app with a .net app. There I had to make sure I transfer only most basic types like int, double, array etc and manage endianness because .net didn't know how qt does its serialization. If both are qt apps, if both apps have same class definitions, you can transfer whole data objects in one go and deserialize at the other end. Obviously this means that both applications have the same definitions of classes of data objects you are sending over. A more smart serialization process, where in you serialize basic types with some string pair, etc. might be a good idea too.

wysota
14th September 2011, 18:42
Those apps are running on the same machine. Using TCP seems like a bit of overkill.

pkj
14th September 2011, 19:18
Agreed. It will be like sending entire US army to find one person. But the question is will it be just as much costly and time taking? :)

wysota
14th September 2011, 19:21
If you use QLocalSocket or a number of other IPC mechanisms available, you don't have to worry about most of the things you mention.